将多边形沿边缘对齐

我尝试了将多边形对齐以实现以下效果:

但我得到的结果是这样的:

local function signedArea(p, q, r)
    local cross = (q.y - p.y) * (r.x - q.x)
                - (q.x - p.x) * (r.y - q.y)
    return cross
end

local function isCCW(p, q, r) return signedArea(p, q, r) < 0 end

local function jarvis_gift_wrapping_algorithm(points)
    local numPoints = #points
    if numPoints < 3 then return end

    local leftMostPointIndex = 1
    for i = 1, numPoints do
        if points[i].x < points[leftMostPointIndex].x then
            leftMostPointIndex = i
        end
    end

    local p = leftMostPointIndex
    local hull = {}

    repeat
        q = points[p + 1] and p + 1 or 1
        for i = 1, numPoints, 1 do
            if isCCW(points[p], points[i], points[q]) then q = i end
        end

        table.insert(hull, points[q])
        p = q
    until (p == leftMostPointIndex)

    return hull
end

这基本上就是这样:https://github.com/kennyledet/Algorithm-Implementations/blob/master/Convex_hull/Lua/Yonaba/convex_hull.lua

如果有任何想法或知道任何可能帮助我实现这一目标的算法,我将非常感谢。

点赞