PHP中的坐标旋转 [英] Coordinate Rotation in PHP
问题描述
(这个问题是针对 PHP 的,我知道在其他语言中讨论过这个问题,但是我在用 PHP 实现它时遇到了问题.)
(This question is specific to PHP, I know this is discussed in other languages, but I'm having trouble with implementing it in PHP.)
我正在尝试旋转 x &要放置在旋转图像上的特征的 y 坐标.
I'm attempting to rotate the x & y coordinates of a feature which is to be placed on a rotated image.
$x
&$y
是图像旋转前块的原始 x,y 坐标.
$x
& $y
are the original x,y coordinates of the block before the image was rotated.
$width2
&$height2
是旋转中心(也就是图片的中心).
$width2
& $height2
are the center of rotation (which is the center of the image).
$sin
&$cos
是正弦和余弦,由 sin($radians)
和cos($radians)
关于(背景)图像旋转的角度(以弧度为单位)
$sin
& $cos
are the sine & cosine, which are obtained with sin($radians)
and
cos($radians)
on the degree of rotation the (background) image was rotated by (in 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);
}
据说这个函数应该给我块的新坐标,并考虑到旋转.但是定位比较远.
Supposedly this function should give me the new coordinates of the block, with the rotation taken into account. But the positioning is quite far off.
我做错了什么?
推荐答案
在计算旋转时,您应该在代码中使用其他变量:
You should use other variables when you compute the rotation, in your code:
$x = $x * $cos - $y * $sin;
$y = $x * $sin + $y * $cos;
$x 被第一个方程修改,然后您在第二个方程中使用了错误的 $x 值.
$x is modified by the first equation, then you're using wrong value of $x in the second.
更改为:
$temp_x = $x * $cos - $y * $sin;
$temp_y = $x * $sin + $y * $cos;
这篇关于PHP中的坐标旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!