Search code examples
mathvectorgeometrytrigonometrycomputational-geometry

Intersection point of a vector or its extension with surrounding rectangle


I would like to find the intersection point of a vector or its extension with the surrounding rectangle, that is, in the image (1) and (2), given (x1, y1), (x2, y2), (a1, a2), (b1, b2), we would like to obtain the point (c1, c2).

enter image description here

I have found the article Find collision point between vector and fencing rectangle but, since the positive y-axis is downward in python/windows, I could not manage the equations and parameters to acheive the correct result. The two following links are also related.

intersection between a line and square

Get intersection point of rectangle and line.

But they do not include the extension of the vector/line segment. How should the equations change to obtain the correct result?


Solution

  • A point on the ray is given by the coordinates (a1 + t (b1 - a1), a2 + t (b2 - a2)).

    The intersections are obtained by solving the four equations

    a1 + t (b1 - a1) = x1
    a1 + t (b1 - a1) = x2
    a2 + t (b2 - a2) = y1
    a2 + t (b2 - a2) = y2
    

    for t and taking the smallest positive solution. If some of the coefficients of t is zero, just ignore the corresponding equation.

    The orientations of the axis play no role.