【发布时间】:2021-05-31 14:53:20
【问题描述】:
我定义了一个 2D 矩形,其中 4 个点按逆时针方向排列 - 例如点 0(x0,y0)、点 1(x1, y1) 等。我想知道如何在 3D 空间中旋转这些点(即使 Rectangle 是 2D)。
我想随机选择要旋转的轴(x、y 或 z)。类似于以下 C++ 代码的行对于矩形中的每个点:
struct Point { float x, y; };
// Rotate around X-Axis
// pt is current point in Rectangle
// rz is randomly chosen z-coordinate value between [-1,1]
void rotateXaxis(Point &p, angle, float rz) {
float rads = PI * angle / 180.0;
float ry = p.y*cos(rads) + rz*sin(rads);
p.y = ry;
}
// Rotate around Y-Axis
// pt is current point in Rectangle
// rz is randomly chosen z-coordinate value between [-1,1]
void rotateXaxis(Point &p, angle, float rz) {
float rads = PI * angle / 180.0;
float rx = rz*sin(rads) + p.x*cos(rads);
p.x = rx;
}
// Rotate around Z-Axis
// pt is current point in Rectangle
// rz is randomly chosen z-coordinate value between [-1,1]
void rotateZaxis(Point &p, angle, float rz) {
float rads = PI * angle / 180.0;
rx = p.x*math.cos(rads) - p.y*math.sin(rads);
ry = p.x*math.sin(rads) + p.y*math.cos(rads);
p.x = rx;
p.y = ry;
}
上面的代码对我想做的事情是否正确?
感谢您的任何帮助。
【问题讨论】:
标签: c++ rotation computational-geometry