Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Convex-hull/Zkl/convex-hull-1.zkl
Normal file
32
Task/Convex-hull/Zkl/convex-hull-1.zkl
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Use Graham Scan to sort points into a convex hull
|
||||
// https://en.wikipedia.org/wiki/Graham_scan, O(n log n)
|
||||
// http://www.geeksforgeeks.org/convex-hull-set-2-graham-scan/
|
||||
// http://geomalgorithms.com/a10-_hull-1.html
|
||||
fcn grahamScan(points){
|
||||
N:=points.len();
|
||||
# find the point with the lowest y-coordinate, x is tie breaker
|
||||
p0:=points.reduce(fcn([(a,b)]ab,[(x,y)]xy){
|
||||
if(b<y)ab else if(b==y and a<x)ab else xy });
|
||||
#sort points by polar angle with p0, ie ccw from p0
|
||||
points.sort('wrap(p1,p2){ ccw(p0,p1,p2)>0 });
|
||||
|
||||
# We want points[0] to be a sentinel point that will stop the loop.
|
||||
points.insert(0,points[-1]);
|
||||
M:=1; # M will denote the number of points on the convex hull.
|
||||
foreach i in ([2..N]){
|
||||
# Find next valid point on convex hull.
|
||||
while(ccw(points[M-1], points[M], points[i])<=0){
|
||||
if(M>1) M-=1;
|
||||
else if(i==N) break; # All points are collinear
|
||||
else i+=1;
|
||||
}
|
||||
points.swap(M+=1,i); # Update M and swap points[i] to the correct place.
|
||||
}
|
||||
points[0,M]
|
||||
}
|
||||
# Three points are a counter-clockwise turn if ccw > 0, clockwise if
|
||||
# ccw < 0, and collinear if ccw = 0 because ccw is a determinant that
|
||||
# gives twice the signed area of the triangle formed by p1, p2 and p3.
|
||||
fcn ccw(a,b,c){ // a,b,c are points: (x,y)
|
||||
((b[0] - a[0])*(c[1] - a[1])) - ((b[1] - a[1])*(c[0] - a[0]))
|
||||
}
|
||||
7
Task/Convex-hull/Zkl/convex-hull-2.zkl
Normal file
7
Task/Convex-hull/Zkl/convex-hull-2.zkl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
pts:=List( T(16,3), T(12,17), T(0,6), T(-4,-6), T(16,6),
|
||||
T(16, -7), T(16,-3),T(17,-4), T(5,19), T(19,-8),
|
||||
T(3,16), T(12,13), T(3,-4), T(17,5), T(-3,15),
|
||||
T(-3,-9), T(0,11), T(-9,-3), T(-4,-2), T(12,10), )
|
||||
.apply(fcn(xy){ xy.apply("toFloat") }).copy();
|
||||
hull:=grahamScan(pts);
|
||||
println("Convex Hull (%d points): %s".fmt(hull.len(),hull.toString(*)));
|
||||
Loading…
Add table
Add a link
Reference in a new issue