Several web sites give the same answer, but it's not working for me. I'd appreciate any guidance in where I'm going wrong.
Situation: I want to rotate a polygon. I have the coordinates of each point. I run each point through this function, which should give me the rotated polygon. However, the new one is shorter and wider than the original - it changes shape. Here is a more complete version of my code;
function rotatePoint($coord,$rot){
/*
in - x, y coordinate, how much to rotate in degrees
do - rotate each point
out - end coordinate point
rotate a point (x, y) by t radians counterclockwise
about the origin (0, 0), the transformed coordinates (x', y')
can be computed by:
x' = cos(t)*x - sin(t)*y
y' = sin(t)*x + cos(t)*y
*/
$rotRad=deg2rad($rot);
$rotCoord=array();
$rotCoord['x']=(cos($rotRad)*$coord['x'])-(sin($rotRad)*$coord['y']);
$rotCoord['y']=(sin($rotRad)*$coord['x'])+(cos($rotRad)*$coord['y']);
return $rotCoord;
};
$polygonPoints=array(
array('y'=>40.039363507917,'x'=>-76.112888306379),
array('y'=>40.039369668435,'x'=>-76.112935245037),
array('y'=>40.039246457955,'x'=>-76.112959384918),
array('y'=>40.039240297425,'x'=>-76.11291244626),
array('y'=>40.039363507917,'x'=>-76.112888306379)
);
$rotateAmt=90;//how much to rotate
$pointX = array();
$pointY = array();
foreach ($polygonPoints as $key => $row)
{
$pointX[$key] = $row['x'];
$pointY[$key] = $row['y'];
}
$maxX=$minX=$polygonPoints[0]['x'];
$maxY=$minY=$polygonPoints[0]['y'];
for($i=0;$i<count($polygonPoints);$i++){
if($polygonPoints[$i]['x']>=$maxX){$maxX=$polygonPoints[$i]['x'];};
if($polygonPoints[$i]['x']<$minX){$minX=$polygonPoints[$i]['x'];};
if($polygonPoints[$i]['y']>=$maxY){$maxY=$polygonPoints[$i]['y'];};
if($polygonPoints[$i]['y']<$minY){$minY=$polygonPoints[$i]['y'];};
}
$center=array('x'=>($maxX+$minX)/2,'y'=>($maxY+$minY)/2);
$adjustment=array('x'=>0-$center['x'],'y'=>0-$center['y']);
echo 'Adjustment<pre>';
var_dump($adjustment);
echo'</pre>';
$adjustedPolygon=array();
for($i=0;$i<count($polygonPoints);$i++){
$adjustedPolygon[$i]['x']=$polygonPoints[$i]['x']+$adjustment['x'];
$adjustedPolygon[$i]['y']=$polygonPoints[$i]['y']+$adjustment['y'];
}
$rotatedPolygon=array();
for($i=0;$i<count($adjustedPolygon);$i++){
// echo 'before rotatePoint '.$i.'='.$adjustedPolygon[$i]['x'].', '.$adjustedPolygon[$i]['y'].'<br />';
$rotatedPolygon[$i]=rotatePoint($adjustedPolygon[$i],$rotateAmt);
// echo 'after rotatePoint '.$i.'='.$rotatedPolygon[$i]['x'].', '.$rotatedPolygon[$i]['y'].'<br />';
$rotatedPolygon[$i]['x']=$rotatedPolygon[$i]['x']-$adjustment['x'];
$rotatedPolygon[$i]['y']=$rotatedPolygon[$i]['y']-$adjustment['y'];
}
Thanks for the help. Let me know if I should put this on math.stackexchange.com