ConvexHull
Package: Libraries.Fabrication.Geometry · Inherits from: Libraries.Language.Object
The ConvexHull class finds the convex hull of a set of 2D points with Andrew's monotone chain algorithm: duplicate points are dropped, the rest sorted by x then y (merge sort), and the lower and upper hulls built in one pass each. The hull comes back counter-clockwise, without repeating its first point.
Example Code
use Libraries.Containers.Number64BitArray
use Libraries.Fabrication.Geometry.ConvexHull
class Main
action Main
Number64BitArray xs
Number64BitArray ys
xs:SetSize(4)
ys:SetSize(4)
xs:Set(1, 10)
xs:Set(2, 10)
ys:Set(2, 10)
xs:Set(3, 5)
ys:Set(3, 2) // inside the triangle of the other three
ConvexHull hull
hull:Compute(xs, ys)
output hull:GetSize() // 3
end
end
Actions
Compute(Libraries.Containers.Number64BitArray xs, Libraries.Containers.Number64BitArray ys)
Computes the hull of the points (xs:Get(i), ys:Get(i)).
Parameters
xsLibraries.Containers.Number64BitArray: The x coordinates.ysLibraries.Containers.Number64BitArray: The y coordinates, as many as xs.
Example
use Libraries.Containers.Number64BitArray
use Libraries.Fabrication.Geometry.ConvexHull
Number64BitArray xs
Number64BitArray ys
xs:SetSize(5)
ys:SetSize(5)
xs:Set(1, 10)
xs:Set(2, 10)
ys:Set(2, 10)
ys:Set(3, 10)
xs:Set(4, 5)
ys:Set(4, 5) // inside the square of the other four
ConvexHull hull
hull:Compute(xs, ys)
output hull:GetSize() // 4
GetConvexX()
The hull's x coordinates, counter-clockwise.
Returns
Libraries.Containers.Number64BitArray: The hull's x coordinates, counter-clockwise.
Example
use Libraries.Containers.Number64BitArray
use Libraries.Fabrication.Geometry.ConvexHull
Number64BitArray xs
Number64BitArray ys
xs:SetSize(3)
ys:SetSize(3)
xs:Set(1, 4)
ys:Set(2, 3)
ConvexHull hull
hull:Compute(xs, ys)
Number64BitArray hullX = hull:GetConvexX()
Number64BitArray hullY = hull:GetConvexY()
integer i = 0
repeat while i < hullX:GetSize()
output hullX:Get(i) + ", " + hullY:Get(i)
i = i + 1
end
GetConvexY()
The hull's y coordinates, counter-clockwise.
Returns
Libraries.Containers.Number64BitArray: The hull's y coordinates, counter-clockwise.
Example
use Libraries.Containers.Number64BitArray
use Libraries.Fabrication.Geometry.ConvexHull
Number64BitArray xs
Number64BitArray ys
xs:SetSize(3)
ys:SetSize(3)
xs:Set(1, 4)
ys:Set(2, 3)
ConvexHull hull
hull:Compute(xs, ys)
Number64BitArray hullY = hull:GetConvexY()
output hullY:Get(0) // 0: the hull starts at the lowest left point
GetSize()
The number of points on the hull found by the last Compute.
Returns
integer: The number of points on the hull found by the last Compute.
Example
use Libraries.Fabrication.Geometry.ConvexHull
ConvexHull hull
output hull:GetSize() // 0 before Compute