2013年2月26日 星期二

<分享>使用GH C#找出最近點連線並檢查是否交集


目標 : 使用GH C#找出最近點連線並檢查是否交集。

檔案 : 

https://dl.dropbox.com/u/29860061/closet%20line%20with%20intersction%20check.gh

流程步驟 : 

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

2.使用基準點(basePoint)與新點雲(calNewCloudPts2)搜尋最近點,並儲存當下indexNumber (calNewCloudPts2[closePtIndex])。
public int calculateDist(List < Point3d > calPts, Point3d startPt){...}

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

4.檢查最新連線(myLine)是否與舊有線段(mylnList)相互交集,如果有交集->不連線,如果沒交集->產生新線段,並放入LIST當中。
public bool checkInt(List<Line> lnA, Line lnB){...}

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

initialnum = closePtIndex;
x = calNewCloudPts2;


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

Main Code:

 private void RunScript(List<Point3d> x, Point3d y, ref object A)
  {

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

    //the initial index is 0
    int initialnum = 0;
    int closePtIndex = 0;
    int totaloldPtsLength = x.Count - 1;
    Line myLine;
    bool mytest = false;




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

      basePoint = x[initialnum];
      calNewCloudPts2 = calNewCloudPts(initialnum, x);
      closePtIndex = calculateDist(calNewCloudPts2, basePoint);
      //Print("closePtIndex  = {0}", closePtIndex);
      myLine = LinkLn(basePoint, calNewCloudPts2[closePtIndex]);

      mytest = checkInt(mylnList, myLine);

      Print("ff = {0}", mytest);
      if(mytest == false){
        mylnList.Add(myLine);

      }

      initialnum = closePtIndex;
      x = calNewCloudPts2;
      Print("calNewCloudPts2 List = {0}", calNewCloudPts2.Count.ToString());

    }

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


    A = mylnList;



  }

結果圖片:

points number - > 100 
 points number - > 500 
 points number - > 1000



心得:

下次將會發布 recursion and timer test

沒有留言:

張貼留言