gpt4 book ai didi

C++ 长方体顶点的点(按位与)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:49 24 4
gpt4 key购买 nike

我试图在给定中心(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它说我可以使用以下公式:

https://math.stackexchange.com/questions/107778/simplest-equation-for-drawing-a-cube-based-on-its-center-and-or-other-verticesWorld 类的构造函数是:

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)

这不可能是正确的。非常感谢任何指导!

最佳答案

问题在于 pow 调用中的按位 &:在 yz 组件中,它们总是分别返回 0 和 2 或 4。 -1^2 = -1^4 = 1,这就是为什么这些分量的符号总是正的。您可以尝试 (i&2)!=0(i&2) >> 1 作为 y 分量。 z 组件也是如此。

关于C++ 长方体顶点的点(按位与),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22222035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com