Presentation is loading. Please wait.

Presentation is loading. Please wait.

242-535 ADA: 15. Basic Math1 Objective o a reminder about dot product and cross product, and their use for analyzing line segments (e.g. do two segments.

Similar presentations


Presentation on theme: "242-535 ADA: 15. Basic Math1 Objective o a reminder about dot product and cross product, and their use for analyzing line segments (e.g. do two segments."— Presentation transcript:

1 242-535 ADA: 15. Basic Math1 Objective o a reminder about dot product and cross product, and their use for analyzing line segments (e.g. do two segments intersect and the distance of a point to a line) Algorithm Design and Analysis (ADA) 242-535, Semester 1 2014-2015 15. Some Basic Maths Used in CG

2 242-535 ADA: 15. Basic Math2 1.Simple Geometry 2.Dot (Scalar) Product 3.Cross (Vector) Product 4.The CCW() Function 5.Line Segment Intersection 6.Distance Between a Point and a Line 7.Area of a Convex PolygonOverview

3 242-535 ADA: 15. Basic Math3 Points o (x, y) Cartesian coordinates o (r, Θ) Polar coordinates Line Segments o Positioned using their end points Lines o 2-tuple (m, c) using y = mx + c o 3-tuple (a, b, c) using ax + by = c o Two points P 0, P 1 on the line using P(t) = (1-t)P 0 + t P 1 Polygons o Ordered list of points 1. Simple Geometry

4 Line Segments & Vectors p = (x, y ) 1 2 O = (0, 0)x y 1 2 Points (vectors): p, p, p  p = p p 1 2 1 2 2 1 p  p = (x  x, y  y ) 1 2 Line segment: p p = p p 2 1 1 2

5 242-535 ADA: 15. Basic Math5 Given the two vectors a = and b = then the dot product is a · b = a1*b1 + a2*b2 + a3*b3 Example a =, b = a · b = 0*2 + -3*3 + 7*1 = 9 – 7 = 2 2. Dot (Scalar) Product

6 Dot Product as Geometry a  b = |a| |b| cos θ The dot product is the product of the magnitudes of the two vectors a and b and the cosine of the angle between them. The angle between a and b is: Θ = cos -1 ((a  b) / |a||b|) The angle between a and b is: Θ = cos -1 ((a  b) / |a||b|)

7 242-535 ADA: 15. Basic Math7 Angle Example

8 242-535 ADA: 15. Basic Math8 It can be used to get the projection (length) of vector a onto vector b. Uses of Dot Product

9 242-535 ADA: 15. Basic Math9 3. Cross (Vector) Product

10 242-535 ADA: 15. Basic Math10 Cross Product Example

11 242-535 ADA: 15. Basic Math11 The cross product a × b is defined as a vector c that is perpendicular to both a and b, with a direction given by the right-hand rule (the unit vector n ) and a magnitude equal to the area of the parallelogram that the vectors span. Cross Product as Geometry the area of the parallelogram. a  b = |a| |b | sin θ n

12 242-535 ADA: 15. Basic Math12 Angle Example

13 242-535 ADA: 15. Basic Math13 You can use the cross product of the vectors a and b to generate a perpendicular vector, which is the normal to the plane defined by a and b. Normals are very useful when calculating things like lighting in 3D computer games, and up/down in FPSs. Uses of Cross Product

14 242-535 ADA: 15. Basic Math14 Suppose we have three vectors a, b, c which form a 3D figure like so: Geometric Properties b a c

15 242-535 ADA: 15. Basic Math15 Area of the parallelogram of the front face of the object is: area = | a x b | Volume of the parallelepiped (the entire object) is: volume = | a · ( b x c ) | If the volume == 0 then it must mean that all three vectors lie in the same plane o in terms of the box diagram, it means that |a| is 0 units above the b x c face Area and Volume

16 242-535 ADA: 15. Basic Math16Example

17 242-535 ADA: 15. Basic Math17 A basic geometric function: o Returns 1 if the points are in counter-clockwise order (ccw) o Returns -1 if the points are in clockwise order (cw) o Returns 0 if the points are collinear CCW() utilises cross product. 4. The CCW() Function 2 0 1 ccw(p0, p1, p2) returns 1

18 Turning of Consecutive Segments Counterclockwise Clockwise No turn (collinear) p p p 0 1 2 p p p 0 1 2 pp p 0 12 p p  p p > 0 0 1 0 2 p p  p p < 0 0 1 0 2 p p  p p = 0 0 1 0 2 Segments p p and p p. Move from p to p then to p. 0 1 1 2 0 1 2

19 242-535 ADA: 15. Basic Math19 5. Line Segment Intersection p4 p3 p1 p2 Boundary cases must be considered.

20 242-535 ADA: 15. Basic Math20

21 Using Cross Products p p 1 2 p p 3 4 Two line segments intersect iff each of the two pairs of cross products below have different signs (or one cross product in the pair is 0). (p  p )  ( p  p ) and ( p  p )  ( p  p ) 1 4 3 4 2 4 3 4 3 2 1 2 4 2 1 2 p 1 p 2 p 3 p 4 3 1 // the line through p p // intersects p p. 4 23 // the line through p p // intersects p p. 4 12

22 242-535 ADA: 15. Basic Math22 SEGMENTS-INTERSECT(p 1, p 2, p 3, p 4 ) 1 d 1 = CCW(p 3, p 4, p 1 ) 2 d 2 = CCW(p 3, p 4, p 2 ) 3 d 3 = CCW(p 1, p 2, p 3 ) 4 d 4 = CCW(p 1, p 2, p 4 ) 5 if ((d 1 > 0 and d 2 0)) && ((d 3 > 0 and d 4 0)) 6 return true 7 elseif d 1 = 0 and ON-SEGMENT(p 3, p 4, p 1 ) 8 return true 9 elseif d 2 = 0 and ON-SEGMENT(p 3, p 4, p 2 ) 10 return true 11 elseif d 3 = 0 and ON-SEGMENT(p 1, p 2, p 3 ) 12 return true 13 elseif d 4 = 0 and ON-SEGMENT(p 1, p 2, p 4 ) 14 return true 15 else return falseCode lines straddle check boundary cases

23 242-535 ADA: 15. Basic Math23 CCW(p i, p j, p k ) // called DIRECTION in CLRS return (p k - p i ) × (p j - p i ) ON-SEGMENT(p i, p j, p k ) 1 if min(x i, x j ) ≤ x k ≤ max(x i, x j ) && min(y i, y j ) ≤ y k ≤ max(y i, y j ) 2 return true 3 else return false

24 242-535 ADA: 15. Basic Math24 Intersection Cases

25 242-535 ADA: 15. Basic Math25

26 242-535 ADA: 15. Basic Math26 Given a point and a line or line segment, determine the shortest distance between them. 6. Distance Between a Point and a Line P3 P1 P2 h The equation of a line defined through two points P1 (x1,y1) and P2 (x2,y2) is P = P1 + u (P2 - P1) using the dot product

27 242-535 ADA: 15. Basic Math27 The point P3 (x3, y3) is closest to the line at the tangent to the line which passes through P3, that is, the dot product of the tangent and line is 0, thus ( P3 - P )  ( P2 - P1 ) = 0 Substituting the equation of the line gives: [ P3 - P1 - u( P2 - P1 )]  ( P2 - P1 ) = 0 Solving this gives the value of u: Uses a  (b+c) = (a  b) + (a  c)

28 242-535 ADA: 15. Basic Math28 Substituting this into the equation of the line gives the point of intersection (x, y) of the tangent as: x = x1 + u (x2 - x1) y = y1 + u (y2 - y1) The distance therefore between the point P3 and the line is the distance between (x, y) and P3.

29 242-535 ADA: 15. Basic Math29 double distToSegment(Point2D p1, Point2D p2, Point2D p3) { double xDelta = p2.getX() - p1.getX(); double yDelta = p2.getY() - p1.getY(); if ((xDelta == 0) && (yDelta == 0)) return -1; // p1 and p2 are the same point double u = ((p3.getX() - p1.getX()) * xDelta + (p3.getY() - p1.getY()) * yDelta) / (xDelta * xDelta + yDelta * yDelta); Point2D closePt; // the (x,y) of the previous slide if (u < 0) closePt = p1; else if (u > 1) closePt = p2; else closePt = new Point2D.Double(p1.getX() + u * xDelta, p1.getY() + u * yDelta); return closePt.distance(p3); }Code

30 242-535 ADA: 15. Basic Math30 The coordinates (x1, y1), (x2, y2), (x3, y3),..., (xn, yn) of a convex polygon are arranged as shown. They must be in counter-clockwise order around the polygon, beginning and ending at the same point. 7. Area of a Convex Polygon (x 1, y 1 ) (x n, y n ) (x 2, y 2 ) … …

31 242-535 ADA: 15. Basic Math31 Example Can be proved using induction on area defined in terms of triangles.


Download ppt "242-535 ADA: 15. Basic Math1 Objective o a reminder about dot product and cross product, and their use for analyzing line segments (e.g. do two segments."

Similar presentations


Ads by Google