旋转函数通常用于对对象(图形、图像、数据等)进行旋转操作。在编程中,旋转函数的核心是应用旋转矩阵或旋转公式来改变对象的坐标或方向。
// 旋转一个点(x,y)绕原点旋转angle弧度
function rotatePoint(x, y, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return {
x: x * cos - y * sin,
y: x * sin + y * cos
};
}
import numpy as np
def rotate_3d(point, angle_x, angle_y, angle_z):
# 转换为弧度
angle_x, angle_y, angle_z = np.radians(angle_x), np.radians(angle_y), np.radians(angle_z)
# 绕X轴旋转矩阵
Rx = np.array([
[1, 0, 0],
[0, np.cos(angle_x), -np.sin(angle_x)],
[0, np.sin(angle_x), np.cos(angle_x)]
])
# 绕Y轴旋转矩阵
Ry = np.array([
[np.cos(angle_y), 0, np.sin(angle_y)],
[0, 1, 0],
[-np.sin(angle_y), 0, np.cos(angle_y)]
])
# 绕Z轴旋转矩阵
Rz = np.array([
[np.cos(angle_z), -np.sin(angle_z), 0],
[np.sin(angle_z), np.cos(angle_z), 0],
[0, 0, 1]
])
# 组合旋转矩阵
rotation_matrix = Rz @ Ry @ Rx
# 应用旋转
return rotation_matrix @ point
function rotateAroundPoint(x, y, centerX, centerY, angle) {
// 平移至原点
const translatedX = x - centerX;
const translatedY = y - centerY;
// 旋转
const rotated = rotatePoint(translatedX, translatedY, angle);
// 平移回原位置
return {
x: rotated.x + centerX,
y: rotated.y + centerY
};
}
// 使用四元数进行3D旋转
glm::quat rotation = glm::quat(glm::vec3(pitch, yaw, roll));
glm::mat4 rotationMatrix = glm::toMat4(rotation);
希望这些信息能帮助你实现正确的旋转函数!如果你有更具体的旋转问题或遇到特定错误,可以提供更多细节以便获得更有针对性的解决方案。