242-535 ADA: 15. Basic Math1 Objective o a reminder about dot product and cross product, and their...

31
242-535 ADA: 15. Basic Math 1 • 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

Transcript of 242-535 ADA: 15. Basic Math1 Objective o a reminder about dot product and cross product, and their...

242-535 ADA: 15. Basic Math

1

• Objectiveo 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

242-535 ADA: 15. Basic Math

2

1. Simple Geometry2. Dot (Scalar) Product3. Cross (Vector) Product4. The CCW() Function5. Line Segment Intersection6. Distance Between a Point and

a Line7. Area of a Convex Polygon

Overview

242-535 ADA: 15. Basic Math

3

• Pointso (x, y) Cartesian coordinateso (r, Θ) Polar coordinates

• Line Segmentso Positioned using their end points

• Lineso 2-tuple (m, c) using y = mx + co 3-tuple (a, b, c) using ax + by = co Two points P0, P1 on the line using P(t) = (1-t)P0 + t P1

• Polygonso Ordered list of points

1. Simple Geometry

Line Segments & Vectors

p = (x , y )

p = (x , y )

1

2

O = (0, 0) x

y

1 1

2 2

Points (vectors): p , p , p p = p p 1 2 1 2 2 1

p p = (x x , y y ) 1 2

1 2 1 2

Line segment: p p = p p 2 1 1 2

242-535 ADA: 15. Basic Math

5

• Given the two vectors a = <a1, a2, a3> and b = <b1, b2, b3> then the dot product is

a· b = a1*b1 + a2*b2 + a3*b3

• Example a = <0, -3, 7>, b = <2, 3, 1> a· b = 0*2 + -3*3 + 7*1 = 9 – 7 = 2

2. Dot (Scalar) Product

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|)

242-535 ADA: 15. Basic Math

7

• Determine the angle between a = <3, -4, -1> and

b = <0, 5, 2>

• Dot product a· b = -22, |a| = , |b| = • The angle is: = = -0.8011927• θ = cos-1(-0.8011927) = 2.5 radians

= 143.24 degrees

Angle Example

242-535 ADA: 15. Basic Math

8

• It can be used to get the projection (length) of vector a onto vector b. 

Uses of Dot Product

242-535 ADA: 15. Basic Math

9

• Given two vectors a = <a1, a2, a3> and b = <b1, b2, b3> then the cross product is

a x b = <a2*b3 – a3*b2, a3*b1 – a1*b3, a1*b2 – a2*b1>This can be written as a determinant: This expands to i - + k

3. Cross (Vector) Product

242-535 ADA: 15. Basic Math

10

• a = <2, 1, -1> and b = <-3, 4, 1>

Calculate a x b i - + kso i - + k

= 5 i + j + 11 k = <5, 1, 11>

Cross Product Example

242-535 ADA: 15. Basic Math

11

• 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

242-535 ADA: 15. Basic Math

12

• |a x b| = |a||b| sin(θ)so θ = sin-1((a x b) / |a||b|)

• What is the angle between a = <2, 1, -1> and b = <-3, 4, 1> (from the last example)?

a x b = 5 i + j + 11 k |a x b| = = 12.124 |a| = (4 + 1 + 1)1/2 = 2.450 |b| = (9 + 16 + 1)1/2 = 5.099

• so θ = sin-1(12.124/(2.450*5.099)) = 76.05 degreesor perhaps 180 - 76.05 = 103.95

Angle Example

242-535 ADA: 15. Basic Math

13

• 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

242-535 ADA: 15. Basic Math

14

• Suppose we have three vectors a, b, c which form a 3D figure like so:

Geometric Properties

ba

c

242-535 ADA: 15. Basic Math

15

• 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 planeo in terms of the box diagram, it means that |a| is 0 units

above the b x c face

Area and Volume

242-535 ADA: 15. Basic Math

16

• Determine if the vectors a = <1, 4, -7>, b = <2, -1, 4>, and c = <0, -9, 18> lie on the same plane.

• i - + k = 18i - 36j - 18k = <18, -36, -18>• a · (b x c) = <1, 4, -7> · <18, -36, -18> = 1*18 + 4*-36 + 7*18 = 18 – 144 + 126 = 0• volume = |a · (b x c) | = 0, which means that all

3 vectors do lie on the same plane.

Example

242-535 ADA: 15. Basic Math

17

• 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

01

ccw(p0, p1, p2) returns 1

Turning of Consecutive Segments

Counterclockwise Clockwise No turn (collinear)

p

p

p

0

1

2

p

p

p0

1

2p p

p0

1 2

p p p p > 00 1 0 2 p p p p < 00 1 0 2p p p p = 00 1 0 2

Segments p p and p p . Move from p to p then to p . 0 1 1 2 0 1 2

242-535 ADA: 15. Basic Math

19

• Do two line segments and intersect ?

5. Line Segment Intersection

p4

p3p1

p2

• Boundary cases must be considered.

242-535 ADA: 15. Basic Math

20

• A segment straddles a line if point p1 lies on one side of the line and point p2 lies on the other side.

• A boundary case arises if p1 or p2 lies directly on the line. Two line segments intersect if and only if either (or both) of the following conditions holds:o 1. Each segment straddles the line containing the other.o 2. An endpoint of one segment lies on the other

segment. (This condition comes from the boundary case.)

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 ) (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

p2

p3

p4

3

1

// the line through p p// intersects p p .

4

2

3

// the line through p p// intersects p p . 4

1 2

242-535 ADA: 15. Basic Math

22

SEGMENTS-INTERSECT(p1, p2, p3, p4)1 d1 = CCW(p3, p4, p1)2 d2 = CCW(p3, p4, p2)3 d3 = CCW(p1, p2, p3)4 d4 = CCW(p1, p2, p4)5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0)) &&

((d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0))6 return true7 elseif d1 = 0 and ON-SEGMENT(p3, p4, p1)8 return true9 elseif d2 = 0 and ON-SEGMENT(p3, p4, p2)10 return true11 elseif d3 = 0 and ON-SEGMENT(p1, p2, p3)12 return true13 elseif d4 = 0 and ON-SEGMENT(p1, p2, p4)14 return true15 else return false

Code

linesstraddle

checkboundarycases

242-535 ADA: 15. Basic Math

23

CCW(pi, pj, pk) // called DIRECTION in CLRS

return (pk - pi) × (pj - pi)

ON-SEGMENT(pi, pj, pk)

1 if min(xi, xj) ≤ xk ≤ max(xi, xj) && min(yi, yj) ≤ yk ≤ max(yi, yj)

2 return true

3 else return false

242-535 ADA: 15. Basic Math

24

Intersection Cases

and straddle each other lines

does not straddle the line containing

242-535 ADA: 15. Basic Math

25

p3 is collinear with and is between p1 and p2

p3 is collinear with and is not between p1 and p2

242-535 ADA: 15. Basic Math

26

• Given a point and a line or line segment, determine the shortest distance between them.

6. Distance Between a Point and a

Line

P3

P1P2

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

242-535 ADA: 15. Basic Math

27

• 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)

242-535 ADA: 15. Basic Math

28

• 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.

242-535 ADA: 15. Basic Math

29

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

242-535 ADA: 15. Basic Math

30

• 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

(x1, y1)

(xn, yn)

(x2, y2)

242-535 ADA: 15. Basic Math

31

Example

Can be proved using induction on area defined in terms of triangles.