繁体   English   中英

检测线与矩形的交点

[英]Detect intersections of lines with rectangles

在此处输入图像描述

我想检测随机线与随机矩形的交点。 我的线 class 得到 4 个坐标并用它们组成一条线。 我的intersectionWith function 得到2条线并返回交点,如果没有点则返回null。

我有 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;
}

而这个 function 将给定线的所有交点保存到一个列表中。 位于矩形。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]);
}

}

我不知道为什么,但它会将随机点识别为交点,如图所示。

因为,如果你看看你的“随机”点在哪里,如果你将边延伸到无穷大,它们实际上就是非相交矩形的边所在的位置。 您需要实际检查是否与矩形边的线段相交。

暂无
暂无

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

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