繁体   English   中英

在 3D 坐标中旋转的二维矩形

[英]2D Rectangle rotated in 3D coordinates

我定义了一个 2D 矩形,其中 4 个点按逆时针方向排序 - 例如点 0(x0,y0)、点 1(x1, y1) 等。我想知道如何在 3D 空间中旋转这些点中的每一个(即使矩形是二维的)。

我想随机选择要旋转的轴(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;
}

上面的代码对我想做的事情是否正确?

提前感谢您的任何帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM