2011-04-09 143 views
-1

嗨,我只是寫在C#函數生成座標的立方體,但我想只是生成座標立方體

產生立方體雙方不是在深度座標的問題!

static class Util 
{ 
    public static List<string> GenerateCubeCoord(double bc,int nt,double stp) 
    { 
     List<string> list = new List<string>(); 
     double CoorBase = bc; 
     int n = nt; 
     double step = stp; 
     int id = 1; 
     for (int x = 0; x < n; x++) 
     { 
      for (int y = 0; y < n; y++) 
      { 
       for (int z = 0; z < n; z++) 
       { 
        list.Add(string.Format("GRID {0} {1}.0 {2}.0 {3}.0 \n", 
         id, step * x + CoorBase, step * y + CoorBase, step * z + CoorBase)); 

        id++; 
       } 
      } 
     } 

     return list; 
    } 

}

我笏生成該所有的座標不是立方體的頂點座標,圖像中的一個

側可立方體

enter image description here

+0

你是什麼意思「我想只生成立方體邊的座標不深入」。 U代表3x3x3立方體的程序應該只打印8個角點的座標? – BiGYaN 2011-04-09 19:13:43

+0

恩,需要更多信息。 – Carra 2011-04-09 19:15:13

回答

2

不改變你代碼太多(假設你的意思是所有的角點,這有點不清楚):

for (int x = 0; x <= n; x += n) 
    for (int y = 0; y <= n; y += n) 
     for (int z = 0; z <= n; z += n) 
      Console.WriteLine("{0} {1} {2}", x, y, z); 

使用LINQ乾淨了一點:

int n = 6; 
var coords = from x in new[] { 0, n } 
      from y in new[] { 0, n } 
      from z in new[] { 0, n } 
      select new { x, y, z }; 

foreach(var coord in coords) 
    Console.WriteLine("{0} {1} {2}", coord.x, coord.y, coord.z); 

編輯更新後的問題:

如果你只是想邊的座標,對於一個允許值座標(x,y或Z)是0或正1:

var coords = from x in new[] { 0, n-1 } 
      from y in Enumerable.Range(0, n) 
      from z in Enumerable.Range(0, n) 
      select new { x, y, z }; 

沖洗和重複其他兩個,你必須在S等所有6邊的座標。

編輯:

隨着上述方案有不同的側面(邊緣點)之間的重疊,所以你必須使用所有3分集的聯合。更好的解決方案是一次查詢所有座標:

var coords = from x in Enumerable.Range(0, n) 
      from y in Enumerable.Range(0, n) 
      from z in Enumerable.Range(0, n) 
      where (x == 0 || x==n-1 || y == 0 || y== n-1 || z == 0 || z== n-1) 
      select new { x, y, z }; 
+0

感謝LINQ看起來更有趣。 – 2011-04-09 19:54:04