In the previous article, we have seen Java Program to Find Mid-Point of a Line
In this article we will discuss about how to find points that divides a line in given ratio using Java programming language.
Java Program to Find Points that Divides a Line in Given Ratio (Section Formula)
Before jumping into the program directly, let’s first know how can we find points that divides a line in given ratio
Explanation:
Formula to Find Points That Divide A Line By Given Ratio: [(mX2+nX1)/m+n , (mY2+nY1)/m+n]
Where,
- (x1,y1) and (x2,y2) are the two points.
- m:n is the ratio.
Example:
When coordinates of the points are ( -3,1) and (4,-5), 2:3 ratio
=> [(mX2+nX1)/m+n , (mY2+nY1)/m+n]
=>[{(2*4)+(3*-3)}/2+3, {(2*-5)+(3*1)}/2+3 ]
=> [(8-9)/5, (-10+3)/5]
=>[-0.2, -1.4]
Let’s see different ways to find points that divides a line in given ratio.
Method-1: Java Program to Find Points that Divides a Line in Given Ratio By Using Static Input Values
Approach:
- Declare the value for both point coordinates and the ratio ‘
m
‘ and ‘n
‘. - Then call the
sectionPoint()
method by passing the coordinates, m and n value as parameter. - In this method the coordinates are calculated using the formula (mX2+nX1)/m+n , (mY2+nY1)/m+n
- Then print the result.
Program:
// JAVA Code to Find Points that Divides a Line in Given Ratio import java.util.Scanner; public class Main { public static void main(String[] args){ // Coordinates double x1 = -3, y1 = 1, x2 = 4, y2 = -5; // Ratio int m =2,n=3; sectionPoint(x1,y1,x2,y2,m,n); } // Divides a line in a givcn ratio and prints its coordinates public static void sectionPoint(double x1,double y1,double x2,double y2,int m,int n) { double coordinate1 = (m*x2+n*x1)/(m+n), coordinate2 = (m*y2+n*y1)/(m+n); // Prints the section coordinate System.out.println("The coordinates of the line after dividing in "+m+":"+n+" ratio is ("+coordinate1+":"+coordinate2+")"); } }
Output: The coordinates of the line after dividing in 2:3 ratio is (-0.2:-1.4)
Method-2: Java Program to Find Points that Divides a Line in Given Ratio By Using User Input Values
Approach:
- Take user input the value for both point coordinates and the ratio ‘
m
‘ and ‘n
‘. - Then call the
sectionPoint()
method by passing the coordinates, m and n value as parameter. - In this method the coordinates are calculated using the formula (mX2+nX1)/m+n , (mY2+nY1)/m+n
- Then print the result.
Program:
// JAVA Code to Find Points that Divides a Line in Given Ratio import java.util.Scanner; public class Main { public static void main(String[] args) { // Taking user input Scanner scan = new Scanner(System.in); // Coordinates System.out.print("Enter first point coordinates : "); double x1 = scan.nextDouble() ,y1 = scan.nextDouble(); System.out.print("Enter second point coordinates : "); double x2 =scan.nextDouble(), y2 = scan.nextDouble(); // Ratio System.out.print("Enter the ratio to divide the line in : "); int m =scan.nextInt(),n=scan.nextInt(); sectionPoint(x1,y1,x2,y2,m,n); } // Divides a line in a givcn ratio and prints its coordinates public static void sectionPoint(double x1,double y1,double x2,double y2,int m,int n) { double coordinate1 = (m*x2+n*x1)/(m+n), coordinate2 = (m*y2+n*y1)/(m+n); // Prints the section coordinate System.out.println("The coordinates of the line after dividing in "+m+":"+n+" ratio is ("+coordinate1+":"+coordinate2+")"); } }
Output: Enter first point coordinates : 1.5 6 Enter second point coordinates : 0.5 3 Enter the ratio to divide the line in : 1 2 The coordinates of the line after dividing in 1:2 ratio is (1.1666666666666667:5.0)
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Related Java Articles: