I have two line segments $[(x_1, y_1),(x_2, y_2)] $ and $[(x_3, y_3),(x_4, y_4)] $ and I want to know if they intersect.
My current algorithm tries the following:
- the line $[(x_1, y_1),(x_2, y_2)] $ is determined by $\boxed{f(x,y) = \frac{y - y_1}{x - x_1} - \frac{y_2 - y_1}{x_2 - x_1} = 0}$.
- I reasoned that if $y = mx+b$ is my line, I want a point such that $y > mx+b$ and one such that $y < mx+b$.
- I want to check if that $f(x,y)$ has opposite signs if I set it on $(x_3,y_3)$ and $(x_4, y_4)$. So the criterion I check right now is: $$ \frac{f(x_3,y_3)}{|f(x_3,y_3)|}\; \frac{f(x_4,y_4)}{|f(x_4,y_4)|} = -1 $$ Unfortunately in my visualization, I still see line segments intersecting.
Is there something wrong with my algorithm? What is a correct way to check that two line segments intersect?