java Java找到两条线的交点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31506740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:43:17  来源:igfitidea点击:

Java find intersection of two lines

javamathgeometrylinepoint

提问by HyperNeutrino

In Java, I have a class Linethat has two variables : mand b, such that the line follows the formula mx + b. I have two such lines. How am I to find the xand ycoordinates of the intersection of the two lines? (Assuming the slopes are different)

在 Java 中,我有一个Line包含两个变量的类:mb,因此该行遵循公式mx + b。我有两条这样的线。我该怎样找到xy两线的交点坐标?(假设斜率不同)

Here is class Line:

这是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;
    }
}

回答by Lourenco

Lets assume you have these 2 functions:

让我们假设你有这两个功能:

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

To find the intersection point of the x-axiswe do:

要找到x-axis我们做的交点:

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 xfor its value (b2-b1) / (m1-m2).

要找到 y,您可以使用函数表达式并替换x其值(b2-b1) / (m1-m2)

So:

所以:

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

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

你有(this.b - line.b),改为(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);
}

回答by wutzebaer

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)));
}

回答by Mikolaj Gierak

The proposed solution by @wutzebaerseems 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.

@wutzebaer提出的解决方案似乎不起作用,而是尝试以下解决方案(基于以下示例的代码:https: //rosettacode.org/wiki/Find_the_intersection_of_two_lines#Java)。s1 和 s2 是第一条线的端点,d1 和 d2 是第二条线的端点。

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)));

}