Java Program to Find Points that Divides a Line in Given Ratio (Section Formula)

bookmark


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)