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

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;


心得 :

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


2013年2月19日 星期二

<已解決><問題>嘗試在GH 裡用使用C#找出最短的路徑



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

檔案 : 

https://dl.dropbox.com/u/29860061/the%20closest%20point%20problem.gh

流程步驟 : 

1.散佈隨機點在範圍內,
   設index 0 為起始點,
   用index 0 去跟除了自己的點搜尋,找到最近距離,並抽取最近點的index
   把 "起始點(initial Point)" 與 "最近點(closest Point)"連線
注意: 比較距離時,當前點不用與自身相比較,因為與自身比較出來的數值一定是0,也是最小,沒意義。

2. 使用"最近點(closest Point)"搜尋最近點,並連線,找到"下個點(next Point)"
注意: 比較距離時,也不用與使用過的點作比較,因為目的是把最近點找出並當成下條連線的起點。例如: 如圖所示index 0 找到最近點 index 9 ,如果index 9 又與除了自身的其他點比較,將會找到 index0,沒意義。

3. 替換"下個點(next Point)" 當成 "最近點(closest Point)",目的是把"下個點(next Point)"
    做為下次連線的起點

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

問題 :

在把使用過的index value 取出作檢查時,程式出現問題"索引超出範圍",希望哪位牛哥妙麗可以幫在下解決一下問題!!! 感激不盡。