Reviewed-on: #18 Co-authored-by: dutchie031 <timrorije@gmail.com>
This commit was merged in pull request #18.
This commit is contained in:
@@ -297,6 +297,49 @@ do -- INIT UTIL
|
||||
return false
|
||||
end
|
||||
|
||||
---@param points Array<Vec3>
|
||||
---@return Array<Vec3>
|
||||
function UTIL.getConvexHull3d(points)
|
||||
if #points == 0 then
|
||||
return {}
|
||||
end
|
||||
|
||||
---comment
|
||||
---@param a Vec3
|
||||
---@param b Vec3
|
||||
---@param c Vec3
|
||||
---@return boolean
|
||||
local function ccw(a, b, c)
|
||||
return (b.z - a.z) * (c.x - a.x) > (b.x - a.x) * (c.z - a.z)
|
||||
end
|
||||
|
||||
table.sort(points, function(left, right)
|
||||
return left.z < right.z
|
||||
end)
|
||||
|
||||
local hull = {}
|
||||
-- lower hull
|
||||
for _, point in pairs(points) do
|
||||
while #hull >= 2 and not ccw(hull[#hull - 1], hull[#hull], point) do
|
||||
table.remove(hull, #hull)
|
||||
end
|
||||
table.insert(hull, point)
|
||||
end
|
||||
|
||||
-- upper hull
|
||||
local t = #hull + 1
|
||||
for i = #points, 1, -1 do
|
||||
local point = points[i]
|
||||
while #hull >= t and not ccw(hull[#hull - 1], hull[#hull], point) do
|
||||
table.remove(hull, #hull)
|
||||
end
|
||||
table.insert(hull, point)
|
||||
end
|
||||
table.remove(hull, #hull)
|
||||
return hull
|
||||
end
|
||||
|
||||
|
||||
---comment
|
||||
---@param points Array<Vec2> points
|
||||
---@return Array<Vec2> hullPoints
|
||||
|
||||
Reference in New Issue
Block a user