简体   繁体   English

检测线与矩形的交点

[英]Detect intersections of lines with rectangles

在此处输入图像描述

I want to detect intersection points of random lines with random rectangels.我想检测随机线与随机矩形的交点。 My Line class get 4 coordinates and makes a line out of them.我的线 class 得到 4 个坐标并用它们组成一条线。 My intersectionWith function gets 2 lines and returns the intersection point, and if there is no point, it returns null.我的intersectionWith function 得到2条线并返回交点,如果没有点则返回null。

I have 2 functions: One finds the closest intersection point and returns it (I think it's irrelevant here): Located in Line.java我有 2 个功能:一个找到最近的交点并返回它(我认为在这里无关紧要):位于 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;
}

And this function keep ALL intersection points of a given line into a list.而这个 function 将给定线的所有交点保存到一个列表中。 Located in Rectangle.java位于矩形。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]);
}

} }

I don't know why but it recognises random points as intersection points as you can see in the picture.我不知道为什么,但它会将随机点识别为交点,如图所示。

Because, if you look where your 'random' points are, they are actually where the sides of non-intersecting rectangles would be if you extend the sides to infinity.因为,如果你看看你的“随机”点在哪里,如果你将边延伸到无穷大,它们实际上就是非相交矩形的边所在的位置。 You need to actually check there is an intersection with the line segment that is the rectangle side.您需要实际检查是否与矩形边的线段相交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM