2013年2月21日 星期四

<分享>嘗試在GH 裡用使用C#找出最短的路徑


目標 : 嘗試在GH 裡用使用C#找出最短的路徑。

檔案 : 

https://dl.dropbox.com/u/29860061/find%20the%20closest%20point%20and%20made%20line.gh

流程步驟 : 

1.散佈隨機點在範圍內,設index 0 為起始點。
使用舊點雲(intput x)計算出基準點(basePoint)與新點雲(calNewCloudPts2)。
新點雲中不包含基準點(basePoint)。
public List<Point3d> calNewCloudPts(int basePtIndex, List<Point3d> oldCloudPts){...}

2. 使用基準點(basePoint)與新點雲(calNewCloudPts2)搜尋最近點,並連線找到最近點(calNewCloudPts2[closePtIndex])。
public int calculateDist(List < Point3d > calPts, Point3d startPt){...}

3. 使用基準點(basePoint)與最近點(calNewCloudPts2[closePtIndex])相連線。
public Line LinkLn(Point3d startPt, Point3d endPt){...}

4. 更新基準點(basePoint)與最近點(calNewCloudPts2[closePtIndex])。

initialnum = closePtIndex;
x = calNewCloudPts2;


5.重複1-3步驟,完成最近距離連線

流程圖片:




Main Code:
    List < Line > mylnList = new List < Line >();
    List<Point3d> calNewCloudPts2 = new List<Point3d>();
    Point3d basePoint = new Point3d();

    int closePtIndex = 0;

    //the initial index is 0
    int initialnum = 0;
    Print("x List = {0}", x.Count.ToString());

    int totaloldPtsLength = x.Count - 1;

    for(int i = 0;i < totaloldPtsLength;i++){

      basePoint = x[initialnum];
      calNewCloudPts2 = calNewCloudPts(initialnum, x);
      closePtIndex = calculateDist(calNewCloudPts2, basePoint);
      mylnList.Add(LinkLn(basePoint, calNewCloudPts2[closePtIndex]));
      initialnum = closePtIndex;
      x = calNewCloudPts2;
      Print("calNewCloudPts2 List = {0}", calNewCloudPts2.Count.ToString());

    }

    //Print("x count = {0}", x.Count.ToString());


    A = mylnList;


心得 :

連續下班後,搞了四天才成功,換了很多不同的方法,最後發現這寫法最直接。
歡迎交流。
下個流程可能進展到線段與線段不能交集,並找到最短路徑,製作出像樹枝的紋理。


沒有留言:

張貼留言