我在地圖上繪製了一條路線(線條)。對於這條路線,我有2個點,起點和終點說(x1,y1)和(x2,y2)。找出使用中點公式的線條上的所有點
我需要找出線上的所有點。我正在使用中點公式。 以下是相同的代碼。
private void GetAllPoints(GraphLocation start, GraphLocation end)
{
GraphLocation midPoint = GetMidPoint(start, end);
allPoints.Add(midPoint);
while (start.Latitude != midPoint.Latitude && start.Longitude != midPoint.Longitude)
{
GraphLocation point = GetMidPoint(start, midPoint);
midPoint = point;
allPoints.Add(point);
}
}
private GraphLocation GetMidPoint(GraphLocation start, GraphLocation end)
{
GraphLocation midPoint = new GraphLocation();
double dLon = DegreesToRadians(end.Longitude - start.Longitude);
double Bx = Math.Cos(DegreesToRadians(end.Latitude)) * Math.Cos(dLon);
double By = Math.Cos(DegreesToRadians(end.Latitude)) * Math.Sin(dLon);
midPoint.Latitude = RadiansToDegrees(Math.Atan2(
Math.Sin(DegreesToRadians(start.Latitude)) + Math.Sin(DegreesToRadians(end.Latitude)),
Math.Sqrt(
(Math.Cos(DegreesToRadians(start.Latitude)) + Bx) *
(Math.Cos(DegreesToRadians(start.Latitude)) + Bx) + By * By)));
midPoint.Longitude = start.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(start.Latitude)) + Bx));
allPoints.Add(midPoint);
return midPoint;
}
此代碼跳過路線的某些部分。
請幫我拿到理想的結果。
方程有問題找出所有的點在一行或代碼? – matcheek
一條線有無窮多個點......究竟是什麼*是期望的結果? –
我嘗試一種方法,除非所有點都被覆蓋,否則遞歸地使用中點公式來斷開線。我無法弄清楚停車情況。 –