(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) 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.
What am I doing wrong?
You should use other variables when you compute the rotation, in your code:
$x = $x * $cos - $y * $sin;
$y = $x * $sin + $y * $cos;
$x is modified by the first equation, then you're using wrong value of $x in the second.
Change to:
$temp_x = $x * $cos - $y * $sin;
$temp_y = $x * $sin + $y * $cos;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With