【问题标题】:2D Rectangle rotated in 3D coordinates在 3D 坐标中旋转的 2D 矩形
【发布时间】: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


    【解决方案1】:

    您的实现对我来说看起来不正确。我是你,我只需要编写一个函数,通过指向一个大致方向的坐标系原点围绕任何轴旋转。然后,您可以随意选择具体的方向。这里是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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-06
      • 2018-05-25
      • 2014-06-12
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多