note 78673 added to function.imagefilledpolygon
| From: | webmaster at mywebsolution dot de | Date: | Mon, 22 Oct 2007 21:24:53 +0000 |
| Subject: | note 78673 added to function.imagefilledpolygon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131645@lists.php.net to get a copy of this message | ||
Just thought that 'tatlar at yahoo dot com's function has some redundant code in it, so I
tried to "improve" it. Now you can choose a variable number of spikes.
<?php
error_reporting(E_ALL);
function drawStar($x, $y, $radius, $spikes=5) {
// $x, $y -> Position in the image
// $radius -> Radius of the star
// $spikes -> Number of spikes
$coordinates = array();
$angel = 360 / $spikes ;
// Get the coordinates of the outer shape of the star
$outer_shape = array();
for($i=0; $i<$spikes; $i++){
$outer_shape[$i]['x'] = $x + ($radius * cos(deg2rad(270 - $angel*$i)));
$outer_shape[$i]['y'] = $y + ($radius * sin(deg2rad(270 - $angel*$i)));
}
// Get the coordinates of the inner shape of the star
$inner_shape = array();
for($i=0; $i<$spikes; $i++){
$inner_shape[$i]['x'] = $x + (0.5*$radius * cos(deg2rad(270-180 - $angel*$i)));
$inner_shape[$i]['y'] = $y + (0.5*$radius * sin(deg2rad(270-180 - $angel*$i)));
}
// Bring the coordinates in the right order
foreach($inner_shape as $key => $value){
if($key == (floor($spikes/2)+1))
break;
$inner_shape[] = $value;
unset($inner_shape[$key]);
}
// Reset the keys
$i=0;
foreach($inner_shape as $value){
$inner_shape[$i] = $value;
$i++;
}
// "Merge" outer and inner shape
foreach($outer_shape as $key => $value){
$coordinates[] = $outer_shape[$key]['x'];
$coordinates[] = $outer_shape[$key]['y'];
$coordinates[] = $inner_shape[$key]['x'];
$coordinates[] = $inner_shape[$key]['y'];
}
// Return the coordinates
return $coordinates ;
}
// Example
$spikes = 5;
$values = drawStar(250, 250, 200, $spikes);
$im = imagecreate(500,500);
imagecolorallocate($im,0,0,0);
$w = imagecolorallocate($im, 255, 255, 255);
imagefilledpolygon($im, $values, $spikes*2, $w);
imageGIF($im);
imagedestroy($im);
?>
----
Server IP: 217.13.201.10
Probable Submitter: 84.138.171.49
----
Manual Page -- http://www.php.net/manual/en/function.imagefilledpolygon.php
Edit -- https://master.php.net/note/edit/78673
Del: integrated -- https://master.php.net/note/delete/78673/integrated
Del: useless -- https://master.php.net/note/delete/78673/useless
Del: bad code -- https://master.php.net/note/delete/78673/bad+code
Del: spam -- https://master.php.net/note/delete/78673/spam
Del: non-english -- https://master.php.net/note/delete/78673/non-english
Del: in docs -- https://master.php.net/note/delete/78673/in+docs
Del: other reasons-- https://master.php.net/note/delete/78673
Reject -- https://master.php.net/note/reject/78673
Search -- https://master.php.net/manage/user-notes.php