给定一个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) 我想使用Polygon类和lat/lng坐标数组在Google Maps上绘制城区区域.
OpenStreetMap为我提供了我需要的所有数据 - 如果我输入一些区域名称,我可以获得OSM XML格式的有用数据,例如拉脱维亚里加的"Vecmilgravis"区的OSM绘制多边形,以及OSM XML格式的数据.
问题是所有这些node节点都按照一些奇怪的顺序排序,所以如果我只提取所有lat和lng对并为Google Maps Polygon类创建一个坐标数组,我看不到我所期望的:
标记正确显示'cos坐标顺序对他们来说并不重要,但是多边形被弄乱'错误的坐标顺序我从OSM数据中复制了.
那么,如何以正确的顺序提取(或手动排序)OSM节点坐标?