【发布时间】:2017-01-10 08:23:04
【问题描述】:
我必须计算多边形上的路径。 多边形点在 gps 坐标(lat、lng)中是已知的。
我这样做的方式: 在多边形周围创建一个矩形(找到最小纬度/经度和最大纬度/经度) 在这个矩形中,我从 min(lat,lng) 开始,一直走到直角。首先沿固定 x 方向移动,当达到最大 x 时,将固定 y 向上移动并返回。
所以这很好。但是我希望能够选择路径的(起始)角度。可能我需要旋转多边形,计算一个新的矩形并再次计算。但是我无法根据给定的角度进行旋转。
当前代码:
// calculate the rectangle
var bottomLeft =
{
lat: points[0].lat,
lng: points[0].lng
};
var topRight =
{
lat: points[0].lat,
lng: points[0].lng
};
for(var i = 1; i < points.length; i++)
{
if (points[i].lat < bottomLeft.lat)
{
bottomLeft.lat = points[i].lat;
}
else if (points[i].lat > topRight.lat)
{
topRight.lat = points[i].lat;
}
if (points[i].lng < bottomLeft.lng)
{
bottomLeft.lng = points[i].lng;
}
else if (points[i].lng > topRight.lng)
{
topRight.lng = points[i].lng;
}
}
//start position
var currentPosition =
{
lat: bottomLeft.lat,
lng: bottomLeft.lng
}
//calculate points
var outputPoints = [];
var direction = 0;
while (currentPosition.lat <= topRight.lat && currentPosition.lng <= topRight.lng)
{
if(pointInPoly(points, currentPosition))
{
outputPoints.push(copy(currentPosition));
}
if (direction == 0)
{
if(currentPosition.lat + stepX < topRight.lat)
{
currentPosition.lat += stepX;
}
else if(currentPosition.lng + stepY < topRight.lng)
{
currentPosition.lng += stepY;
direction = 1;
}
else
{
break;
}
}
else
{
if(currentPosition.lat - stepX > bottomLeft.lat)
{
currentPosition.lat -= stepX;
}
else if(currentPosition.lng + stepY < topRight.lng)
{
currentPosition.lng += stepY;
direction = 0;
}
else
{
break;
}
}
}
return outputPoints;
}
因此,用户应该能够给出一个角度,然后必须在计算网格时考虑该角度。尽管多边形可能需要平移/旋转才能进行计算,但显然多边形需要位于与其制作位置完全相同的位置,并且红线(路径)必须正好适合。
要将线添加到地图中,我使用以下代码:
function showLine(polygon) {
if(!isConvex(polygon))
{
console.log("cannot calculate outputPoints, polygon not convex");
return;
}
var outputPoints = calculatePoints(polygon._latlngs, 0.001,0.85,0.85)
var polyline = L.polyline(outputPoints, {color: 'red', opacity: 2, weight: 2});
polyline.addTo(Window.map);
console.log("done");
}
有什么想法可以做到这一点吗?
致以诚挚的问候,
拉克索
【问题讨论】:
标签: javascript rotation polygon