C++ 二维欧几里得矢量旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4780119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2D Euclidean vector rotations
提问by deceleratedcaviar
I have a euclidean vector a
sitting at the coordinates (0, 1)
.
I want to rotate a
by 90 degrees (clockwise) around the origin: (0, 0)
.
我有一个a
位于坐标处的欧几里得向量(0, 1)
。我想a
围绕原点旋转90 度(顺时针):(0, 0)
。
If I have a proper understanding of how this should work, the resultant (x, y) coordinates after the rotation should be (1, 0)
.
If I were to rotate it by 45 degrees (still clockwise) instead, I would have expected the resultant coordinates to be (0.707, 0.707)
.
如果我对这应该如何工作有一个正确的理解,旋转后的结果 (x, y) 坐标应该是(1, 0)
. 如果我将它旋转 45 度(仍然是顺时针),我会期望得到的坐标为(0.707, 0.707)
.
theta = deg2rad(angle);
cs = cos(theta);
sn = sin(theta);
x = x * cs - y * sn;
y = x * sn + y * cs;
Using the above code, with an angle
value of 90.0 degrees, the resultant coordinates are: (-1, 1)
.
And I am so damn confused.
The examples seen in the following links represent the same formula shown above surely?
使用上面的代码,angle
值为 90.0 度,结果坐标为:(-1, 1)
。我他妈的很困惑。以下链接中的示例肯定代表上述相同的公式吗?
What have I done wrong? Or have I misunderstood how a vector is to be rotated?
我做错了什么?或者我误解了向量是如何旋转的?
回答by Sebastian Paaske T?rholm
Rotating a vector 90 degrees is particularily simple.
将矢量旋转 90 度特别简单。
(x, y)
rotated 90 degrees around (0, 0)
is (-y, x)
.
(x, y)
旋转 90 度(0, 0)
是(-y, x)
。
If you want to rotate clockwise, you simply do it the other way around, getting (y, -x)
.
如果你想顺时针旋转,你只需反过来做,得到(y, -x)
.
回答by Caspar Kleijne
you should remove the vars from the function:
您应该从函数中删除 vars:
x = x * cs - y * sn; // now x is something different than original vector x
y = x * sn + y * cs;
create new coordinates becomes, to avoid calculation of x before it reaches the second line:
创建新坐标变为,以避免在到达第二行之前计算 x:
px = x * cs - y * sn;
py = x * sn + y * cs;
回答by Altivo
Rotate by 90 degress around 0,0:
围绕 0,0 旋转 90 度:
x' = -y
y' = x
Rotate by 90 degress around px,py:
围绕 px,py 旋转 90 度:
x' = -(y - py) + px
y' = (x - px) + py
回答by MSalters
Sounds easier to do with the standard classes:
使用标准类听起来更容易:
std::complex<double> vecA(0,1);
std::complex<double> i(0,1); // 90 degrees
std::complex<double> r45(sqrt(2.0),sqrt(2.0));
vecA *= i;
vecA *= r45;
Vector rotation is a subset of complex multiplication. To rotate over an angle alpha
, you multiply by std::complex<double> { cos(alpha), sin(alpha) }
矢量旋转是复数乘法的一个子集。要旋转一个角度alpha
,请乘以std::complex<double> { cos(alpha), sin(alpha) }
回答by xtofl
You're calculating the y-part of your new coordinate based on the 'new' x-part of the new coordinate. Basically this means your calculating the new output in terms of the new output...
您正在根据新坐标的“新”x 部分计算新坐标的 y 部分。基本上这意味着您根据新输出计算新输出......
Try to rewrite in terms of input and output:
尝试在输入和输出方面重写:
vector2<double> multiply( vector2<double> input, double cs, double sn ) {
vector2<double> result;
result.x = input.x * cs - input.y * sn;
result.y = input.x * sn + input.y * cs;
return result;
}
Then you can do this:
然后你可以这样做:
vector2<double> input(0,1);
vector2<double> transformed = multiply( input, cs, sn );
Note how choosing proper names for your variables can avoid this problem alltogether!
请注意如何为变量选择正确的名称可以完全避免这个问题!