Elements of 2d Computational Geometry AI for Gaming Santa Clara, 2016
3d Determinants Absolute value is the volume of the parallelepiped Sign is the orientation
2d Orientation A+B+C A+C B+C C A+B A B Sign of determinant a x a y 1 b x b y 1 c x c y 1 depends on the orientation of the points (a x,a y, 1), (b x,b y, 1) and (c x,c y, 1). If (a x,a y ), (b x,b y ), and (c x,c y ) are oriented counterclockwise, then and only then is the determinant positive.
Application Do two lines intersect? C B A D C lies on a di erent side of AB than D Signs of determinants are di erent a x a y 1 b x b y 1 c x c y 1 a x a y 1 b x b y 1 d x d y 1 < 0.
2d Circles Three points determine a circle If they are collinear, the circle is degenerate with a radius of infinity A B C
2d Circles Geometric construction: Draw two chords AB and BC A B C
2d Circles Given three points A, B, C Draw cords AB, BC A B 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 C
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
2d Circles Calculate the following determinant a x a y a 2 x + a 2 y 1 b x b y b 2 x + b 2 y 1 c x c y c 2 x + c 2 y 1 d x d y d 2 x + d 2 y 1 = a x d x a y d y (a x d x ) 2 +(a y d y ) 2 b x d x b y d y (b x d x ) 2 +(b y d y ) 2 c x d x c y d y (c x d x ) 2 +(c y d y ) 2 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
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 -+- +-- Α ++- Β +++ P -++ +-+ C --+
~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)
Barycentric Coordinates We note that in the preceding formula Only two dot products change if we change point p
Lines, Rays, Segments Lines Given by two points a and d d L={(1-t)a+td t real} a
Lines, Rays, Segments Ray given by two points d L={(1-t)a+td t >= 0} a
Lines, Rays, Segments Segment between two points d a L={(1-t)a+td 0 <= t <= 1}
Dot Product v obtuse right angle acute u * v<0 u * v=0 u * v>0 u The dot product can be used for many geometric tests
Dot Product v ~u ~v = ~u ~v cos ~u ~u = ~u 2 v ~u ~v = ~v ~u ~u (~v + ~w) = ~u ~v + ~u ~w (r~u) (s~v) = sr(~u ~v) θ d u d=(u v)(u u) -1 u d = u -1 u v= v cos θ
Bounding Volumes Sphere Axis Aligned Bounding Box (AABB) Oriented bounding box (OBB) better bound, better culling faster test, less memory
AABB Representations min max min widths center radius
AABB Intersections Project min and max points on x-axis and y-axis AABB intersects if the projected intervals intersect
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
Intersection Ray Circle Ray given by origin and direction Solve for the points that lie on the outside of the circle {P+td t>0} r C Check whether they lie on the ray d ~p + t ~ d ~c = r (~p + t ~ d ~c) (~p + t ~ d ~c) = r 2 (~m + t ~ d) (~m + t ~ d) = r 2 with ~m = ~p ~c ( ~ d ~d)t 2 + 2(~m ~d)t +(~m ~m r 2 ) = 0 t = (~m ~d) ± q (~m ~d) 2 ~m ~m + r 2 P
Intersection Ray Circle Intersect twice Intersect tangentially No intersection False Intersection Ray starts inside
Intersection Ray AABB Use intersection with horizontal and vertical slab. If there is no or only one intersection, then the ray cannot intersect the AABB. Horizontal Slab If the intersections do not overlap, then the ray does not intersect the AABB. Vertical Slab