Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,88 +0,0 @@
|
|||
with Ada.Text_IO;
|
||||
with Ada.Containers.Vectors;
|
||||
|
||||
procedure Convex_Hull is
|
||||
|
||||
type Point is record
|
||||
X, Y : Integer;
|
||||
end record;
|
||||
|
||||
package Point_Vectors is
|
||||
new Ada.Containers.Vectors (Index_Type => Positive,
|
||||
Element_Type => Point);
|
||||
use Point_Vectors;
|
||||
|
||||
function Find_Convex_Hull (Vec : in Vector) return Vector is
|
||||
|
||||
function Counter_Clock_Wise (A, B, C : in Point) return Boolean is
|
||||
((B.X - A.X) * (C.Y - A.Y) > (B.Y - A.Y) * (C.X - A.X));
|
||||
|
||||
function Less_Than (Left, Right : Point) return Boolean is
|
||||
(Left.X < Right.X);
|
||||
|
||||
package Sorter is
|
||||
new Point_Vectors.Generic_Sorting (Less_Than);
|
||||
|
||||
Sorted : Vector := Vec;
|
||||
Result : Vector := Empty_vector;
|
||||
use type Ada.Containers.Count_Type;
|
||||
begin
|
||||
if Vec = Empty_Vector then
|
||||
return Empty_Vector;
|
||||
end if;
|
||||
|
||||
Sorter.Sort (Sorted);
|
||||
|
||||
-- Lower hull
|
||||
for Index in Sorted.First_Index .. Sorted.Last_Index loop
|
||||
while
|
||||
Result.Length >= 2 and then
|
||||
not Counter_Clock_Wise (Result (Result.Last_Index - 1),
|
||||
Result (Result.Last_Index),
|
||||
Sorted (Index))
|
||||
loop
|
||||
Result.Delete_Last;
|
||||
end loop;
|
||||
Result.Append (Sorted (Index));
|
||||
end loop;
|
||||
|
||||
-- Upper hull
|
||||
declare
|
||||
T : constant Ada.Containers.Count_Type := Result.Length + 1;
|
||||
begin
|
||||
for Index in reverse Sorted.First_Index .. Sorted.Last_Index loop
|
||||
while
|
||||
Result.Length >= T and then
|
||||
not Counter_Clock_Wise (Result (Result.Last_Index - 1),
|
||||
Result (Result.Last_Index),
|
||||
Sorted (Index))
|
||||
loop
|
||||
Result.Delete_Last;
|
||||
end loop;
|
||||
Result.Append (Sorted (Index));
|
||||
end loop;
|
||||
end;
|
||||
|
||||
Result.Delete_Last;
|
||||
return Result;
|
||||
end Find_Convex_Hull;
|
||||
|
||||
procedure Show (Vec : in Vector) is
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
Put ("[ ");
|
||||
for Point of Vec loop
|
||||
Put ("(" & Point.X'Image & "," & Point.Y'Image & ")");
|
||||
end loop;
|
||||
Put (" ]");
|
||||
end Show;
|
||||
|
||||
Vec : constant Vector :=
|
||||
(16, 3) & (12,17) & ( 0, 6) & (-4,-6) & (16, 6) &
|
||||
(16,-7) & (16,-3) & (17,-4) & ( 5,19) & (19,-8) &
|
||||
( 3,16) & (12,13) & ( 3,-4) & (17, 5) & (-3,15) &
|
||||
(-3,-9) & ( 0,11) & (-9,-3) & (-4,-2) & (12,10);
|
||||
begin
|
||||
Show (Find_Convex_Hull (Vec));
|
||||
Ada.Text_IO.New_Line;
|
||||
end Convex_Hull;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
define :point [x y][]
|
||||
define :point [x, y]
|
||||
|
||||
orientation: function [P Q R][
|
||||
val: sub (Q\y - P\y)*(R\x - Q\x) (Q\x - P\x)*(R\y - Q\y)
|
||||
|
|
@ -8,7 +8,7 @@ orientation: function [P Q R][
|
|||
]
|
||||
|
||||
calculateConvexHull: function [points][
|
||||
result: new []
|
||||
result: []
|
||||
|
||||
if 3 > size points [
|
||||
loop points 'p -> 'result ++ p
|
||||
|
|
@ -41,26 +41,26 @@ calculateConvexHull: function [points][
|
|||
return result
|
||||
]
|
||||
|
||||
points: @[
|
||||
to :point [16 3],
|
||||
to :point [12 17],
|
||||
to :point [0 6],
|
||||
to :point @[neg 4 neg 6],
|
||||
to :point [16 6],
|
||||
to :point @[16 neg 7],
|
||||
to :point @[17 neg 4],
|
||||
to :point [5 19],
|
||||
to :point @[19 neg 8],
|
||||
to :point [3 16],
|
||||
to :point [12 13],
|
||||
to :point @[3 neg 4],
|
||||
to :point [17 5],
|
||||
to :point @[neg 3 15],
|
||||
to :point @[neg 3 neg 9],
|
||||
to :point [0 11],
|
||||
to :point @[neg 9 neg 3],
|
||||
to :point @[neg 4 neg 2],
|
||||
to :point [12 10]
|
||||
points: to [:point] @[
|
||||
[16 3],
|
||||
[12 17],
|
||||
[0 6],
|
||||
@[neg 4 neg 6],
|
||||
[16 6],
|
||||
@[16 neg 7],
|
||||
@[17 neg 4],
|
||||
[5 19],
|
||||
@[19 neg 8],
|
||||
[3 16],
|
||||
[12 13],
|
||||
@[3 neg 4],
|
||||
[17 5],
|
||||
@[neg 3 15],
|
||||
@[neg 3 neg 9],
|
||||
[0 11],
|
||||
@[neg 9 neg 3],
|
||||
@[neg 4 neg 2],
|
||||
[12 10]
|
||||
]
|
||||
|
||||
hull: calculateConvexHull points
|
||||
|
|
|
|||
|
|
@ -1,93 +0,0 @@
|
|||
typedef Point = {x:Float, y:Float};
|
||||
|
||||
class Main {
|
||||
|
||||
// Calculate orientation for 3 points
|
||||
// 0 -> Straight line
|
||||
// 1 -> Clockwise
|
||||
// 2 -> Counterclockwise
|
||||
static function orientation(pt1:Point, pt2:Point, pt3:Point): Int
|
||||
{
|
||||
var val = ((pt2.x - pt1.x) * (pt3.y - pt1.y)) -
|
||||
((pt2.y - pt1.y) * (pt3.x - pt1.x));
|
||||
if (val == 0)
|
||||
return 0;
|
||||
else if (val > 0)
|
||||
return 1;
|
||||
else return 2;
|
||||
}
|
||||
|
||||
static function convexHull(pts:Array<Point>):Array<Point> {
|
||||
var result = new Array<Point>();
|
||||
|
||||
// There must be at least 3 points
|
||||
if (pts.length < 3)
|
||||
for (i in pts) result.push(i);
|
||||
|
||||
// Find the leftmost point
|
||||
var indexMinX = 0;
|
||||
for (i in 0...(pts.length - 1))
|
||||
if (pts[i].x < pts[indexMinX].x)
|
||||
indexMinX = i;
|
||||
|
||||
var p = indexMinX;
|
||||
var q = 0;
|
||||
|
||||
while (true) {
|
||||
// The leftmost point must be part of the hull.
|
||||
result.push(pts[p]);
|
||||
|
||||
q = (p + 1) % pts.length;
|
||||
|
||||
for (i in 0...(pts.length - 1))
|
||||
if (orientation(pts[p], pts[i], pts[q]) == 2) q = i;
|
||||
|
||||
p = q;
|
||||
|
||||
// Break from loop once we reach the first point again.
|
||||
if (p == indexMinX)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static function main() {
|
||||
var pts = new Array<Point>();
|
||||
pts.push({x: 16, y: 3});
|
||||
pts.push({x: 12, y: 17});
|
||||
pts.push({x: 0, y: 6});
|
||||
pts.push({x: -4, y: -6});
|
||||
pts.push({x: 16, y: 6});
|
||||
|
||||
pts.push({x: 16, y: -7});
|
||||
pts.push({x: 16, y: -3});
|
||||
pts.push({x: 17, y: -4});
|
||||
pts.push({x: 5, y: 19});
|
||||
pts.push({x: 19, y: -8});
|
||||
|
||||
pts.push({x: 3, y: 16});
|
||||
pts.push({x: 12, y: 13});
|
||||
pts.push({x: 3, y: -4});
|
||||
pts.push({x: 17, y: 5});
|
||||
pts.push({x: -3, y: 15});
|
||||
|
||||
pts.push({x: -3, y: -9});
|
||||
pts.push({x: 0, y: 11});
|
||||
pts.push({x: -9, y: -3});
|
||||
pts.push({x: -4, y: -2});
|
||||
pts.push({x: 12, y: 10});
|
||||
|
||||
var hull = convexHull(pts);
|
||||
Sys.print('Convex Hull: [');
|
||||
var length = hull.length;
|
||||
var i = 0;
|
||||
while (length > 0) {
|
||||
if (i > 0)
|
||||
Sys.print(', ');
|
||||
Sys.print('(${hull[i].x}, ${hull[i].y})');
|
||||
length--;
|
||||
i++;
|
||||
}
|
||||
Sys.println(']');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue