Java find intersection of two lines
Asked Answered
B

4

5

In Java, I have a class Line that has two variables : m and b, such that the line follows the formula mx + b. I have two such lines. How am I to find the x and y coordinates of the intersection of the two lines? (Assuming the slopes are different)

Here is class Line:

import java.awt.Graphics;
import java.awt.Point;

public final class Line {
    public final double m, b;

    public Line(double m, double b) {
        this.m = m;
        this.b = b;
    }

    public Point intersect(Line line) {
        double x = (this.b - line.b) / (this.m - line.m);
        double y = this.m * x + this.b;
        return new Point((int) x, (int) y);
    }

    public void paint(Graphics g, int startx, int endx, int width, int height) {
        startx -= width / 2;
        endx -= width / 2;
        int starty = this.get(startx);
        int endy = this.get(endx);
        Point points = Format.format(new Point(startx, starty), width, height);
        Point pointe = Format.format(new Point(endx, endy), width, height);
        g.drawLine(points.x, points.y, pointe.x, pointe.y);
    }

    public int get(int x) {
        return (int) (this.m * x + this.b);
    }

    public double get(double x) {
        return this.m * x + this.b;
    }
}
Blithering answered 19/7, 2015 at 22:48 Comment(2)
Youve got code there already: Does it not work? Also, think about what you would do on pen and paper with 2 y=mx+b lines if you were trying to solve them. Equate and solve for x to define a general solution for x and then use either lines equation to solve for y. Translate the formulas to codeLambkin
Are you having trouble figuring out the formula? That's Mathematics. Or are you having trouble translating your known formula into code? That shouldn't be too hard, and is already done.Graber
B
9

Lets assume you have these 2 functions:

y = m1*x + b1    
y = m2*x + b2

To find the intersection point of the x-axis we do:

m1*x+b1 = m2*x+b2    
m1*x-m2*x = b2 - b2    
x(m1-m2) = (b2-b1)    
x = (b2-b1) / (m1-m2)

To find y, you use of the function expressions and replace x for its value (b2-b1) / (m1-m2).

So:

y = m1 * [(b2-b1) / (m1-m2)] + b1

You have (this.b - line.b), change to (line.b - this.b).

public Point intersect(Line line) {
    double x = (line.b - this.b) / (this.m - line.m);
    double y = this.m * x + this.b;

    return new Point((int) x, (int) y);
}
Bridoon answered 19/7, 2015 at 23:1 Comment(2)
Perfect! I thought it was just a little error since my numbers were small. Thanks a lot!Blithering
Just be aware, this solution doesn't work for vertical and parallel lines. In case of m-line.m=0 it is undefined.Ezmeralda
J
2

The proposed solution by @wutzebaer seems not to work, instead try the solution below (code based on the example from: https://rosettacode.org/wiki/Find_the_intersection_of_two_lines#Java). s1 and s2 are the endpoints of the first line and d1 and d2 are the endpoints of the second line.

public static Point2D.Float calculateInterceptionPoint(Point2D.Float s1, Point2D.Float s2, Point2D.Float d1, Point2D.Float d2) {

        double a1 = s2.y - s1.y;
        double b1 = s1.x - s2.x;
        double c1 = a1 * s1.x + b1 * s1.y;

        double a2 = d2.y - d1.y;
        double b2 = d1.x - d2.x;
        double c2 = a2 * d1.x + b2 * d1.y;

        double delta = a1 * b2 - a2 * b1;
        return new Point2D.Float((float) ((b2 * c1 - b1 * c2) / delta), (float) ((a1 * c2 - a2 * c1) / delta));

    }

public static void main(String[] args) {

    System.out.println(calculateInterceptionPoint(new Point2D.Float(3, 5), new Point2D.Float(0, 2), new Point2D.Float(1, 2), new Point2D.Float(4, 0)));

}
Judicatory answered 3/5, 2020 at 12:21 Comment(2)
Yep! This solution is working! Upvoted! Thanks!Bobodioulasso
For the input parameters you can also use two Line2D.Float (fields: x1, y1, x2, y2).Jollification
G
1

That's what i got. Couldn't find any exeptions which don't work:

public static Point calculateInterceptionPoint(Point s1, Point d1, Point s2, Point d2) {

    double sNumerator = s1.y * d1.x + s2.x * d1.y - s1.x * d1.y - s2.y * d1.x;
    double sDenominator = d2.y * d1.x - d2.x * d1.y;

    // parallel ... 0 or infinite points, or one of the vectors is 0|0
    if (sDenominator == 0) {
        return null;
    }

    double s = sNumerator / sDenominator;

    double t;
    if (d1.x != 0) {
        t = (s2.x + s * d2.x - s1.x) / d1.x;
    } else {
        t = (s2.y + s * d2.y - s1.y) / d1.y;
    }

    Point i1 = new Point(s1.x + t * d1.x, s1.y + t * d1.y);

    return i1;

}

public static void main(String[] args) {
    System.out.println(calculateInterceptionPoint(new Point(3, 5), new Point(0, 2), new Point(1, 2), new Point(4, 0)));
    System.out.println(calculateInterceptionPoint(new Point(3, 5), new Point(0, 2), new Point(1, 2), new Point(0, 2)));
    System.out.println(calculateInterceptionPoint(new Point(0, 0), new Point(0, 2), new Point(0, 0), new Point(2, 0)));
    System.out.println(calculateInterceptionPoint(new Point(0, 0), new Point(0, 2), new Point(0, 0), new Point(0, 2)));
    System.out.println(calculateInterceptionPoint(new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0)));
}
Garner answered 4/5, 2018 at 21:44 Comment(2)
You cannot instantiate a Point object with double coordinations in the constructor; you should create one empty object and then populate it. The constructor only accepts int values.Millham
In any case the implementation seems wrong to me. Math doesn't look right.Fivespot
S
0

The simplest solution:

import java.util.Scanner;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    System.out.println(Arrays.toString(calculateIntersection()));
  }

  public static double[] calculateIntersection() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("The program calculates the point of intersection of two lines, given by their equations: ax + b. Please introduce the a and b coefficients of both lines:");

    double m1 = scanner.nextInt();
    double b1 = scanner.nextInt();
    double m2 = scanner.nextInt();
    double b2 = scanner.nextInt();

    if ((m2 - m1) == 0) {
      throw new ArithmeticException("The lines don't have intersection, because they're parallel.");
    }

    // Intersection [x,y] formula
    double crossX = (b1 - b2) / (m2 - m1);
    double crossY = (m1 * crossX + b1);

    double[] array = new double[] {crossX,crossY};
    return array;
  }
}
Student answered 29/4, 2022 at 15:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.