java 如何检查直线和矩形之间的交点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15514906/
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-10-31 19:53:05  来源:igfitidea点击:

How to check intersection between a line and a rectangle?

javalinecollisionintersectionrectangles

提问by Code Doggo

The title says it all, Ive been searching around and couldnt find anything that was straight and to the point. How would I take a line with points (x1,y1) & (x2, y2) and check its intersection between a rectangle (xR,yR)? I saw in the Line2D package that there were some intersection methods but not sure how to set it all up. Can someone show me a correct way of setting it up to check for an intersection (collision)?

标题说明了一切,我一直在四处寻找,找不到任何直截了当的内容。我如何取一条带点 (x1,y1) & (x2, y2) 的线并检查它在矩形 (xR,yR) 之间的交点?我在 Line2D 包中看到有一些交集方法,但不确定如何设置。有人可以告诉我设置它以检查交叉路口(碰撞)的正确方法吗?

采纳答案by MadProgrammer

Using the available classes from the 2D Graphics API.

使用 2D Graphics API 中的可用类。

Rectangle r1 = new Rectangle(100, 100, 100, 100);
Line2D l1 = new Line2D.Float(0, 200, 200, 0);
System.out.println("l1.intsects(r1) = " + l1.intersects(r1));

What this doesn't tell you, is where...

这并没有告诉你,是哪里...

回答by loopkin

A rectangle is 4 lines. You could compute the intersect between your line and the 4 lines of the rectangle.

一个矩形是 4 条线。您可以计算您的线与矩形的 4 条线之间的交点。

given the equations of two lines, they would intersect when x and y are equal.

给定两条线的方程,当 x 和 y 相等时,它们会相交。

y = m1x + b1 y = m2x + b2

y = m1x + b1 y = m2x + b2

solving the equation you should get:

解方程你应该得到:

x = b2 - b1 / (m1 - m2);

x = b2 - b1 / (m1 - m2);

Note that if m1 == m2, the lines are parallel and will never intersect, watch out for the divided by 0 in this case.

请注意,如果 m1 == m2,则这些线是平行的并且永远不会相交,在这种情况下要注意除以 0。

Then, since you are dealing with segments ratter than infinite lines, check if the intersect falls off within your segments (check if both X and Y are within each segment's boundaries).

然后,由于您处理的线段比无限线更粗,请检查线段内的相交是否脱落(检查 X 和 Y 是否都在每个线段的边界内)。

回答by user2875504

Returns null if lines do not intersect. Modified some c code from another response to similar question to make it Java. Haven't bothered to look into how/why it works, but does the job I needed it to.

如果线不相交,则返回 null。从另一个对类似问题的回答中修改了一些 c 代码,使其成为 Java。没有费心去研究它是如何/为什么工作的,但是我需要它做的工作。

static Point get_line_intersection(Line2D.Double pLine1, Line2D.Double pLine2)
{
    Point
        result = null;

    double
        s1_x = pLine1.x2 - pLine1.x1,
        s1_y = pLine1.y2 - pLine1.y1,

        s2_x = pLine2.x2 - pLine2.x1,
        s2_y = pLine2.y2 - pLine2.y1,

        s = (-s1_y * (pLine1.x1 - pLine2.x1) + s1_x * (pLine1.y1 - pLine2.y1)) / (-s2_x * s1_y + s1_x * s2_y),
        t = ( s2_x * (pLine1.y1 - pLine2.y1) - s2_y * (pLine1.x1 - pLine2.x1)) / (-s2_x * s1_y + s1_x * s2_y);

    if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
    {
        // Collision detected
        result = new Point(
            (int) (pLine1.x1 + (t * s1_x)),
            (int) (pLine1.y1 + (t * s1_y)));
    }   // end if

    return result;
}