检测线与矩形的交点
Detect intersections of lines with rectangles
我想检测随机线与随机矩形的交点。
我的线 class 得到 4 个坐标并用它们做一条线。
我的 intersectionWith 函数得到 2 条线和 returns 交点,如果没有点,它 returns null.
我有两个功能:
找到最近的交点并 returns 它(我认为这与这里无关):
位于 Line.java
public Point closestIntersectionToStartOfLine(Rectangle rect){
Line[] rectLines = new Line[4];
Point[] IntersectionPoints = new Point[4];
double minDistance=0;
//up
rectLines[0] = new Line(rect.getUpperLeft().getX(), rect.getUpperLeft().getY(), rect.getWidth()+rect.getUpperLeft().getX(), rect.getUpperLeft().getY());
//right
rectLines[1] = new Line(rect.getWidth()+rect.getUpperLeft().getX(), rect.getUpperLeft().getY(), rect.getUpperLeft().getX()+rect.getWidth(), rect.getHeight()+rect.getUpperLeft().getY());
//down
rectLines[2] = new Line(rect.getUpperLeft().getX()+rect.getWidth(), rect.getHeight()+rect.getUpperLeft().getY(), rect.getUpperLeft().getX(), rect.getUpperLeft().getY()+rect.getHeight());
//left
rectLines[3] = new Line(rect.getUpperLeft().getX(), rect.getUpperLeft().getY()+rect.getHeight(), rect.getUpperLeft().getX(), rect.getUpperLeft().getY());
for (int i=0; i<4; i++) {
if (intersectionWith(rectLines[i]) != null)
IntersectionPoints[i] = intersectionWith(rectLines[i]);
}
for (int i=0; i<IntersectionPoints.length; i++) {
if (i == 0)
minDistance = rect.getUpperLeft().distance(IntersectionPoints[i]);
else {
if (rect.getUpperLeft().distance(IntersectionPoints[i]) < minDistance)
Point.closestIntersectionToStartOfLine = IntersectionPoints[i];
}
}
return Point.closestIntersectionToStartOfLine;
}
并且此函数将给定线的所有交点保存到列表中。
位于 Rectangle.java
// Return a (possibly empty) List of intersection points
// with the specified line.
public java.util.List<Point> intersectionPoints(Line line) {
List<Point> intersectionPointsList = new ArrayList<Point>();
Line[] rectangleSides = new Line[4];
Point[] corners = new Point[4];
rectangleCorners(corners);
recatngleSides(rectangleSides, corners);
for (int i = 0; i < 4; i++) {
Point p = line.intersectionWith(rectangleSides[i]);
if (p != null && !intersectionPointsList.contains(p)) {
intersectionPointsList.add(p);
}
}
return intersectionPointsList;
}
public void rectangleCorners(Point[] corners) {
double width = this.getWidth();
double height = this.getHeight();
double x = this.upperLeft.getX();
double y = this.upperLeft.getY();
//upper left
corners[0] = this.getUpperLeft();
//upper right
corners[1] = new Point(x + width, y);
//down right
corners[2] = new Point(x + width, y + height);
//down left
corners[3] = new Point(x, y + height);
}
/**
* recatngleSides.
* Saves the lines in the 4 rectangle edges.
*
* @param sides - an empty array of lines
* @param corners - an array of edge points.
*/
public void recatngleSides(Line[] sides, Point[] corners) {
// up
sides[0] = new Line(corners[0], corners[1]);
// right
sides[1] = new Line(corners[1], corners[2]);
// down
sides[2] = new Line(corners[2], corners[3]);
// left
sides[3] = new Line(corners[3], corners[0]);
}
}
我不知道为什么,但它会将随机点识别为交点,正如您在图片中看到的那样。
因为,如果您查看 'random' 点的位置,如果您将边延伸到无穷远,它们实际上就是非相交矩形的边所在的位置。您需要实际检查与矩形边的线段是否有交点。
我想检测随机线与随机矩形的交点。 我的线 class 得到 4 个坐标并用它们做一条线。 我的 intersectionWith 函数得到 2 条线和 returns 交点,如果没有点,它 returns null.
我有两个功能: 找到最近的交点并 returns 它(我认为这与这里无关): 位于 Line.java
public Point closestIntersectionToStartOfLine(Rectangle rect){
Line[] rectLines = new Line[4];
Point[] IntersectionPoints = new Point[4];
double minDistance=0;
//up
rectLines[0] = new Line(rect.getUpperLeft().getX(), rect.getUpperLeft().getY(), rect.getWidth()+rect.getUpperLeft().getX(), rect.getUpperLeft().getY());
//right
rectLines[1] = new Line(rect.getWidth()+rect.getUpperLeft().getX(), rect.getUpperLeft().getY(), rect.getUpperLeft().getX()+rect.getWidth(), rect.getHeight()+rect.getUpperLeft().getY());
//down
rectLines[2] = new Line(rect.getUpperLeft().getX()+rect.getWidth(), rect.getHeight()+rect.getUpperLeft().getY(), rect.getUpperLeft().getX(), rect.getUpperLeft().getY()+rect.getHeight());
//left
rectLines[3] = new Line(rect.getUpperLeft().getX(), rect.getUpperLeft().getY()+rect.getHeight(), rect.getUpperLeft().getX(), rect.getUpperLeft().getY());
for (int i=0; i<4; i++) {
if (intersectionWith(rectLines[i]) != null)
IntersectionPoints[i] = intersectionWith(rectLines[i]);
}
for (int i=0; i<IntersectionPoints.length; i++) {
if (i == 0)
minDistance = rect.getUpperLeft().distance(IntersectionPoints[i]);
else {
if (rect.getUpperLeft().distance(IntersectionPoints[i]) < minDistance)
Point.closestIntersectionToStartOfLine = IntersectionPoints[i];
}
}
return Point.closestIntersectionToStartOfLine;
}
并且此函数将给定线的所有交点保存到列表中。 位于 Rectangle.java
// Return a (possibly empty) List of intersection points
// with the specified line.
public java.util.List<Point> intersectionPoints(Line line) {
List<Point> intersectionPointsList = new ArrayList<Point>();
Line[] rectangleSides = new Line[4];
Point[] corners = new Point[4];
rectangleCorners(corners);
recatngleSides(rectangleSides, corners);
for (int i = 0; i < 4; i++) {
Point p = line.intersectionWith(rectangleSides[i]);
if (p != null && !intersectionPointsList.contains(p)) {
intersectionPointsList.add(p);
}
}
return intersectionPointsList;
}
public void rectangleCorners(Point[] corners) {
double width = this.getWidth();
double height = this.getHeight();
double x = this.upperLeft.getX();
double y = this.upperLeft.getY();
//upper left
corners[0] = this.getUpperLeft();
//upper right
corners[1] = new Point(x + width, y);
//down right
corners[2] = new Point(x + width, y + height);
//down left
corners[3] = new Point(x, y + height);
}
/**
* recatngleSides.
* Saves the lines in the 4 rectangle edges.
*
* @param sides - an empty array of lines
* @param corners - an array of edge points.
*/
public void recatngleSides(Line[] sides, Point[] corners) {
// up
sides[0] = new Line(corners[0], corners[1]);
// right
sides[1] = new Line(corners[1], corners[2]);
// down
sides[2] = new Line(corners[2], corners[3]);
// left
sides[3] = new Line(corners[3], corners[0]);
}
}
我不知道为什么,但它会将随机点识别为交点,正如您在图片中看到的那样。
因为,如果您查看 'random' 点的位置,如果您将边延伸到无穷远,它们实际上就是非相交矩形的边所在的位置。您需要实际检查与矩形边的线段是否有交点。