是否可以使用坐标计算图像地图上每个多边形区域的宽度和高度?
我有一张图片,并使用具有多个不同大小多边形的图像地图。我需要找到每个多边形的中心点。
是否可以使用坐标计算图像地图上每个多边形区域的宽度和高度?
我有一张图片,并使用具有多个不同大小多边形的图像地图。我需要找到每个多边形的中心点。
/* Calculate the centermost point of an array of areas
@param {element[]} areas an array of area elements
@returns {object} {x,y} coords of the center point
*/
function calculateCenterPoint(areas) {
var maxX = 0,
minX = Infinity,
maxY = 0,
minY = Infinity;
// note: using Array.prototype.forEach instead of calling forEach directly
// on "areas" so it will work with array-like objects, e.g. jQuery
Array.prototype.forEach.call(areas, function (e) {
var i = 0,
coords = e.getAttribute('coords').split(',');
while (i < coords.length) {
var x = parseInt(coords[i++],10),
y = parseInt(coords[i++],10);
if (x < minX) minX = x;
else if (x > maxX) maxX = x;
if (y < minY) minY = y;
else if (y > maxY) maxY = y;
}
});
return {
x: minX + (maxX - minX) / 2,
y: minY + (maxY - minY) / 2
};
}