Let's start from finite polygons.
To intersect polygon a line must intersect one of its edges. Intersection between line and an edge is possible only if two points lie on different sides from the line.
That can be easily checked with sign(cross_product(Ep-Lp,Ld))
for two points of the edge. Ep
- edge point, Lp
- some point on the line, Ld
- direction vector of the line, cross_product(A,B)=Ax*By-Ay*Bx
.
To deal with infinite polygons we may introduce "infinite points". If we have a half infinite edge with point E1
and direction Ed
, its "second point" is something like E1+infinity*Ed
, where infinity
is "big enough number".
For "infinite points" the check will be slightly different:
cross_product(Ep-Lp,Ld)=
=cross_product(E1+infinity*Ed-Lp,Ld)=
=cross_product(E1-Lp+infinity*Ed,Ld)=
=cross_product(E1-Lp,Ld)+cross_product(infinity*Ed,Ld)=
=cross_product(E1-Lp,Ld)+infinity*cross_product(Ed,Ld)
If cross_product(Ed,Ld)
is zero (the line is parallel to the edge), the sign will be determined by the first component. Otherwise the second component will dominate and determine the sign.