2011-04-09 153 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