$2D$ Line Segment - Triangle Intersection

656 Views Asked by At

I've seen similar questions but could not solve my problem with those. My question is how to detect an intersection of a line segment and a triangle on a 2D coordinate system? I don't need the point of intersection, I just quickly need to decide whether the segment cuts into the triangle or not.

The Line segment is represented by two points, S (Sx,Sy). The triangle is represented by 3 points, T (T1x,T1y, T2x,T2y, T3x,T3y)

Your help is appreciated.

1

There are 1 best solutions below

4
On

All right, maybe we can go for an algorithm!

given the two end of the segment $A=(a_x,a_y),B=(b_x,b_y)$ and the three vertices of the triangle $C=(c_x,c_y),D=(d_x,d_y),E=(e_x,e_y)$

Intersection (A,B,C,D,E)

(1) if A is in the triangle and B is not (or vice versa) then return True

(2) if (Intersection(A,(A+B)/2,C,D,E) or Intersection((A+B)/2,B,C,D,E))
then return True else return False

Here is a method to check whether a point is in a triangle using cross product http://www.blackpawn.com/texts/pointinpoly/

bad one (thanks Daryl): To check if an end of the segment, say A, is in the triangle we can simply check it's coordinates against the triangle vertices ones:

For A to be in the triangle we should have $min \{c_x,d_x,e_x\} \leq a_x \leq max \{c_x,d_x,e_x\}$ and $min \{c_y,d_y,e_y\} \leq a_y \leq max \{c_y,d_y,e_y\}$