【问题标题】:Coordinate Rotation in PHPPHP中的坐标旋转
【发布时间】:2012-09-26 15:56:59
【问题描述】:

(这个问题是 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);
    }

假设这个函数应该给我块的新坐标,考虑到旋转。但是定位差得很远。

我做错了什么?

【问题讨论】:

  • 如果你已经有一段时间没有做三角学了,请允许我把你从我刚刚爬起来的高中兔子身上救出来。 弧度 表示取一个角度(如 45°)并乘以 pi / 180。$radians = $angle * (pi()/180);

标签: php rotation trigonometry


【解决方案1】:

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

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

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

改为:

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

【讨论】:

  • 哇!那是小学生的错误! 尴尬我会更改它并让您知道是否可以解决问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 2023-04-03
  • 2021-06-04
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
相关资源
最近更新 更多