0
$\begingroup$

I have input polygon, lets say I have x,y,width,height of rectangle on 2-d plane. I want to know does this ploygon intersects with a line ? What math I equations to use? I want to know, Red Rectangle intersects with blue line?

Solutions for Rectangle

Check each edge intersects with line, may be simple and basic solution. Is there any other way?

enter image description here

Pls add tags for this questions.

$\endgroup$
4
  • 1
    $\begingroup$ Can you explain what the x,y,w,h mean? I'm assuming x and y are describing a point, and the w and h mean width and height. Is this correct? Also, are we to assume that the rectangle sides are parallel to the x and y axes? $\endgroup$
    – Aiden Chow
    Aug 28, 2020 at 18:49
  • $\begingroup$ Is this a strictly two dimensional problem? $\endgroup$
    – copper.hat
    Aug 28, 2020 at 19:18
  • $\begingroup$ @AidenChow - Yes, and it may not be parallel to x and y plane. $\endgroup$
    – ajayramesh
    Aug 29, 2020 at 1:21
  • $\begingroup$ yes only 2-d problem $\endgroup$
    – ajayramesh
    Aug 29, 2020 at 1:21

2 Answers 2

1
$\begingroup$

Here is one way of doing it in $\mathbb{R}^2$:

Suppose the line has the form $x_0 + t d \in \mathbb{R}^2$ for $t \in \mathbb{R}$. Let $p = (-d_2,d_1)$ ($p$ for 'perpendicular').

Given a point $x$, project $x$ onto the line $t p$, $t \in \mathbb{R}$ and compute the corresponding $t$, that is $t(x) = {p^T x \over \|p\|}$.

Now compute $t(c_k)$ for each corner $c_k$ and then we see that the line intersects the rectangle iff $\min t(c_k) \le t(x_0) $ and $\max t(c_k) \ge t(x_0) $.

(There is no need to divide by $\|p\|^2$ of course when doing the actual computation.)

$\endgroup$
0
$\begingroup$

I am merely stating the obvious:

If the edges of your rectangle are parallel to the $x$ and $y$ axis (as depicted in your graph), you can simplify the problem as follows. Suppose your line is of the form $y(x)=ax+b$.

If $a>0$, then it suffices to check whether $y$ intersects the left or lower edge of your rectangle.

If $a=0$, check whether it intersects the left edge.

If $a<0$, check whether it intersects the left or upper edge.

$\endgroup$
3
  • $\begingroup$ The OP clarified that the sides might not be parallel to the x and y axes, so this unfortunately invalidates your answer. $\endgroup$
    – Aiden Chow
    Aug 29, 2020 at 2:07
  • $\begingroup$ @AidenChow By transforming the coordinate system accordingly (eg rotation), so that the edges of the rectangle are again parallel to the axes, you can apply the above mentioned simplification just as well. Note that this transformation will, in general, also lead to new a and b values. $\endgroup$
    – mr.math
    Aug 29, 2020 at 6:22
  • $\begingroup$ Maybe you can edit your answer to clarify that. The OP might not know how to do such a transformation mathematically. $\endgroup$
    – Aiden Chow
    Aug 29, 2020 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .