Download presentation

Presentation is loading. Please wait.

Published byJeffery Bigsby Modified about 1 year ago

1
Computational Geometry

2
Given two lines L1: A(x1,y1) B(x2,y2) L2: C(x3,y3) D(x4,y4) Q. Find if they are perpendicular: Method1: Calculate m1=(B.y-A.y)/(B.x-A.x) m2=(D.y-C.y)/(D.x-C.x) If(m1*m2==-1) then “Perpendicular”

3
Problem with this approach: 1.Check for divisibility by zero 2. Loss of precision in divisibilty Method 2: Check if dotProduct is zero: int dotProduct(Point A,Point B,Point C,Point D) { int x1,y1,x2,y2; x1=B.x-A.x; y1=B.y-A.y; x2=D.x – C.x; y2 = D.y – C.y; return (x1*x2+y1*y2); }

4
Q. How to calculate cross product: int crossProduct(Point A, Point B, Point C, Point D) { int x1,y1,x2,y2; x1=B.x-A.x; y1=B.y-A.y; x2=D.x – C.x; y2 = D.y – C.y; return (x1*y2-x2*y1); }

5
◦ Find the distance between a line AB and point C ◦ A(x1,y1), B(x2,y2), C(x3,y3) A B C

6
Area of parallelogram = AB x AC Area of triangle = (AB x AC)/2 (Base * Height)/2 = (AB x AC)/2 Height = (AB x AC)/|AB| A B C

7
Distance between a line segment (AB) and a point (C). double linePointDist(Point A, Point B, Point C, boolean isSegment){ double dist = cross(B-A,C-A) / distance(A,B); if(isSegment) { int dot1 = dot(B-A,C-B); // AB. BC if(dot1 > 0) return distance(B,C); int dot2 = dot(A-B,C-A); // BA. AC if(dot2 > 0) return distance(A,C); } return abs(dist); }

8
Try to use struct/class for a point and all calculations using p.x and p.y ◦ struct Point { int x; int y; } Use templating for extending point with double/float coordinates. Overload operators for cross/dot product, add, subtract. Sometimes, a clever heuristic will save you lot of work and knowledge otherwise required to solve a problem. While comparing two floating point numbers, we take a very small value(epsilon ~ 10^-6) for comparison.

9
Find the area of a polygon. We start triangulating the polygon by dividing into number of triangles. Pick a vertex and two adjacent points. Calculate area of triangle so formed Now the vertex picked is same but other two vertex change. Area of the polygon shown = ◦ ABC(AB x AC) +ACD(AC x AD) + ADE(AD x AE) ◦ If it were a Concave Polygon?????? C D E B A

10
int area = 0; //We will triangulate the polygon into triangles with points p[0],p[i],p[i+1] for(int i = 1; i+1

11
Test if a point is interior or exterior

12
Drawing a ray from given point out to infinity in some direction. Each time the ray crossed the boundary of the polygon, it would cross from the interior to the exterior, or vice versa Odd number of time crosses: Interior Even : Exterior To implement, pick multiple large random number for a far point and vote for the best answer

13
String testPoint(verts, x, y) { int N = lengthof(verts); int cnt = 0; double x2 = random()* ; double y2 = random()* ; for(int i = 0; i

14
A = y 2 -y 1 B = x 1 -x 2 C = A*x 1 +B*y 1 A 1 B 2 x + B 1 B 2 y = B 2 C 1 A 2 B 1 x + B 1 B 2 y = B 1 C 2 To solve ◦ A1x + B1y = C1 ◦ A2x + B2y = C2 Multiply 1 by B2 and 1 by B1 So x = (B2C1 – B1C2) / (A1B2 – A2B1)

15
double det = A 1 *B 2 - A 2 *B 1 { if(det == 0){ //Lines are parallel } else{ double x = (B 2 *C 1 - B 1 *C 2 )/det double y = (A 1 *C 2 - A 2 *C 1 )/det } For line segment, check x,y lies on it or not. min(x 1,x 2 ) ≤ x ≤ max(x1,x2) Precision issues: What if we just want to check if two line segments intersect or not??

16
// sign of BA x CA int ccw(Point A, Point B, Point C); return value 0 if BAxCA = 0 => A, B, C are collinear 1 if BAxCA > 0 => Triangle ABC is aligned anti-clockwise -1 if BAxCA Triangle ABC is aligned clockwise Application example if(ccw(a, c, d) == ccw(b, c, d)) // a, b lie on the same side of line cd

17
// Line intersection AB and CD if(ccw(A, B, C) == 0 && ccw(A, B, D) == 0){ // A, B, C, D are collinear if( (AB. BC > 0 and AB. BD > 0) || (BA. AC > 0 and BA. AD > 0) ){ // Doesn’t Intersect else // Intersect } if( ccw(A, C, D) != ccw(B, C, D) && ccw(C, A, B) != ccw(D, A, B) ) // Intersect else // Doesn’t Intersect

18
Given N sets of points, find the closest distance between two points.

19
10

20
20

21

22
8 16 6

23
12 6 4

24
4 8

25
Sort the set according to x-coordinates Sweep a vertical line across the set of points,from left to right, performing certain actions every time a point is encountered during the plane sweep Lets say till now the closest pair encountered has a distance of d and now the sweep line is at a new point p. In order to get a distance < d, we need to consider only points within a strip of distance d to the left of the sweep line. Along y axis, we need to consider points within distance +d and –d from the current point p. Thus the strip reduces to d x 2*d rectangle. These points will be stored in an ordered set D (sorted by their y- coordinates)

26
Remove the points further than d to the left of p from the ordered set D that is storing the points in the strip. Determine the point on the left of p that is closest to it in d x 2*d rectangle If the distance between this point and p is less than d (the current minimum distance), then update the closest current pair and d.

27
There can be many points in d x2*d rectangle for a p, so how is complexity O(nlgn)? In the given rectangle, each point(except p) will be at distance > d from each other. If their existed a pair with distance(dnew) less than d, then the minimum distance till now would have been dnew. So what is the maximum number of points that can exist in d x 2*d rectangle such that any two points are at distance at least d ?

28
Maximum number of points in d x 2*d can be 6 Imagine a circle of radius d around each point representing the area that we are not allowed to insert another point into We can place points somewhere inside the light blue area or on the edges of the circles Hence we put one more in the middle of the left side leaving the entire box covered except for the remaining two corners and the middle of the right side (all three of these points being right on the boundaries of the circles) So a maximum of 6 points.

29
We are only removing points which are distance > d along x-axis not along y-axis. Thus giving the width of rectangle as d. But how to maintain it’s height 2*d? We cannot remove all points at a distance > d along y axis because the next point we consider may be directly above the current point which may require those points A set may even contain close to n points when all points are on a vertical line In that set we consider only points with y coordinates (y-d to y+d), and find the minimum distance

30
double lineSweep(Point arr[]) { d = distance(arr[0],arr[1]); l=0; set.insert(arr[0]); set.insert(arr[1]); for(r=2;r

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google