有没有办法检查两条线是否相交,交点是什么?
Is there a way to check if 2 lines are intersecting, and what is the intersection point?
在@Alex 的帮助下,我成功创建了以下代码:
// Returns true if the lines intersect, false otherwise
public boolean isIntersecting(Line other) {
if (equals(other)){
return false;
}
double x11 = this.start.getX();
double y11 = this.start.getY();
double x12 = this.end.getX();
double y12 = this.end.getY();
double x21 = other.start.getX();
double y21 = other.start.getY();
double x22 = other.end.getX();
double y22 = other.end.getY();
// special handling may be needed when x11 == x12
double m1 = (y12 - y11) / (x12 - x11);
double b1 = (x11 * y12 - x12 * y11) / (x12 - x11);
// special handling may be needed when x21 == x22
double m2 = (y22 - y21) / (x22 - x21);
double b2 = (x21 * y22 - x22 * y21) / (x22 - x21);
if ((long) m1 == (long) m2) {
if (this.start == other.start)
return true;
if (other.start == other.end)
return true;
if (other.start == this.end)
return true;
if (other.start == this.start)
return true;
return false;
}
double x = (b2 - b1)/(m1 - m2);
double y = m1 * x + b1; // or m2 * x + b2
if (x>x11 && x<x12 && y<y11 && y>y12 && x>x21 && x<x22 && y<y21 && y>y22) {
Point.intersection = new Point(x, y);
return true;
}
return false;
}
// Returns the intersection point if the lines intersect,
// and null otherwise.
public Point intersectionWith(Line other) {
if (isIntersecting(other)) {
return Point.intersection;
}
return null;
}
问题是,我真的不知道这些线是否只有一个交点或多个。我不知道我还需要做哪些事情以及要检查哪些条件以验证它们只有一个交集。
我不得不说,线条不必是无限的。这意味着一条线可以在第二条线结束的地方开始,并且它们也将具有相同的斜率("m")并且只有一个交点。
另外,我想学习如何正确发送交点,因为我觉得我做错了,错误地发送到第二个函数。
此方法找到两条直线的交点(所有情况)和 returns 交点。
如果输入线平行或重合,null
返回点。
public static Point findIntersection(Line line1, Line line2) {
double x11 = line1.getX1();
double y11 = line1.getY1();
double x12 = line1.getX2();
double y12 = line1.getY2();
double x21 = line2.getX1();
double y21 = line2.getY1();
double x22 = line2.getX2();
double y22 = line2.getY2();
if (x11 == x12 && x21 == x22) { // both lines are constant x
if (x11 == x21) {
System.out.println("Lines coincide");
} else {
System.out.println("Lines are parallel to each other and axis 0Y");
}
// no intersection point
return null;
} else if (x11 == x12 || x21 == x22) { // either line is constant x
double x;
double m;
double b;
if (x11 == x12) { // first line is constant x, second is sloped
x = x11;
m = (y22 - y21) / (x22 - x21);
b = (x22 * y21 - x21 * y22) / (x22 - x21);
} else { // second line is constant x, first is sloped
x = x21;
m = (y12 - y11) / (x12 - x11);
b = (x12 * y11 - x11 * y12) / (x12 - x11);
}
double y = m * x + b;
System.out.printf("Lines intersect in (%.2f, %.2f)%n", x, y);
return new Point(x, y);
} else { // both lines are sloped
double m1 = (y12 - y11) / (x12 - x11);
double b1 = (x12 * y11 - x11 * y12) / (x12 - x11);
double m2 = (y22 - y21) / (x22 - x21);
double b2 = (x22 * y21 - x21 * y22) / (x22 - x21);
if (m1 == m2) {
if (b1 == b2) {
System.out.println("Sloped lines coincide");
} else {
System.out.println("Lines are parallel with slope " + m1);
}
// no intersection point
return null;
}
// calculating intersection coordinates
double x = (b2 - b1)/(m1 - m2);
double y = m1 * x + b1; // or m2 * x + b2
System.out.printf("Lines intersect in (%.2f, %.2f)%n", x, y);
return new Point(x, y);
}
}
在@Alex 的帮助下,我成功创建了以下代码:
// Returns true if the lines intersect, false otherwise
public boolean isIntersecting(Line other) {
if (equals(other)){
return false;
}
double x11 = this.start.getX();
double y11 = this.start.getY();
double x12 = this.end.getX();
double y12 = this.end.getY();
double x21 = other.start.getX();
double y21 = other.start.getY();
double x22 = other.end.getX();
double y22 = other.end.getY();
// special handling may be needed when x11 == x12
double m1 = (y12 - y11) / (x12 - x11);
double b1 = (x11 * y12 - x12 * y11) / (x12 - x11);
// special handling may be needed when x21 == x22
double m2 = (y22 - y21) / (x22 - x21);
double b2 = (x21 * y22 - x22 * y21) / (x22 - x21);
if ((long) m1 == (long) m2) {
if (this.start == other.start)
return true;
if (other.start == other.end)
return true;
if (other.start == this.end)
return true;
if (other.start == this.start)
return true;
return false;
}
double x = (b2 - b1)/(m1 - m2);
double y = m1 * x + b1; // or m2 * x + b2
if (x>x11 && x<x12 && y<y11 && y>y12 && x>x21 && x<x22 && y<y21 && y>y22) {
Point.intersection = new Point(x, y);
return true;
}
return false;
}
// Returns the intersection point if the lines intersect,
// and null otherwise.
public Point intersectionWith(Line other) {
if (isIntersecting(other)) {
return Point.intersection;
}
return null;
}
问题是,我真的不知道这些线是否只有一个交点或多个。我不知道我还需要做哪些事情以及要检查哪些条件以验证它们只有一个交集。
我不得不说,线条不必是无限的。这意味着一条线可以在第二条线结束的地方开始,并且它们也将具有相同的斜率("m")并且只有一个交点。
另外,我想学习如何正确发送交点,因为我觉得我做错了,错误地发送到第二个函数。
此方法找到两条直线的交点(所有情况)和 returns 交点。
如果输入线平行或重合,null
返回点。
public static Point findIntersection(Line line1, Line line2) {
double x11 = line1.getX1();
double y11 = line1.getY1();
double x12 = line1.getX2();
double y12 = line1.getY2();
double x21 = line2.getX1();
double y21 = line2.getY1();
double x22 = line2.getX2();
double y22 = line2.getY2();
if (x11 == x12 && x21 == x22) { // both lines are constant x
if (x11 == x21) {
System.out.println("Lines coincide");
} else {
System.out.println("Lines are parallel to each other and axis 0Y");
}
// no intersection point
return null;
} else if (x11 == x12 || x21 == x22) { // either line is constant x
double x;
double m;
double b;
if (x11 == x12) { // first line is constant x, second is sloped
x = x11;
m = (y22 - y21) / (x22 - x21);
b = (x22 * y21 - x21 * y22) / (x22 - x21);
} else { // second line is constant x, first is sloped
x = x21;
m = (y12 - y11) / (x12 - x11);
b = (x12 * y11 - x11 * y12) / (x12 - x11);
}
double y = m * x + b;
System.out.printf("Lines intersect in (%.2f, %.2f)%n", x, y);
return new Point(x, y);
} else { // both lines are sloped
double m1 = (y12 - y11) / (x12 - x11);
double b1 = (x12 * y11 - x11 * y12) / (x12 - x11);
double m2 = (y22 - y21) / (x22 - x21);
double b2 = (x22 * y21 - x21 * y22) / (x22 - x21);
if (m1 == m2) {
if (b1 == b2) {
System.out.println("Sloped lines coincide");
} else {
System.out.println("Lines are parallel with slope " + m1);
}
// no intersection point
return null;
}
// calculating intersection coordinates
double x = (b2 - b1)/(m1 - m2);
double y = m1 * x + b1; // or m2 * x + b2
System.out.printf("Lines intersect in (%.2f, %.2f)%n", x, y);
return new Point(x, y);
}
}