Generate coordinates for cube
Hi i just write function in C# that generate coordinate for cube but the problem i want just
generate the coordinate of cube sides not in the depth !!!
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;
}
}
I wat to generate all this coordinate not the corner coordinates of cube , in the image one
side of may cube
Without changing your code too much (assuming you meant all corner points, it's a little unclear):
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);
A little cleaner using 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);
Edit after updated question:
If you just want the coordinates of the sides, the allowed values for one coordinate (x,y or z) are either 0 or n-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 };
Rinse and repeat for the other two and you have the set of coordinates for all 6 sides.
Edit:
With above solution there are overlaps between the different sides (the edge points), so you'd have to use the union of all 3 collections. A better solution is to query for all coordinates in one go:
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 };
精彩评论