Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat...

29
Rodrigo Silveira Computational Geometry Facultat d’Inform` atica de Barcelona Universitat Polit` ecnica de Catalunya Basic tools: orientation tests

Transcript of Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat...

Page 1: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

Rodrigo Silveira

Computational Geometry

Facultat d’Informatica de Barcelona

Universitat Politecnica de Catalunya

Basic tools:orientation tests

Page 2: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

Turning right or turning left?

Computational Geometry, Facultat d’Informatica de Barcelona, UPC

Consider a sequence of 3 points: p,q, r

pq

r

Page 3: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 4: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 5: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 6: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 7: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 8: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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?

Page 9: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 10: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 11: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

∣∣∣∣∣∣∣∣

Page 12: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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?

Page 13: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 14: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 15: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 16: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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 `

Page 17: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 18: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 19: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 20: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 21: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 22: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 23: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 24: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 25: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 26: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 27: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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!

Page 28: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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

Page 29: Universitat Polit ecnica de Catalunya Facultat d’Inform ... · Computational Geometry, Facultat d’Inform atica de Barcelona, UPC Things we can do with the orientation test Test

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