在JavaScript中查找多边形的中心点 [英] Find centerpoint of polygon in JavaScript
本文介绍了在JavaScript中查找多边形的中心点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了下面的代码来找到中心点,但我不确定它是否真的产生了中心点。如果多边形有5点而不是4点会怎么样?此外,这可以通过更有效的方式完成,操作更少吗?
函数average(array){
//数组,然后除以长度
return _.reduce(array,function(sum,num){
return sum + num;
},0)/ array.length;
}
//我有一个二维数组,我想得到
$ b的平均值var coords = [
[-1.2, 5.1],
[-1.3,5.2],
[-1.8,5.9],
[-1.9,5.8]
]
//所以我得到第一列
var lats = coords.map(function(coord){
return coord [0];
})
//然后第二个
var longs = coords.map(function(coord){
return coord [1];
})
/ /并且每列的平均值为
console.log([average(lats),average(longs)])
示例。
解决方案
这应该得到质心任何区域。 Polygonrel =noreferrer> polygon
/ * jslint sub: true,maxerr:50,indent:4,browser:true * /
/ *全局控制台* /
(function(){
use strict;
函数Point(x,y){
this.x = x;
this.y = y;
}
函数Region(points){
this.points = points || [];
this.length = points.length;
}
Region.prototype.area = function(){
var area = 0,
i,
j,
point1,
point2; (i = 0,j = this.length-1; i< this.length; j = i,i ++){
point1 = this.points [i];
。
point2 = this.points [j];
area + = point1.x * point2.y;
area - = point1.y * point2.x;
}
area / = 2;
返回区域;
};
Region.prototype.centroid = function(){
var x = 0,
y = 0,
i,
j,
f,
point1,
point2; (i = 0,j = this.length-1; i< this.length; j = i,i ++){
point1 = this.points [i];
。
point2 = this.points [j];
f = point1.x * point2.y - point2.x * point1.y;
x + =(point1.x + point2.x)* f;
y + =(point1.y + point2.y)* f;
}
f = this.area()* 6;
返回新的Point(x / f,y / f);
};
var polygon = [
{x:-1.2,y:5.1},
{x:-1.3,y:5.2},
{x:-1.8,y:5.9},
{x:-1.9,y:5.8}
],
region = new区域(多边形);
console.log(region.centroid());
}());
在 jsfiddle
I have a "place" object from Google Maps which has a set of coordinates that represent a bounding box for a given location, say London. Each set of coordinates has a latitude and longitude.
I have written the below code to find the centerpoint, but I am not sure if it does actually produce the centerpoint. What if the polygon has 5 points instead of 4? Also, can this be done in a more efficient way, with less operations?
function average(array) {
// Add together and then divide by the length
return _.reduce(array, function (sum, num) {
return sum + num;
}, 0) / array.length;
}
// I have a two-dimensional array that I want to get the average of
var coords = [
[ -1.2, 5.1 ],
[ -1.3, 5.2 ],
[ -1.8, 5.9 ],
[ -1.9, 5.8 ]
]
// So I get the first column
var lats = coords.map(function (coord) {
return coord[0];
})
// Then the second
var longs = coords.map(function (coord) {
return coord[1];
})
// And average each column out
console.log([average(lats), average(longs)])
解决方案
This should get the centroid of the area of any polygon
/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global console */
(function () {
"use strict";
function Point(x, y) {
this.x = x;
this.y = y;
}
function Region(points) {
this.points = points || [];
this.length = points.length;
}
Region.prototype.area = function () {
var area = 0,
i,
j,
point1,
point2;
for (i = 0, j = this.length - 1; i < this.length; j=i,i++) {
point1 = this.points[i];
point2 = this.points[j];
area += point1.x * point2.y;
area -= point1.y * point2.x;
}
area /= 2;
return area;
};
Region.prototype.centroid = function () {
var x = 0,
y = 0,
i,
j,
f,
point1,
point2;
for (i = 0, j = this.length - 1; i < this.length; j=i,i++) {
point1 = this.points[i];
point2 = this.points[j];
f = point1.x * point2.y - point2.x * point1.y;
x += (point1.x + point2.x) * f;
y += (point1.y + point2.y) * f;
}
f = this.area() * 6;
return new Point(x / f, y / f);
};
var polygon = [
{"x": -1.2, "y": 5.1},
{"x": -1.3, "y": 5.2},
{"x": -1.8, "y": 5.9},
{"x": -1.9, "y": 5.8}
],
region = new Region(polygon);
console.log(region.centroid());
}());
On jsfiddle
这篇关于在JavaScript中查找多边形的中心点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文