a question about polygons devising a computational method for determining if a point lies in a plane...

16
A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Upload: kristian-richard

Post on 18-Dec-2015

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

A question about polygons

Devising a computational method for determining if a point lies in a

plane convex polygon

Page 2: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Problem background

• Recall our ‘globe.cpp’ ray-tracing demo

• It depicted a scene showing two objects

• One of the objects was a square tabletop

• Its edges ran parallel to x- and z-axes

• That fact made it easy to determine if the spot where a ray hit the tabletop fell inside or outside of the square’s boundary edges

• We were lucky!

Page 3: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Alternative geometry?

• What if we wanted to depict our globe as resting on a tabletop that wasn’t a nicely aligned square? For example, could we show a tabletop that was a hexagon?

• The only change is that the edges of this six-sided tabletop can’t all be lined up with the coordinate system axes

• We need a new approach to determining if a ray hits a spot that’s on our tabletop

Page 4: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

A simpler problem

• How can we tell if a point lies in a triangle?

ab

c

p q

Point p lies inside triangle Δabc Point q lies outside triangle Δabc

Page 5: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Triangle algorithm

• Draw vectors from a to b and from a to c

• We can regard these vectors as the axes for a “skewed” coordinate system

• Then every point in the triangle’s plane would have a unique pair of coordinates

• We can compute those coordinates using Cramer’s Rule (from linear algebra)

Page 6: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

The algorithm idea

a b

c

pap = c1*ab + c2*ac

c1 = det( ap, ac )/det( ab, ac )c2 = det( ab, ap )/det( ab, ac )

Page 7: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Cartesian Analogy

p = (c1,c2)

x-axis

y-axis

x + y = 1

p lies outside the triangle if c1 < 0 or c2 < 0 or c1+c2 > 1

Page 8: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Determinant function: 2x2

typedef float scalar_t;

typedef struct { scalar_t x, y; } vector_t;

scalar_t det( vector_t a, vector_t b )

{

return a.x * b.y – b.x * a.y;

}

Page 9: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Constructing a regular hexagon

( cos(0*theta), sin(0*theta) )

( cos(1*theta), sin(1*theta) )( cos(2*theta), sin(2*theta) )

( cos(3*theta), sin(3*theta) )

( cos(4*theta), sin(4*theta) ) ( cos(5*theta), sin(5*theta) )

theta = 2*PI / 6

Page 10: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Subdividing the hexagon

A point p lies outside the hexagon -- unless it lies inside one of these four sub-triangles

Page 11: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Same approach for n-gons

• Demo program ‘hexagon.cpp’ illustrates the use of our just-described algorithm

• Every convex polygon can be subdivided into triangles, so the same ideas can be applied to any regular n-sided polygon

• Exercise: modify the demo-program so it draws an octagon, a pentagon, a septagon

Page 12: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Extension to a tetrahedron

• A tetrahedron is a 3D analog of a triangle

• It has 4 vertices, located in space (but not all vertices can lie in the same plane)

• Each face of a tetrahedron is a triangle

oa

b

c

Page 13: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Cramer’s Rule in 3D

typedef float scalar_t;typedef struct { scalar_t x, y, z; } vector_t;scalar_t det( vector_t a, vector_t b, vector_t c ){

scalar_t sum = 0.0;sum += a.x * b.y * c.z – a.x * b.z * c.y;sum += a.y * b.z * c.x – a.y * b.x * c.z;sum += a.z * b.x * c.y – a.z * b.y * c.x;return sum;

}

Page 14: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Is point in tetrahedron?

• Let o, a, b, c be vertices of a tetrahedron• Form the three vectors oa, ob, oc and

regard them as the coordinate axes in a “skewed” 3D coordinate system

• Then any point p in space has a unique triple of coordinates:

op = c1*oa + c2*ob + c3*oc• These three coordinates can be computed

using Cramer’s Rule

Page 15: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Details of Cramer Rule

c1 = det( op, ob, oc )/det( oa, ob, oc )

c2 = det( oa, op, oc )/det( oa, ob, oc )

c3 = det( oa, ob, op )/det( oa, ob, oc )

Point p lies inside the tetrahedron – unless

c1 < 0 or c2 < 0 or c3 < 0

or c1 + c2 + c3 > 1

Page 16: A question about polygons Devising a computational method for determining if a point lies in a plane convex polygon

Convex polyhedron

• Just as a convex polygon can be divided into subtriangles, any convex polyhedron can be divided into several tetrahedrons

• We can tell if a point lies in the polyhedron by testing to see it lies in one of the solid’s tetrahedral parts

• An example: the regular dodecahedron can be raytraced by using these ideas!