The rectangle has 4 lines. You can calculate the intersection between your line and the four lines of the rectangle.
given the equations of two lines, they intersect when x and y are equal.
y = m1x + b1 y = m2x + b2
the solution to the equation you should get:
x = b2 - b1 / (m1 - m2);
Note that if m1 == m2, the lines are parallel and never intersect, be aware of the division by 0. In this case.
Then, since you are dealing with segments exceeding infinite lines, check if the intersection does not intersect inside your segments (check if both X and Y correspond to the boundaries of each segment).
loopkin
source share