微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 3D 坐标中旋转的 2D 矩形

如何解决在 3D 坐标中旋转的 2D 矩形

我有一个 2D 矩形,定义为以逆时针方式排列的 4 个点 - 例如点 0(x0,y0)、点 1(x1,y1) 等。我想知道如何在 3D 空间中旋转这些点中的每一个(即使矩形是 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,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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。