给定一个x,y点数组,如何按顺时针顺序(围绕它们的整体平均中心点)对该数组的点进行排序?我的目标是将点传递给线创建函数,以最终看起来相当"坚实"的东西,尽可能凸起,没有相交的线.
为了它的价值,我正在使用Lua,但任何伪代码都会受到赞赏.非常感谢您的帮助!
更新:作为参考,这是基于Ciamej优秀答案的Lua代码(忽略我的"app"前缀):
function appSortPointsClockwise(points)
local centerPoint = appGetCenterPointOfPoints(points)
app.pointsCenterPoint = centerPoint
table.sort(points, appGetIsLess)
return points
end
function appGetIsLess(a, b)
local center = app.pointsCenterPoint
if a.x >= 0 and b.x < 0 then return true
elseif a.x == 0 and b.x == 0 then return a.y > b.y
end
local det = (a.x - center.x) * (b.y - center.y) - (b.x - center.x) * (a.y - center.y)
if det < 0 then return true
elseif det > 0 then …Run Code Online (Sandbox Code Playgroud) 给出以下路径(例如)描述SVG三次贝塞尔曲线:"M300,140C300,40,500,40,500,140",并假设连接端点300,140到500,140的直线(关闭曲线下面积),是否可能计算如此封闭的面积?
任何人都可以建议一个公式(或JavaScript)来实现这一目标吗?