Find Intersection Of Two Lines In Java

In this instructional exercise, you will figure out how to find the intersection of the two lines by taking the two lines equation on a 2-D plane through a very simple Java code.
If you don’t know how to find the intersection of the two given lines then you are at the right place to find the solution to your problem.

Intersection Of Two Lines in Java

The purpose of the crossing point implies the time when two lines meet. These two lines are spoken to by the condition a1x2 + b1x + c1= 0 and a2x2 + b2x + c2 = 0 separately.
You can apply the below-given formula to find the coordinates of x and y where the points will intersect.

The formula is:-

x=((b1*c2)-(b2*c1)/(a1*b2)-(a2*b1)) and y=((a2*c1)-(a1*c2)/(a1*b2)-(a2*b1))

Below is the code through which you can achieve your goal of finding the intersection of two given lines.

import java.util.Scanner;
public class LineIntersection
{
    public static void main(String[] args)
    {
        int a1,b1,c1,a2,b2,c2;
        int x,y;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the value of a,b,c of first-line in form of equation ax*x +b*x+c=0");
        a1=sc.nextInt();
        b1=sc.nextInt();
        c1=sc.nextInt();
        System.out.println("Enter the value of a1,b1,c1 of second-line in form of equation a1x*x+b1*x+c1=0");
        a2=sc.nextInt();
        b2=sc.nextInt();
        c2=sc.nextInt();
        x=((b1*c2)-(b2*c1)/(a1*b2)-(a2-b1));
        y=((a2*c1)-(a1*c2)/(a1*b2)-(a2*b1));
        System.out.println("X coordinate is:",x);
        System.out.println("Y coordinate is:",y);
    }
}

The Output is:-

Enter the value of a,b,c of first-line in form of equation a1x*x +b1*x+c1=0
1
2
1
Enter the value of a,b,c of first-line in form of equation a2x*x +b2*x+c2=0
2
3
5
x-coordinate is:-7
y-coordinate is:3

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *