【问题标题】:calculation on rotated polygon旋转多边形的计算
【发布时间】:2017-01-10 08:23:04
【问题描述】:

我必须计算多边形上的路径。 多边形点在 gps 坐标(lat、lng)中是已知的。

我这样做的方式: 在多边形周围创建一个矩形(找到最小纬度/经度和最大纬度/经度) 在这个矩形中,我从 min(lat,lng) 开始,一直走到直角。首先沿固定 x 方向移动,当达到最大 x 时,将固定 y 向上移动并返回。

example.

所以这很好。但是我希望能够选择路径的(起始)角度。可能我需要旋转多边形,计算一个新的矩形并再次计算。但是我无法根据给定的角度进行旋转。

当前代码:

// 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


    【解决方案1】:

    您必须先增加 x 再增加 y,而不是增加一个取决于您的角度的值。

    x += stepX

    变成

    x += stepX * cos(angle) y += stepX * sin(angle)

    y += stepY

    变成

    x -= stepY * sin(angle) y += stepY * cos(angle)

    【讨论】:

    • 我想过这个,但我不确定起点在哪里。 (为了覆盖整个多边形)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多