420 likes | 524 Views
Current Jotto standings…. Sophs. Jrs. Srs. Profs. pluot 1. pluot 2. pluot 1. pluot 2. squid 2. squid 1. squid 0. squid 1. Sophs & "Profs" guessing. Turning direction. (15,30). 3. (20,20). (40,20). 2. 3'. Bertrand Planes' Life Clock. (10,10). 1.
E N D
Current Jotto standings… Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2 squid 1 squid 0 squid 1 Sophs & "Profs" guessing...
Turning direction (15,30) 3 (20,20) (40,20) 2 3' Bertrand Planes' Life Clock (10,10) 1 def turningDirection( pt1, pt2, pt3 ): """ returns 1 if pt1 -> pt2 -> pt3 is a CCW turn returns -1 if pt1 -> pt2 -> pt3 is a CW turn returns 0 if they are collinear """ x1, y1 = pt1; x2, y2 = pt2; x3, y3 = pt3; # the signed magnitude of the cross product CP = (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1) if CP > 0: return 1 if CP < 0: return -1 return 0 Python code for CCW turning
Geometric algorithms All points on this line are Line Segment intersection… (xA,yA) + s(xB-xA,yB-yA) (xB,yB) dxA dyA (xi,yi) (x2,y2) (xA,yA) (x1,y1) All points on this line are (x1,y1) + r(x2-x1,y2-y1) dx1 dy1 dx1 dxA dy1dyA r s xA - x1 yA- y1 Solving these equations finds the intersection via r and s. =
Geometric algorithms Line Segment intersection… pt2 = (20,20) pt5 = (40, 20) pt3 = (10, 20) pt1 = (10, 10) pt4 = (20, 10) Line segment #1 runs from (10, 10) to (20, 20) Line segment #2 runs from (10, 20) to (20, 10) Intersection result = (15.0, 15.0, 1, 0.5, 0.5) Line segment #1 runs from (10, 10) to (10, 20) Line segment #2 runs from (20, 20) to (20, 10) Intersection result = (0, 0, 0, 0, 0) Line segment #1 runs from (10, 10) to (20, 20) Line segment #2 runs from (20, 10) to (40, 20) Intersection result = (0.0, 0.0, 1, -1.0, -1.0)
Geometric algorithms Java has its advantages! Line2d.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4); Polygon().contains(x,y); Polygon Line2D
Convex Hull First approach: brute force? the segments surrounding the exterior of a point set.
The Graham Scan "first algorithm in computational geometry" a stack (e.g., python list) 4) push P[0] and P[1] onto S 5) run the scan: i = 2 while i < N: A = the top of stack S B = the second point in S if (P[i] is to the left of B wrt A): push P[i] onto stack S i = i+1 else: pop stack S and discard the top 1) Find extremal point, P[0] 2) Sort all other points in terms of their angles with P[0] - use atan2 ! 3) the sorted list is P[i]
Graham Scan: java Stack grahamScan(Coordinate[] c) { Point p; Stack ps = newStack(); ps.push(c[0]); ps.push(c[1]); ps.push(c[2]); for (int i = 3; i < c.length; i++) { p = ps.pop(); while (computeOrientation(ps.peek(), p, c[i]) > 0)) { ps.pop(); } ps.push(p); ps.push(c[i]); } ps.push(c[0]); return ps; }
Convex Hull #2 Jarvis’s March - shown here convex hull Graham’s Scan - see previous
Jarvis March draw a line to this point (why?) start here Jarvis’s March - shown here convex hull Graham’s Scan - see previous
Jarvis March draw a line to this point q start here draw a line to the point with the LEAST relative angle (q) Jarvis’s March - shown here convex hull Graham’s Scan - see previous
Jarvis March draw a line to the point with the LEAST relative angle (q) start here Jarvis’s March - shown here convex hull Graham’s Scan - see previous
Jarvis March draw a line to the point with the LEAST relative angle (q) q relative to the previous angle! Jarvis’s March - shown here convex hull Graham’s Scan - see previous
Jarvis March continue until you return… Jarvis’s March - shown here convex hull Graham’s Scan - see previous
This week's Problems… • read over these problems: judge easy/hard? • try one or two to finish this afternoon/evening... • what geometric computation is needed? Hopefully everyone can get 1 or 2 of these completed!
The EE Problem Input # of test cases # of room vertices, # of routers, # of test points example input C + B vertices of the room A test locations + + routers Locations of the routers A Locations of the test points Output B C Data Set 1: 10.81 3.42 0.00 The strength of the signal at each test location == 1.0/(closest visible router ** 2)
W W W W The IE Problem Input Industrial Engineering # of test cases 1 4 4 0.1 0.1 0.0 0.9 1.0 0.05 1.1 -0.1 -0.1 -0.1 0.8 0 1.1 0.5 0.7 0 0.3 0.5 0 0.3 # of stores and possible warehouse locations (x,y) location of stores $.5 (x,y,price) location of warehouses and their cost to build in Mega$ S delivery cost = Euclidean distance S S Output $.3 $.3 S $.8 Data Set 1: 2.32 The minimum total cost to supply all stores from some warehouse(s).
The Superpaint Problem Input one side of the square lattice Col 4 Col 3 Col 2 Col 1 . . . . C . C . . . . . C . . . number of occupied squares Row 1 4 3 2 1 2 3 4 1 Row 2 Locations of the cows (row,col) Row 3 Row 4 Col 4 Output Col 3 Col 2 Col 1 . . . . B . B . . B . . B . B . Row 1 5 Row 2 Row 3 The number of locations that "attack" all occupied squares with a Queen's move Row 4
The Safepens Problem Input Number of rectangular fences 4 1 1 16 16 6 6 11 13 7 7 9 12 3 3 10 5 The fences! (lower left and upper right vertices) (16,16) (11,13) (9,12) (7,7) (6,6) Output (10,5) (3,3) 3 1 (1,1) The deepest nesting level The number of pens at that level
"Sweepline algorithm" http://en.wikipedia.org/wiki/Bentley–Ottmann_algorithm
D Priority Queue C Binary Search Tree A B http://en.wikipedia.org/wiki/Bentley–Ottmann_algorithm
The Screens Problem Input # of test cases # of room vertices # of projector screens (x,y) location of "you" room Vertices of the room oriented screens Output Data Set 1: 90.00% Line segments of the screens The total fraction of the presentation you can observe (all screens' contributions!)
Current Jotto standings… Sophs Jrs Srs Profs Chalk 1 Chalk 0 Chalk 1 Chalk 1 Quine 1 Quine 1 Quine 2 Quine 2 aught 2 aught 1 aught 1 aught 2 jotto 2 jotto 2 jotto 0 jotto 1 savvy 2 savvy 0 savvy 1 savvy 1 clash 2 clash 0 clash 1 clash 1
Current Jotto standings… Sophs Jrs Srs Others icily 0 icily 0 icily 1 icily 1 strep 2 strep 2 strep 2 strep 1 spork 1 spork 3 spork 0 spork 0 spend 2 spend 2 spend 2 spend 2 peeps 2 peeps 1 peeps 2 peeps 1 furls 1 furls 1 furls 0 furls 1 Ghost 2 Ghost 1 Ghost 1 Ghost 0 Tanks 2 Tanks 1 Tanks 2 Tanks 1 Gecko 2 Gecko 1 Gecko 1 Gecko 1
"QuickHull" choose L and R pts draw chord
"QuickHull" choose L and R pts draw chord • assign sides • find farthest point on each side • create triangle recurse!
"QuickHull" choose L and R pts draw chord • assign sides • assign sides • find farthest point on each side • create triangle recurse!
The Sweepline Algorithm choose L and R pts draw chord • assign sides • assign sides • find farthest point on each side • create triangle recurse!
The Safepens Problem Input Number of rectangular fences 4 1 1 16 16 6 6 11 13 7 7 9 12 3 3 10 5 The fences! (lower left and upper right vertices) (16,16) (11,13) (9,12) (7,7) (6,6) Output (10,5) (3,3) 3 1 (1,1) The deepest nesting level The number of pens at that level
The Superpaint Problem Input one side of the square lattice Col 4 Col 3 Col 2 Col 1 . . . . C . C . . . . . C . . . number of occupied squares Row 1 4 3 2 1 2 3 4 1 Row 2 Locations of the cows (row,col) Row 3 Row 4 Col 4 Output Col 3 Col 2 Col 1 . . . . B . B . . B . . B . B . Row 1 5 Row 2 Row 3 The number of locations that "attack" all occupied squares with a Queen's move Row 4
The Screens Problem Input # of test cases # of room vertices # of projector screens (x,y) location of "you" room Vertices of the room oriented screens Output Data Set 1: 90.00% Line segments of the screens The total fraction of the presentation you can observe (all screens' contributions!)
The EE Problem Input # of test cases # of room vertices, # of routers, # of test points example input C + B vertices of the room A test locations + + routers Locations of the routers A Locations of the test points Output B C Data Set 1: 10.81 3.42 0.00 The strength of the signal at each test location == 1.0/(closest visible router ** 2)
W W W W The IE Problem Input Industrial Engineering # of test cases 1 4 4 0.1 0.1 0.0 0.9 1.0 0.05 1.1 -0.1 -0.1 -0.1 0.8 0 1.1 0.5 0.7 0 0.3 0.5 0 0.3 # of stores and possible warehouse locations (x,y) location of stores $.5 (x,y,price) location of warehouses and their cost to build in Mega$ S delivery cost = Euclidean distance S S Output $.3 $.3 S $.8 Data Set 1: 2.32 The minimum total cost to supply all stores from some warehouse(s).
Convex Hull Problems… • read over these problems… • which ones are convex hull? • which ones could be convex hull? and the rest?
Current Jotto standings… Quine 5 Win! Sophs Jrs Srs Others icily 0 icily 0 icily 1 icily 1 strep 2 strep 2 strep 2 strep 1 spork 1 spork 3 spork 0 spork 0 spend 2 spend 2 spend 2 spend 2 peeps 2 peeps 1 peeps 2 peeps 1 furls 1 furls 1 furls 0 furls 1 Ghost 2 Ghost 1 Ghost 1 Ghost 0 Tanks 2 Tanks 1 Tanks 2 Tanks 1 Gecko 2 Gecko 1 Gecko 1 Gecko 1
What are these? public int mystery1(Point A, Point B, Point P) { int cp1 = (B.x-A.x)*(P.y-A.y) - (B.y-A.y)*(P.x-A.x); if (cp1>0) return 1; else return -1; } public int mystery2(Point A, Point B, Point C) { int ABx = B.x-A.x; int ABy = B.y-A.y; int num = ABx*(A.y-C.y)-ABy*(A.x-C.x); if (num < 0) num = -num; return num; }
What are these? public int mystery1(Point A, Point B, Point P) { int cp1 = (B.x-A.x)*(P.y-A.y) - (B.y-A.y)*(P.x-A.x); if (cp1>0) return 1; else return -1; } sortOfDistance Hints: whichSide public int mystery2(Point A, Point B, Point C) { int ABx = B.x-A.x; int ABy = B.y-A.y; int num = ABx*(A.y-C.y)-ABy*(A.x-C.x); if (num < 0) num = -num; return num; }