I've checked other answers, but am having trouble figuring out if they fill my needs. I'm trying to turn this into a MySql procedure. I want to check if a line defined by two points at some point intersects a rectangle. I do not need to know where on the line, only if it does at some point.
Asked
Active
Viewed 520 times
2 Answers
0
For each of the rectangle sides, you check if it intersects with the line. See e.g. here for explanation on how to to this. If none intersect, return false. Otherwise, return true only if the intersection point of the line and the side is between the start and end points of the line.

Community
- 1
- 1

nojka_kruva
- 1,454
- 1
- 10
- 23
0
With two points on line you can get two-point form of line equation
(y - y1) * (x2 - x1) = (y2 - y1) * (x - x1)
and transform it to general form
A * x + B * y + C = 0
Then substitute X and Y coordinate of every rectangle corner to this equation and find the signs of expressions
S[i] = Sign(A * x[i] + B * y[i] + C)
If all signs are the same (non-zero), then this line doesn't intersect rectangle (all vertices lie in the same semiplane relative to the line), otherwise line does intersect it.

MBo
- 77,366
- 5
- 53
- 86