php - PHP中的坐标旋转

标签 php rotation trigonometry

(这个问题是特定于 PHP 的,我知道这是用其他语言讨论的,但我在用 PHP 实现它时遇到了问题。)

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

$x & $y 是图像旋转前 block 的原始 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);
    }

据说这个函数应该给我 block 的新坐标,并考虑到旋转。但定位还差得很远。

我做错了什么?

最佳答案

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

$x = $x * $cos - $y * $sin;
$y = $x * $sin + $y * $cos;

$x 被第一个等式修改,那么你在第二个等式中使用了错误的 $x 值。

更改为:

$temp_x = $x * $cos - $y * $sin;
$temp_y = $x * $sin + $y * $cos;

关于php - PHP中的坐标旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8742237/

相关文章:

Android 模拟器 - 屏幕旋转

ios - 沿弧线放置 UICollectionViewCells (UIBezierPath)

c - GCC 使用的函数 rem_pio2f() 是什么?

php - Magento addAttributeToFilter,按属性文本过滤

php - 从php在数据库中插入特殊字符

php - Debian 上的 Apache - 升级到 PHP8.1 - phpinfo() 不显示新版本并与 mpm 冲突

php - 登录 session 在 Firefox 中立即过期,但在 Chrome 中工作正常

python - 高效的平行 3D 旋转

ios - 更改UIToolbar UIBarMetrics

c# - 确定角度是否在 2 个其他角度之间