PHP 中的坐标旋转
(这个问题特定于 PHP,我知道这在其他语言中进行了讨论,但我在 PHP 中实现它时遇到了麻烦。
)要放置在旋转图像上的特征的 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);
}
据说这个函数应该给我块的新坐标,并考虑到旋转。但定位却相差甚远。
我做错了什么?
(This question is specific to PHP, I know this is discussed in other languages, but I'm having trouble with implementing it in PHP.)
I'm attempting to rotate the x & y coordinates of a feature which is to be placed on a rotated image.
$x
& $y
are the original x,y coordinates of the block before the image was rotated.
$width2
& $height2
are the center of rotation (which is the center of the image).
$sin
& $cos
are the sine & cosine, which are obtained with sin($radians)
andcos($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.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在计算旋转时,您应该在代码中使用其他变量:
$x 由第一个方程修改,然后在第二个方程中使用错误的 $x 值。
更改为:
You should use other variables when you compute the rotation, in your code:
$x is modified by the first equation, then you're using wrong value of $x in the second.
Change to: