2D 欧几里得向量旋转

发布于 2024-10-13 18:42:07 字数 509 浏览 3 评论 0原文

我有一个欧几里得向量 a 位于坐标 (0, 1) 处。 我想围绕原点 a 旋转 90 度(顺时针):(0, 0)

如果我正确理解了它的工作原理,旋转后得到的 (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;

使用上述代码,angle 值为 90.0 度,所得坐标为:(-1, 1)。 我真的很困惑。 以下链接中看到的示例肯定代表上面显示的相同公式吗?

我做错了什么? 或者我是否误解了矢量如何旋转?

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).

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).

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?

What have I done wrong?
Or have I misunderstood how a vector is to be rotated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

日裸衫吸 2024-10-20 18:42:07

将矢量旋转 90 度特别简单。

(x, y)(0, 0)旋转90度为(-y, x)

如果您想顺时针旋转,只需以相反的方式进行即可,得到(y, -x)

Rotating a vector 90 degrees is particularily simple.

(x, y) rotated 90 degrees around (0, 0) is (-y, x).

If you want to rotate clockwise, you simply do it the other way around, getting (y, -x).

北方。的韩爷 2024-10-20 18:42:07

您应该从函数中删除变量:

x = x * cs - y * sn; // now x is something different than original vector x
y = x * sn + y * cs;

创建新坐标变为,以避免在到达第二行之前计算 x:

px = x * cs - y * sn; 
py = x * sn + y * cs;

you should remove the vars from the function:

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:

px = x * cs - y * sn; 
py = x * sn + y * cs;
寄居者 2024-10-20 18:42:07

围绕 0,0 旋转 90 度:

x' = -y
y' = x

围绕 px,py 旋转 90 度:

x' = -(y - py) + px
y' = (x - px) + py

Rotate by 90 degress around 0,0:

x' = -y
y' = x

Rotate by 90 degress around px,py:

x' = -(y - py) + px
y' = (x - px) + py
且行且努力 2024-10-20 18:42:07

听起来使用标准类更容易做到:

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;

向量旋转是复数乘法的子集。要旋转角度 alpha,请乘以 std::complex{ cos(alpha), sin(alpha) }

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) }

和影子一齐双人舞 2024-10-20 18:42:07

您正在根据新坐标的“新”x 部分计算新坐标的 y 部分。基本上,这意味着您根据新输出计算新输出...

尝试根据输入和输出重写:

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;
}

然后您可以执行以下操作:

vector2<double> input(0,1);
vector2<double> transformed = multiply( input, cs, sn );

注意如何为变量选择正确的名称可以完全避免此问题!

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...

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文