围绕点旋转多边形

时间:2014-01-19 04:54:04

标签: php rotation

有几个网站给出了相同的答案,但它对我不起作用。我很感激我在哪里出错了。

情况:我想旋转多边形。我有每个点的坐标。我通过这个函数运行每个点,应该给我旋转的多边形。然而,新的比原来更短更宽 - 它改变了形状。这是我的代码的更完整版本;

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'];
      }

感谢您的帮助。如果我将其放在math.stackexchange.com上,请告诉我。

1 个答案:

答案 0 :(得分:2)

这是我提出的解决方案......

它需要一整套点并围绕指定的中心旋转。如果需要,它还可以缩放多边形。该函数返回一个新的一维点数组。

数组是一维的,因为我在PHP imagepolygon()函数中使用它。

################################################################### 
#####               ROTATE POLYGON FUNCTION                   #####
#####                                                         #####
#####               PLEASE DO REMOVE THIS NOTICE              #####
#####               by Steve Burgess - 2015                   #####
###################################################################
# $polygon - points of the polygon in a 1 dimensional array
#            e.g. (x1,y1,x2,y2,x3,y3.... etc)
# $angle   - rotation angle in degrees
# $centrex - x coordinate of centre of rotation
# $centrey - y coordinate of centre of rotation
# $scale   - scale for resizing the polygon
#
# The function returns a new array of scaled/rotated values that can
# be used in the imagepolygon() and imagefilledpolygon() functions
###################################################################

function rotatepolygon($polygon,$angle=0,$centrex,$centrey,$scale=1)
{
# I have negated the angle here so the function rotates in the same
# direction as the imagerotate() function in PHP

# PHP Trigonometric Functions (e.g. cosine, sine) require the angle to
# be in radians rather than degrees - hence the deg2rad() conversion.

$angle=deg2rad(-$angle); 

if($scale<>1)
{
    # Using the array_map() function performs the scaling for the entire array
    # in one line - rather than having to write code that loops through the array.       

$polygonscaled = array_map("scale",$polygon,array_fill(0,count($polygon),$scale));
    $polygon=$polygonscaled;
    $centrex=$centrex*$scale;
    $centrey=$centrey*$scale;
}

for($i=0;$i<count($polygon);$i=$i+2)
{
    # Using the array map function to perform these transformations was beyond me.
    # If anyone has any bright ideas about this, please drop me a line

    # Original coordinates of each point
$x=$polygon[$i];$y=$polygon[$i+1];

    # As imagepolygon requires a 1 dimensional array, the new x and the new y
    # coordinates are simply added to the rotated array one after the other
    $rotated[]=($centrex+(($x-$centrex)*cos($angle))-(($y-$centrey)*sin($angle))); 
$rotated[]=($centrey+(($x-$centrex)*sin($angle))+(($y-$centrey)*cos($angle))); 
}

return $rotated;
}

function scale($value,$scale)
{
# This function is essential for the polygon rotate function.
# Make sure you copy/paste this into your code as well as the main function.

return($value*$scale);
}