第一次问。我想在XY平面中的c ++中的3d中旋转一个点,并且正在使用以下函数来完成任务。
void rotateXY(double angle){
//save the x and y and z coordinates in seperate variables
double x = this->pos[0]; // value 1
double y = this->pos[1]; // value 0
double z = this->pos[2]; // value 0, but in the xy rotation it is not important
double radian = angle*M_PI/180;
this->pos[0] = cos(radian)*x - sin(radian)*y;
this->pos[1] = sin(radian)*x + cos(radian)*y;
this->pos[2] = 1*z;
};
在此,我直接操作点的坐标,因此this-> pos [0]
[如果我调用另一个名为rotateXYP的函数,在该函数中,我首先从旋转点中减去一个数学矢量,并在旋转后向其添加相同的数学矢量,得到所需的结果。
void rotateXYP(double angle, eng::point originOfRotation){
this->subVec(originOfRotation);
this->rotateXY(angle);
this->addVec(originOfRotation);
};
void rotateXY(double angle){
//save x,y and z in seperate variables for manipulation
double x = this->pos[0]; // value 1
double y = this->pos[1]; // value 0
double z = this->pos[2]; // value 0, but in the xy rotation it is not important
//convert from degrees to radians because cmath requires it
double radian = angle*M_PI/180;
//apply the values according to a rotation matrix found on the internet
this->pos[0] = cos(radian)*x - sin(radian)*y;
this->pos[1] = sin(radian)*x + cos(radian)*y;
this->pos[2] = 1*z;
};
为什么我将点(1 | 0 | 0)作为输入对函数RotateXY(90)的跟随作为输出。
(6.12323e-17|1|0)
而不是
(0|1|0)
并且如果我调用函数rotateXYP(90,某个点),我会得到正确的点,而x坐标上没有小数。我怀疑这与以下代码行中的cos和sin有关:
this->pos[0] = cos(radian)*x - sin(radian)*y;
由于我对c ++太缺乏经验,所以我寻求答案,并希望这不是一个坏问题。
您的实现是正确的。这只是浮点运算的本质。所有数字均表示为近似值。平移点时,您会得到更好的数值条件。
我可能会补充说,这种影响将独立于所使用的编程语言和硬件而发生。