Jump to content

Recommended Posts

I am trying to rotate a point around a fixed origin in the middle of an image (i need to do this for data in an array so i don't want to rotate the whole image) So far I have:

 

function returnRotatedPoint($x,$y,$a){

$a = deg2rad($a); // convert to radians

$ox = 100; // origin point - always fixed but may change (image size is 200x200)

$oy = 100;

$x = $ox + ( cos($a) * ($x - $ox) - sin($a) * ($y - $oy) );

$y = $oy + ( sin($a) * ($x - $ox) + cos($a) * ($y - $oy) );

    return  array("x"=>$x,"y"=>$y);

}

 

When I call it by in my code:

 

$zpoint1 = returnRotatedPoint(10, 100, 50);

echo $zpoint1["x"].",". $zpoint1["y"];

 

the data it is really wonky. I haven't been able to find a pattern yet. Anyone see something obvious that I am missing? I don't usually work in PHP and am sure it is probably just something I am doing wrong.

 

Nevermind... maybe I should go running more often and not try to overwrite x, while the next line needs x untouched. Fixed code below. Issue fixed... close this thread.

 

function returnRotatedPoint($x,$y,$a){

$a = deg2rad($a); // convert to radians

$ox = 100; // origin point - always fixed but may change

$oy = 100;

$xnew = $ox + ( cos($a) * ($x - $ox) - sin($a) * ($y - $oy) );

$ynew = $oy + ( sin($a) * ($x - $ox) + cos($a) * ($y - $oy) );

 

    return  array("x"=>$xnew,"y"=>$ynew);

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.