我创建了一个充当点的小球模型立方体。这些点有坐标:
{0 <= x <= 9, 0 <= y <= -9, 0 <= z <= -9}
立方体的内部是空的,所以点只存在于立方体的表面。空白空间表示为 (100, 100, 100) 处的点,当我执行绘制循环时,我会忽略与它们匹配的点,这就是为什么在我将在下面发布的代码中将其作为执行某些操作的条件或不做。
目标是获取立方体的点,并对它们应用变换以将它们映射到球体上。
这是为立方体位置创建数组然后为球体位置创建数组的代码:
// initialize cube array
points = new Matrix[10, 10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
points[i, j, k] = Matrix.CreateTranslation(new Vector3(100, 100, 100));
}
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
points[i, j, 0] = Matrix.CreateTranslation(new Vector3(i, -j, 0));
points[i, j, 9] = Matrix.CreateTranslation(new Vector3(i, -j, -9));
}
}
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
points[0, j, k] = Matrix.CreateTranslation(new Vector3(0, -j, -k));
points[9, j, k] = Matrix.CreateTranslation(new Vector3(9, -j, -k));
}
}
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
points[i, 0, k] = Matrix.CreateTranslation(new Vector3(i, 0, -k));
points[i, 9, k] = Matrix.CreateTranslation(new Vector3(i, -9, -k));
}
}
// end cube array initialization
// create sphere array
double d;
double theta;
double phi;
double r = 10;
spherePoints = new Matrix[10, 10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
if (points[i, j, k] != Matrix.CreateTranslation(new Vector3(100, 100, 100)))
{
d = Math.Sqrt(Math.Pow(i, 2) + Math.Pow(-j, 2) + Math.Pow(-k, 2));
theta = Math.Acos(-k / d);
phi = Math.Atan2(-j, i);
spherePoints[i, j, k] = Matrix.CreateTranslation(new Vector3((float)(r * Math.Sin(theta) * Math.Cos(phi)),
(float)(r * Math.Sin(theta) * Math.Sin(phi)),
(float)(r * Math.Cos(theta))));
}
else
spherePoints[i, j, k] = Matrix.CreateTranslation(new Vector3(100, 100, 100));
}
}
}
// end creation of sphere array
立方体:
不是球体...:
据我所知,我完全遵循了这个公式,但它似乎只产生了一个球体的八分之一。边缘似乎也有奇怪的分组。