How to know if a vector is going to collide a square

53 Views Asked by At

Let's say on a $2D$ space: I have $2$ points $A$ ($x, y$) and a point $B$ ($y, x$). I know that the vector is going from $A$ to $B$. I also have a square (I have height/width of the square and central point of it $x,y$)

How to mathematically know if the vector is going to collide the square ?

Example: Vector will not collide

Example: Vector will collide

1

There are 1 best solutions below

3
On

Use the implicit form of the supporting line of the segment, let

$$(y_B-y_A)(x-x_A)-(x_B-x_A)(y-y_A)=0.$$

If when you plug the coordinates of the four corners in the LHS, you get four times the same sign, then the supporting line does not cross the square.


If you also want to know if the intersection occurs ahead of or behind $B$, then it may be easier to perform a change of coordinates such that $B$ moves to the origin and $A$ on the (negative) $x$ axis.

Then it is an easy matter to detect the crossing (change of sign among the four $y$s) and to locate the intersections of the sides with the axis.