2014-01-14 80 views
1

我在地圖上繪製了一條路線(線條)。對於這條路線,我有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; 
} 

此代碼跳過路線的某些部分。

請幫我拿到理想的結果。

+0

方程有問題找出所有的點在一行或代碼? – matcheek

+0

一條線有無窮多個點......究竟是什麼*是期望的結果? –

+0

我嘗試一種方法,除非所有點都被覆蓋,否則遞歸地使用中點公式來斷開線。我無法弄清楚停車情況。 –

回答

0

看起來你只是在行的前半部分計算中點,然後每次平分它,因爲你只是在開始和中間進行計算。 GraphLocation point = GetMidPoint(start, midPoint); 如何調用GetMidPoint()遞歸?停止條件是限制遞歸的深度,所以你沒有得到很多的要點。

+0

是的,你是對的,存在我的問題。我無法找出遞歸的停止條件。 –

+0

爲什麼不給一個整數的例如。 2000到GetMidPoint()的第一個調用。每次遞歸越深,你減1,直到你達到0? – Matthias