在 3D 坐标中旋转的 2D 矩形
2D Rectangle rotated in 3D coordinates
我有一个 2D 矩形,其中 4 个点以逆时针方式排列 - 例如point 0(x0,y0), point 1(x1, y1), etc.I 想知道如何在 3D 中旋转这些点中的每一个 space(即使 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;
}
上面的代码对我想做的事情是否正确?
在此先感谢您的帮助。
我觉得你的实现不正确。如果我是你,我只会编写一个函数,用于通过指向大致方向的坐标系原点绕任何轴旋转。然后,您可以根据需要选择具体的方向。这里是python中的代码(更简洁,思路更清晰),你可以用C++实现。
import numpy as np
import math
def rotation(axis, angle, Vector):
'''
axis should be a unit vector!
'''
axis_X_Vector = np.cross(axis, V)
rotated_Vector = Vector
rotated_Vector = rotated_Vector + math.sin(angle)*axis_X_Vector
rotated_Vector = rotated_Vector + (1 - math.cos(angle))*np.cross(axis, axis_X_Vector)
return rotated_Vector
我有一个 2D 矩形,其中 4 个点以逆时针方式排列 - 例如point 0(x0,y0), point 1(x1, y1), etc.I 想知道如何在 3D 中旋转这些点中的每一个 space(即使 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;
}
上面的代码对我想做的事情是否正确?
在此先感谢您的帮助。
我觉得你的实现不正确。如果我是你,我只会编写一个函数,用于通过指向大致方向的坐标系原点绕任何轴旋转。然后,您可以根据需要选择具体的方向。这里是python中的代码(更简洁,思路更清晰),你可以用C++实现。
import numpy as np
import math
def rotation(axis, angle, Vector):
'''
axis should be a unit vector!
'''
axis_X_Vector = np.cross(axis, V)
rotated_Vector = Vector
rotated_Vector = rotated_Vector + math.sin(angle)*axis_X_Vector
rotated_Vector = rotated_Vector + (1 - math.cos(angle))*np.cross(axis, axis_X_Vector)
return rotated_Vector