我正在尝试计算长方体中的点,给定它的中心(这是一个 Vector3)以及沿 x、y 和 z 轴的边的长度。我在 math.stackexchange.com 上找到了以下内容:https ://math.stackexchange.com/questions/107778/simplest-equation-for-drawing-a-cube-based-on-its-center-and-or-other -vertices表示我可以使用以下公式:
World 类的构造函数是:
World::World(Vector3 o, float d1, float d2, float d3) : origin(o)
{
// If we consider an edge length to be d, we need to find r such that
// 2r = d in order to calculate the positions of each vertex in the world.
float r1 = d1 / 2,
r2 = d2 / 2,
r3 = d3 / 2;
for (int i = 0; i < 8; i++)
{
/* Sets up the vertices of the cube.
*
* @see http://bit.ly/1cc2RPG
*/
float x = o.getX() + (std::pow(-1, i&1) * r1),
y = o.getY() + (std::pow(-1, i&2) * r2),
z = o.getZ() + (std::pow(-1, i&4) * r3);
points[i] = Vector3(x, y, z);
std::cout << points[i] << "\n";
}
}
我将以下参数传递给构造函数:
Vector3 o(0, 0, 0);
World w(o, 100.f, 100.f, 100.f);
所有 8 个顶点的输出坐标为:
(50, 50, 50)
(-50, 50, 50)
(50, 50, 50)
(-50, 50, 50)
(50, 50, 50)
(-50, 50, 50)
(50, 50, 50)
(-50, 50, 50)
这不可能是正确的。任何指导将不胜感激!