|
本帖最后由 weiwei 于 2013-2-21 23:28 编辑
目標 : 嘗試在GH 裡用使用C#找出最短的路徑。
blog :
http://lochengwei.blogspot.com/2013/02/gh-c_21.html
流程步驟 :
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;
流程圖片:
|
|