24
Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016

Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Embed Size (px)

Citation preview

Page 1: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Elements of 2d Computational Geometry

AI for Gaming Santa Clara, 2016

Page 2: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

3d Determinants

Absolute value is the volume of the parallelepiped Sign is the orientation

Page 3: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

2d Orientation

A B

CA+B

A+C B+CA+B+C

Sign of determinant

������

ax

ay

1

bx

by

1

cx

cy

1

������

depends on the orientation of the points (ax

, ay

, 1), (bx

, by

, 1) and (cx

, cy

, 1).If (a

x

, ay

), (bx

, by

), and (cx

, cy

) are oriented counterclockwise, then and only

then is the determinant positive.

Page 4: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Application

A

BC

D

Do two lines intersect?

C lies on a di↵erent side of AB than DSigns of determinants are di↵erent

������

ax

ay

1

bx

by

1

cx

cy

1

������⇥

������

ax

ay

1

bx

by

1

dx

dy

1

������< 0.

Page 5: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

2d Circles• Three points determine a circle

• If they are collinear, the circle is degenerate • with a radius of infinity

A B

C

Page 6: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

2d Circles• Geometric construction:

• Draw two chords AB and BC

A B

C

Page 7: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

2d Circles• Given three points A, B, C • Draw cords AB, BC • Create perpendicular

bisectors for each chord • By drawing a large enough circle

around A and B • The intersection is the center

of the circle • Radius is the distance of

center point to one of the three points

A B

C

Page 8: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

2d Circles• Determine whether a point is inside a circle

• If the circle is given by center and radius • Calculate distance of point from center and

compare with radius • If the circle is given by three points

• Use determinant formula • No need to calculate circle first

Page 9: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

2d Circles• Calculate the following determinant

• If A, B, C are in counter-clockwise order and the determinant is positive, then D lies inside the circle

• If A, B, C are in counter-clockwise order and the determinant is negative, then D lies not inside the circle

• If A, B, C are not in counter-clockwise order and the determinant is negative, then D lies inside the circle

• If A, B, C are not in counter-clockwise order and the determinant is positive, then D lies not inside the circle

��������

ax

ay

a2x

+ a2y

1bx

by

b2x

+ b2y

1cx

cy

c2x

+ c2y

1dx

dy

d2x

+ d2y

1

��������=

������

ax

� dx

ay

� dy

(ax

� dx

)2 + (ay

� dy

)2

bx

� dx

by

� dy

(bx

� dx

)2 + (by

� dy

)2

cx

� dx

cy

� dy

(cx

� dx

)2 + (cy

� dy

)2

������

Page 10: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Barycentric Coordinates• Given three (not collinear)

points a, b, c • Every point p can be written

as • p=αa+βb+(1-α-β)c• Divides plane into seven

areas depending on the signs of the coefficients

Α

Β

C

P

-+-

++-

+--

+++

--+

-++ +-+

Page 11: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

~p = ↵~a+ �~b+ (1� ↵� �)~c

, ~p� ~c = ↵(~a� ~c) + �(~b� ~c)

which implies

(~p� ~c) · (~a� ~c) = ↵(~a� ~c) · (~a� ~c) + �(~b� ~c) · (~a� ~c)

(~p� ~c) · (~b� ~c) = ↵(~a� ~c) · (~b� ~c) + �(~b� ~c) · (~b� ~c)

which gives according to Cramer’s rule

↵ =

�����(~p� ~c) · (~a� ~c) (

~b� ~c) · (~a� ~c)

(~p� ~c) · (~b� ~c) (

~b� ~c) · (~b� ~c)

����������(~a� ~c) · (~a� ~c) (

~b� ~c) · (~a� ~c)

(~a� ~c) · (~b� ~c) (

~b� ~c) · (~b� ~c)

�����

� =

����(~a� ~c) · (~a� ~c) (~p� ~c) · (~a� ~c)

(~a� ~c) · (~b� ~c) (~p� ~c) · (~b� ~c)

���������(~a� ~c) · (~a� ~c) (

~b� ~c) · (~a� ~c)

(~a� ~c) · (~b� ~c) (

~b� ~c) · (~b� ~c)

�����

Page 12: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Barycentric Coordinates• We note that in the preceding formula

• Only two dot products change if we change point p

Page 13: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Lines, Rays, Segments• Lines

• Given by two points a and d

a

dL={(1-t)a+td | t real}

Page 14: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Lines, Rays, Segments• Ray given by two points

a

dL={(1-t)a+td | t >= 0}

Page 15: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Lines, Rays, Segments• Segment between two points

a

dL={(1-t)a+td | 0 <= t <= 1}

Page 16: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Dot Product

u

v

u﹡v<0

obtuse

u﹡v=0

right angle

u﹡v>0

acute

The dot product can be used for many geometric tests

Page 17: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Dot Product

u

v

||v||

|d|=||u||-1u・v=||v|| cos θ

d=(u·v)(u·u)-1 u

~u · ~v = ||~u|| ||~v|| cos ✓~u · ~u = ||~u||2

~u · ~v = ~v · ~u~u · (~v + ~w) = ~u · ~v + ~u · ~w(r~u) · (s~v) = sr(~u · ~v)

Page 18: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Bounding Volumes

Sphere Axis Aligned Bounding Box (AABB)

Oriented bounding box (OBB)

better bound, better culling

faster test, less memory

Page 19: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

AABBRepresentations

min — max min — widths center — radius

Page 20: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

AABB IntersectionsProject min and max points on x-axis and y-axis AABB intersects if the projected intervals intersect

Page 21: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

AABB Intersections• Intersection test for min-max representation

class AABB: def __init__(self, mini, maxi): self.mini = mini self.maxi = maxi def __str__(self): return “[{},{}]”.format(str(self.mini), str(self.maxi) def intersect(self, other): if self.maxx < other.minx or self.minx > self.maxx: return False if self.maxy < other.miny or self.miny > self.maxy: return False return True

Page 22: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Intersection Ray Circle

||~p+ t~d� ~c|| = r

(~p+ t~d� ~c) · (~p+ t~d� ~c) = r2

(~m+ t~d) · (~m+ t~d) = r2 with ~m = ~p� ~c

(~d · ~d)t2 + 2(~m · ~d)t+ (~m · ~m� r2) = 0

t = �(~m · ~d)±q(~m · ~d)2 � ~m · ~m+ r2

r

C

P

d

{P+td | t>0}Ray given by origin and direction

Solve for the points that lie on the outside of the circle

Check whether they lie on the ray

Page 23: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Intersection Ray CircleIntersect twice Intersect tangentially No intersection

False Intersection Ray starts inside

Page 24: Elements of 2d Computational Geometrytschwarz/COEN129/PPT/AnalyticalGeometry.pdf · Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016. 3d Determinants Absolute

Intersection Ray AABB

Horizontal Slab

Vertical Slab

Use intersection with horizontal and vertical slab.

If there is no or only one intersection, then the ray cannot intersect the AABB.

If the intersections do not overlap, then the ray does not intersect the AABB.