Jump to content

Simple Rotation of point around origin question...


cr8zy1van

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);

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.