Rodrigo Silveira
Computational Geometry
Facultat d’Informatica de Barcelona
Universitat Politecnica de Catalunya
Basic tools:orientation tests
Turning right or turning left?
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Consider a sequence of 3 points: p,q, r
pq
r
Turning right or turning left?
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Basic question: do they make a right turn?A left turn?Or perhaps they are colinear?
Consider a sequence of 3 points: p,q, r
pq
r
Turning right or turning left?
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Basic question: do they make a right turn?A left turn?Or perhaps they are colinear?
Consider a sequence of 3 points: p,q, r
pq
r
Turning right or turning left?
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Basic question: do they make a right turn?A left turn?Or perhaps they are colinear?
Consider a sequence of 3 points: p,q, r
pq
r
Turning right or turning left?
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Basic question: do they make a right turn?A left turn?Or perhaps they are colinear?
Consider a sequence of 3 points: p,q, r
pq
r
This is a fundamental basic operation in geometric algorithms. Requires a solution that is
• Efficient
• Numerically robust
Turning right or turning left?
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Basic question: do they make a right turn?A left turn?Or perhaps they are colinear?
Consider a sequence of 3 points: p,q, r
pq
r
This is a fundamental basic operation in geometric algorithms. Requires a solution that is
• Efficient
• Numerically robust
Solution: signed area of triangle 4pqr
Signed area of triangle
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
The signed area of 4pqr tells us the area ofthe triangle, and the sign gives the orientation(left-turning or right-turning)
Signed area of 4pqr
pq
r
• Can the area be zero?
Signed area of triangle
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
The signed area of 4pqr tells us the area ofthe triangle, and the sign gives the orientation(left-turning or right-turning)
Signed area of 4pqr
pq
r
• A(4pqr) is twice the area of the parallelogramdefined by vectors pq and w
• Recall: in 2-dimensions, the area of the paral-lelogram defined by two vectors is the deter-minant of a 2 × 2 matrix whose columns arethe two vectors
• Can the area be zero?
Computing A(4pqr)
pq
rw = r − p
pq = q − p
Signed area of triangle
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Signed area of 4pqr
A(4pqr) =1
2
∣∣∣∣∣ pqx wx
pqy wy
∣∣∣∣∣
p
q
rw = r − p
pq = q − p
Signed area of triangle
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Signed area of 4pqr
A(4pqr) =1
2
∣∣∣∣∣ pqx wx
pqy wy
∣∣∣∣∣
p
q
rw = r − p
pq = q − p
=1
2
∣∣∣∣∣ (q − p)x (r − p)x
(q − p)y (r − p)y
∣∣∣∣∣=
1
2
∣∣∣∣∣ qx − px rx − px
qy − py ry − py
∣∣∣∣∣=
1
2
∣∣∣∣∣∣∣∣px qx rx
py qy ry
1 1 1
∣∣∣∣∣∣∣∣
Signed area of triangle
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Signed area of 4pqr
A(4pqr) =1
2
∣∣∣∣∣ pqx wx
pqy wy
∣∣∣∣∣
p
q
rw = r − p
pq = q − p
=1
2
∣∣∣∣∣ (q − p)x (r − p)x
(q − p)y (r − p)y
∣∣∣∣∣=
1
2
∣∣∣∣∣ qx − px rx − px
qy − py ry − py
∣∣∣∣∣=
1
2
∣∣∣∣∣∣∣∣px qx rx
py qy ry
1 1 1
∣∣∣∣∣∣∣∣Fact: (check it yourself!)
A(4pqr) > 0 if and only if p, q, r are in counter-clockwise order
Does it turn right or left?
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Is point r to the right or left of the oriented line `?
Point with respect to a line
r
p
q
`
RIGHT
LEFT
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Is point r to the right or left of the oriented line `?
Point with respect to a line
r
p
q
`
Equivalent question: do p, q, r turn right or left?
RIGHT
LEFT
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Is point r to the right or left of the oriented line `?
Point with respect to a line
r
p
q
`
Line-segment intersection test
Equivalent question: do p, q, r turn right or left?
`
pq
r
sDoes segment rs intersect line `?
RIGHT
LEFT
RIGHT
LEFT
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Is point r to the right or left of the oriented line `?
Point with respect to a line
r
p
q
`
Line-segment intersection test
Equivalent question: do p, q, r turn right or left?
`
pq
r
sDoes segment rs intersect line `?
rs intersects line ` ↔ r and s are on opposite sides of `
RIGHT
LEFT
RIGHT
LEFT
↔ (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 `)
rs intersects line ` ↔ r and s are on opposite sides of `
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Does segment rs intersect halfline h?
Halfline-segment intersection
r
p
h
RIGHT
LEFT
q
s
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Does segment rs intersect halfline h?
Halfline-segment intersection
r
p
h
Equivalent question(s):
• r and s are on opposite sides of h(so we can assume r is to the left of h)
RIGHT
LEFT
q
s
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Does segment rs intersect halfline h?
Halfline-segment intersection
r
p
h
Equivalent question(s):
• r and s are on opposite sides of h(so we can assume r is to the left of h)
RIGHT
LEFT
q
s
hr
s
p
RIGHTLEFT
q
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Does segment rs intersect halfline h?
Halfline-segment intersection
r
p
h
Equivalent question(s):
• r and s are on opposite sides of h(so we can assume r is to the left of h)
RIGHT
LEFT
q
s
hr
s
p
RIGHTLEFT
qp h
q
r
sRIGHTLEFT
ph
q
r
sRIGHTLEFT
More things you can do with this test
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
Does segment rs intersect halfline h?
Halfline-segment intersection
r
p
h
Equivalent question(s):
• r and s are on opposite sides of h(so we can assume r is to the left of h)
RIGHT
LEFT
q
s
• r, s, p makes a right turn
hr
s
p
RIGHTLEFT
qp h
q
r
sRIGHTLEFT
ph
q
r
sRIGHTLEFT
More things you can do with this test
Does segment pq intersect segment rs?
Segment-segment intersection
r
p
q
s
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
More things you can do with this test
Does segment pq intersect segment rs?
Segment-segment intersection
r
p
q
s
r
p
q
s
Computational Geometry, Facultat d’Informatica de Barcelona, UPC
More things you can do with this test
Does segment pq intersect segment rs?
Segment-segment intersection
r
p
Equivalent question(s):
• r and s are on opposite “sides” of pq
q
s
r
p
q
s
Computational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPC
More things you can do with this test
Does segment pq intersect segment rs?
Segment-segment intersection
r
p
Equivalent question(s):
• r and s are on opposite “sides” of pq
q
s
r
p
q
s
Computational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPC
More things you can do with this test
Does segment pq intersect segment rs?
Segment-segment intersection
r
p
Equivalent question(s):
• r and s are on opposite “sides” of pq
q
s
• p and q are on opposite “sides” of rs
r
p
q
s
Computational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPC
Summary
Computational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPC
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!
Summary
Computational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPC
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
Summary
Computational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPCComputational Geometry, Facultat d’Informatica de Barcelona, UPC
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!
Questions?
Another useful test: point in circle• See notes on course webpage