嗨,我有一个带坐标的多边形
[
[-73.9280684530257, 40.8099975343718],
[-73.9282820374729, 40.8100875554645],
[-73.9280124002104, 40.8103130893677],
[-73.927875543761, 40.8102554080229],
[-73.9280684530257, 40.8099975343718]
]
如何使用javascript查找此多边形每边的中心?
答案 0 :(得分:1)
你可以通过取平均值来占据中心位置。
var polygon = [[-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], [-73.9280124002104, 40.8103130893677], [-73.927875543761, 40.8102554080229], [-73.9280684530257, 40.8099975343718]],
centers = polygon.map(function (point, i, points) {
var neighbour = points[(i + 1) % points.length];
return point.map(function (value, j) {
return (value + neighbour[j]) / 2;
});
});
console.log(centers);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)
另一种解决方案可以是这种形式
var vertexArray = [[-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], [-73.9280124002104, 40.8103130893677], [-73.927875543761, 40.8102554080229], [-73.9280684530257, 40.8099975343718]];
var centerArray = [];
for(i=0;i<vertexArray .length;i++){
nextIndex = (i+1)%vertexArray.length;
centerArray[i] = [] ;
centerArray[i][0] = (vertexArray [i][0] + vertexArray[nextIndex][0])/2;
centerArray[i][1] = (vertexArray[i][1] + vertexArray[nextIndex][1])/2
}
console.log(centerArray);
&#13;