I have a 2D Rectangle defined with 4 points ordered in counter clockwise fashion - e.g. point 0(x0,y0), point 1(x1, y1), etc.I would like to know how to rotate each of these points in 3D space (even though the Rectangle is 2D).
I would like to randomly choose the axis (x, y or z) to rotate around. Something along the lines of the following C++ code for each point in the Rectangle:
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;
}
Is the above code correct for what I would like to do?
Thank you in advanced for any help.
Your implementation doesn't look correct to me. I were you I would just write one function for rotation around any axis through the origin of the coordinate system pointing in a general direction. Then you choose the specific directions as you please. Here is code in python (it is more concise and presents the idea more clearly), you can implement it in 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