Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let C be the center of the bounding box and P the other endpoint of the segment. Let V = (Vx, Vy) = P - C be the vector pointing from C to P.</p> <p>Let the bounding box have height = 2 * H and width = 2 * W. W and H are half of the normal height and width, which will make the computations below simpler.</p> <p>Here is the idea: Consider the case where V is in the first quadrant, so Vx > 0 and Vy > 0. The segment then intersects the top side or right side. We can tell the which one by comparing the slope of V and the slope of the segment from the center of the box to the top right corner. If the slope is larger, then it intersects the top and otherwise it intersects the side.</p> <p>So, if V is in the first quadrant, and if Vy / Vx > H / W the segment intersects the top. If Vy / Vx &lt; H / W, it intersects the side. If they are equal, it intersects the corner.</p> <p>Once you know which side it intersects, you can use similar triangles to figure out the point of intersection. If it intersects the top at the point I = (Ix, H), then Vx / Vy = Ix / H or Ix = H * Vx / Vy. If it intersects the side at the point I = (W, Iy) then Vy / Vx = Iy / W or Iy = W * Vy / Vx.</p> <p>The same methods will work in the other quadrants; you just need to keep track of the signs of Vx and Vy. You can make your life a bit easier by asserting that the return value satisfies sign(Ix) == sign(Vx) and sign(Iy) == sign(Vy).</p> <p>Note also that, if you are careful with signs, you can avoid division: Vy / Vx > H / W is equivalent to Vy * W > Vx * H, assuming that Vx > 0.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload