Java Program to Find Line Passing Through 2 Points

In the previous article, we have seen Java Program to Check if Three Points are Collinear

In this article we will discuss about how to find line passing through 2 points  using Java programming language.

Java Program to Find Line Passing Through 2 Points

Before jumping into the program directly, let’s first know how we can find line passing through 2 points.

Explanation:

Lets assume we have two point (x1,y1) and (x2,y2) .now we can find equation of the line by

x1x + y1y = c1

x2x + y2y = c2

Let’s see different ways to find line passing through 2 points.

Method-1: Java Program to Find Line Passing Through 2 Points By Using Static Value

Approach :

  • Initialize and declare the point x1,x2.
  • Find out the equation of the line
  • Print it .

Program :

class Main
{
   
     // Driver method
    public static void main(String args[])
    {
        IntsPoint x1 = new IntsPoint(3, 2);
        IntsPoint x2 = new IntsPoint(2, 6);
        lines_pts(x1,x2);
    }
    
     static class IntsPoint
    {
        int a,b;
        public IntsPoint(int a, int b) 
            {
                this.a = a;
                this.b = b;
            }
    }
    //find the line passing through 2 points by using equation
   static void lines_pts(IntsPoint x1, IntsPoint x2)
    {
        int x = x2.b - x1.b;
        int y = x1.a - x2.a;
        int z = x * (x1.a) + y * (x1.b);
        if (y < 0)
            System.out.println("The line passing through points x1 and x2 is: " + x + "x - " + y + "y = " + z);
        else 
            System.out.println("The line passing through points x1 and x2 is: " + x + "x + " + y + "y = " + z);
    }
}
Output:

The line passing through points x1 and x2 is: 4x + 1y = 14

Method-2: Java Program to Find Line Passing Through 2 Points By Using Dynamic Value

Approach :

  • Take input of point x1,x2.
  • Find out the equation of the line
  • Print it .

Program :

import java.util.*;
class Main
{
     // Driver method
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        int l ,m ;
        //taking input of point values
        System.out.println("Enter values of point 1: ");
        l=s.nextInt();
        m=s.nextInt();
        IntsPoint x1 = new IntsPoint(l, m);
        System.out.println("Enter values of point 2: ");
        l=s.nextInt();
        m=s.nextInt();
        IntsPoint x2 = new IntsPoint(l, m);
        lines_pts(x1,x2);
    }
    static class IntsPoint
    {
        int a,b;
        public IntsPoint(int a, int b) 
            {
                this.a = a;
                this.b = b;
            }
    }
    
   //find the line passing through 2 points by using equation
   static void lines_pts(IntsPoint x1, IntsPoint x2)
    {
        int x = x2.b - x1.b;
        int y = x1.a - x2.a;
        int z = x * (x1.a) + y * (x1.b);
        if (y < 0)
            System.out.println("The line passing through points x1 and x2 is: " + x + "x - " + y + "y = " + z);
        else 
            System.out.println("The line passing through points x1 and x2 is: " + x + "x + " + y + "y = " + z);
    }
}
Output:

Enter values of point 1: 
3
2
Enter values of point 2: 
2
4
The line passing through points x1 and x2 is: 2x + 1y = 8

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: