如何在 R^2 中进行旋转?

发布于 2024-09-15 07:37:26 字数 973 浏览 7 评论 0原文

我遇到了一个看似简单的数学问题:我需要在二维笛卡尔坐标系中旋转点,即我有一个由 (x/y) 和角度 gamma 给出的点,并且我需要获取以下坐标如果按 gamma 旋转该点...

示例:如果 x = 2 且 y = 0 并且旋转角度为 90°,则结果点将是 x' = 0,y' = -2(顺时针旋转),

所以我发现这个公式在网上(http://en.wikipedia.org/wiki/Rotation_matrix)并实现了一些代码来测试它:

$x = 1; echo "x: " . $x . "<br>";
$y = 1; echo "y: " . $y . "<br>";
$gamma = 45; echo "gamma: " . $gamma . "<br>";

$sinGamma = sin(deg2rad($gamma));
$cosGamma = cos(deg2rad($gamma));

$x2 = $x*$cosGamma - $y*$sinGamma; echo "x2: " . $x2 . "<br>";
$y2 = $y*$cosGamma + $x*$sinGamma; echo "y2: " . $y2 . "<br>";

虽然这对于 90/180/270 度的角度非常有效,但其他任何东西都会导致完全垃圾!

即:

如果 x=1 且 y=1 且 gamma=45°,则结果点将恰好位于 x 轴上...好吧 - 上面的脚本将输出:

x: 1
y: 1
gamma: 45
x2: 1.11022302463E-16
y2: 1.41421356237

我理解错了吗? (学校对我来说已经结束了很长一段时间^^)我该如何做呢?

i'm stuck with a seemingly simple problem in mathematics: i need to rotate points in a 2-dimensional cartesian coordinate system, i.e. i have a point given by (x/y) and an angle gamma and i need to get the coordinates of this point if rotated by gamma...

example: if x = 2 and y = 0 and the angle of rotation is 90°, the resulting point would be x' = 0, y' = -2 (rotated clockwise)

so i found this formula on the net (http://en.wikipedia.org/wiki/Rotation_matrix) and implemented some code to test it:

$x = 1; echo "x: " . $x . "<br>";
$y = 1; echo "y: " . $y . "<br>";
$gamma = 45; echo "gamma: " . $gamma . "<br>";

$sinGamma = sin(deg2rad($gamma));
$cosGamma = cos(deg2rad($gamma));

$x2 = $x*$cosGamma - $y*$sinGamma; echo "x2: " . $x2 . "<br>";
$y2 = $y*$cosGamma + $x*$sinGamma; echo "y2: " . $y2 . "<br>";

while this works just GREAT for angles of 90/180/270 degrees, anything else would result in total crap!

i.e.:

if x=1 and y=1 and gamma=45°, the resulting point would lay exactly on the x-axis... well - the script above would output:

x: 1
y: 1
gamma: 45
x2: 1.11022302463E-16
y2: 1.41421356237

did i understand sth wrong? (school's over a long time for me ^^) how do i get this right?

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

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

发布评论

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

评论(2

云胡 2024-09-22 07:37:26

你的数字实际上看起来很正确 - (1,1) 绕原点旋转 45 度将是 (0, sqrt(2))。 x2 看起来很奇怪,因为前面有 1,但 E-16 意味着这个数字实际上是 0.000000000000000111022 或类似的东西。 sqrt(2) 的结果约为 1.414。

由于浮点舍入误差,您不会获得准确的结果(更不用说您正在处理无理数)。

Your numbers actually look pretty much right there -- (1,1) rotated 45 degrees around the origin would be (0, sqrt(2)). x2 looks odd because of the 1 in front, but the E-16 means the number's actually .000000000000000111022 or something like that. And sqrt(2) comes out to somewhere around 1.414.

You're not going to get exact results due to floating-point rounding error (not to mention you're working with irrational numbers).

节枝 2024-09-22 07:37:26

你的代码是正确的。事实上,您的示例没有完全在 y 轴上结束,这只是由于本质上不精确的浮点计算,如果您想旋转实坐标点,无论如何都无法避免这种情况。

Your code is correct. The fact that your example doesn't end up exactly on the y axis is only due to inherently inexact floating point calculation, which you can't avoid anyway, if you want to rotate real-coordinate points.

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