Basic tools: orientation tests Rodrigo Silveira Computational Geometry Facultat d’Informàtica de Barcelona Universitat Politècnica de Catalunya Turning right or turning left? Consider a sequence of 3 points: p, q, r r p q Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Turning right or turning left? Consider a sequence of 3 points: p, q, r r p q Basic question: do they make a right turn? A left turn? Or perhaps they are colinear? Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Turning right or turning left? Consider a sequence of 3 points: p, q, r r p q Basic question: do they make a right turn? A left turn? Or perhaps they are colinear? Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Turning right or turning left? Consider a sequence of 3 points: p, q, r r p q Basic question: do they make a right turn? A left turn? Or perhaps they are colinear? Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Turning right or turning left? Consider a sequence of 3 points: p, q, r r p q Basic question: do they make a right turn? A left turn? Or perhaps they are colinear? This is a fundamental basic operation in geometric algorithms. Requires a solution that is • Efficient • Numerically robust Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Turning right or turning left? Consider a sequence of 3 points: p, q, r r p q Basic question: do they make a right turn? A left turn? Or perhaps they are colinear? This is a fundamental basic operation in geometric algorithms. Requires a solution that is • Efficient • Numerically robust Solution: signed area of triangle 4pqr Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Signed area of triangle Signed area of 4pqr r The signed area of 4pqr tells us the area of the triangle, and the sign gives the orientation (left-turning or right-turning) p q • Can the area be zero? Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Signed area of triangle Signed area of 4pqr r The signed area of 4pqr tells us the area of the triangle, and the sign gives the orientation (left-turning or right-turning) p q • Can the area be zero? Computing A(4pqr) • A(4pqr) is twice the area of the parallelogram defined by vectors pq and w • Recall: in 2-dimensions, the area of the parallelogram defined by two vectors is the determinant of a 2 × 2 matrix whose columns are the two vectors Computational Geometry, Facultat d’Informàtica de Barcelona, UPC w =r−p r p pq = q − p q Signed area of triangle Signed area of 4pqr 1 pq x A(4pqr) = 2 pq y wx wy w =r−p r p pq = q − p Computational Geometry, Facultat d’Informàtica de Barcelona, UPC q Signed area of triangle Signed area of 4pqr 1 A(4pqr) = 2 1 = 2 1 = 2 1 = 2 pq x wx pq y wy (q − p)x (q − p)y qx − px qy − py px qx py qy 1 1 w =r−p (r − p)x (r − p)y rx − px ry − py rx ry 1 Computational Geometry, Facultat d’Informàtica de Barcelona, UPC r p pq = q − p q Signed area of triangle Signed area of 4pqr 1 A(4pqr) = 2 1 = 2 1 = 2 1 = 2 pq x wx pq y wy (q − p)x (q − p)y qx − px qy − py px qx py qy 1 1 w =r−p (r − p)x (r − p)y rx − px ry − py rx ry 1 r p pq = q − p Does it turn right or left? Fact: (check it yourself!) A(4pqr) > 0 if and only if p, q, r are in counter-clockwise order Computational Geometry, Facultat d’Informàtica de Barcelona, UPC q More things you can do with this test Point with respect to a line LEFT r q ` RIGHT Is point r to the right or left of the oriented line `? p Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Point with respect to a line LEFT r q ` RIGHT Is point r to the right or left of the oriented line `? p Equivalent question: do p, q, r turn right or left? Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Point with respect to a line LEFT r q ` RIGHT Is point r to the right or left of the oriented line `? p Equivalent question: do p, q, r turn right or left? Line-segment intersection test r ` p Does segment rs intersect line `? q s LEFT RIGHT Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Point with respect to a line LEFT r q ` RIGHT Is point r to the right or left of the oriented line `? p Equivalent question: do p, q, r turn right or left? Line-segment intersection test r ` p Does segment rs intersect line `? q s rs intersects line ` ↔ r and s are on opposite sides of ` LEFT RIGHT ↔ (r is to the left and s is to the right of `) OR (r is to the right and s is to the left of `) Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Halfline-segment intersection LEFT RIGHT r h q Does segment rs intersect halfline h? p s Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Halfline-segment intersection LEFT RIGHT r h q Does segment rs intersect halfline h? p Equivalent question(s): s • r and s are on opposite sides of h (so we can assume r is to the left of h) Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Halfline-segment intersection LEFT RIGHT r h q Does segment rs intersect halfline h? p Equivalent question(s): • r and s are on opposite sides of h (so we can assume r is to the left of h) s r p h q LEFT RIGHT s Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Halfline-segment intersection LEFT RIGHT r h q Does segment rs intersect halfline h? p Equivalent question(s): • r and s are on opposite sides of h (so we can assume r is to the left of h) s r p LEFT RIGHT s h q r h p RIGHT q LEFT s Computational Geometry, Facultat d’Informàtica de Barcelona, UPC r h p LEFT s RIGHT q More things you can do with this test Halfline-segment intersection LEFT RIGHT r h q Does segment rs intersect halfline h? p Equivalent question(s): • r and s are on opposite sides of h (so we can assume r is to the left of h) • r, s, p makes a right turn s r p LEFT RIGHT s h q r h p RIGHT q LEFT s Computational Geometry, Facultat d’Informàtica de Barcelona, UPC r h p LEFT s RIGHT q More things you can do with this test Segment-segment intersection Does segment pq intersect segment rs? r q p s Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Segment-segment intersection Does segment pq intersect segment rs? r q p r q s p s Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Segment-segment intersection Does segment pq intersect segment rs? r q p r q s p s Equivalent question(s): • r and s are on opposite “sides” of pq Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Segment-segment intersection Does segment pq intersect segment rs? r q p r q s p s Equivalent question(s): • r and s are on opposite “sides” of pq Computational Geometry, Facultat d’Informàtica de Barcelona, UPC More things you can do with this test Segment-segment intersection Does segment pq intersect segment rs? r q p r q s p s Equivalent question(s): • r and s are on opposite “sides” of pq • p and q are on opposite “sides” of rs Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Summary Things we can do with the orientation test • Test if p, q, r make a right turn • Test if a point is to the left of a line • Test if a segment and a line intersect • Test if a segment and a halfline intersect • Test if two segments intersect • Test if a point is inside a triangle All by evaluating the sign of a couple of determinants! Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Summary Things we can do with the orientation test • Test if p, q, r make a right turn • Test if a point is to the left of a line • Test if a segment and a line intersect • Test if a segment and a halfline intersect • Test if two segments intersect • Test if a point is inside a triangle All by evaluating the sign of a couple of determinants! Another useful test: point in circle • See notes on course webpage Computational Geometry, Facultat d’Informàtica de Barcelona, UPC Summary Things we can do with the orientation test • Test if p, q, r make a right turn • Test if a point is to the left of a line • Test if a segment and a line intersect • Test if a segment and a halfline intersect • Test if two segments intersect • Test if a point is inside a triangle All by evaluating the sign of a couple of determinants! Another useful test: point in circle • See notes on course webpage Questions? Computational Geometry, Facultat d’Informàtica de Barcelona, UPC