PHP 中的坐标旋转

Ala*_*air 4 php trigonometry rotation

(这个问题是针对 PHP 的,我知道这在其他语言中也有讨论,但我在 PHP 中实现它时遇到了麻烦。)

我正在尝试旋转要放置在旋转图像上的特征的 x 和 y 坐标。

$x&$y是图像旋转之前块的原始 x,y 坐标。

$width2&$height2是旋转中心(即图像的中心)。

$sin&$cos是正弦和余弦,它们是根据sin($radians)cos($radians)背景)图像旋转的旋转度(以弧度为单位)获得的

function RotatePoints($x,$y,$width2,$height2,$sin,$cos)
    {
    // translate point back to origin:
    $x -= $width2;
    $y -= $height2;

    // rotate point
    $x = $x * $cos - $y * $sin;
    $y = $x * $sin + $y * $cos;

    // translate point back:
    $x += $width2;
    $y += $height2;

    return array($x,$y);
    }
Run Code Online (Sandbox Code Playgroud)

据说这个函数应该给我块的新坐标,并考虑到旋转。但定位却相差甚远。

我究竟做错了什么?

Tot*_*oto 6

在代码中计算旋转时,您应该使用其他变量:

$x = $x * $cos - $y * $sin;
$y = $x * $sin + $y * $cos;
Run Code Online (Sandbox Code Playgroud)

$x 由第一个方程修改,然后您在第二个方程中使用了错误的 $x 值。

改成:

$temp_x = $x * $cos - $y * $sin;
$temp_y = $x * $sin + $y * $cos;
Run Code Online (Sandbox Code Playgroud)