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.imagefilledpol
ygon.php
Edit -- https://master
.php.net/note/edit/78673
Del: integrated -- h
ttps://master.php.net/note/delete/78673/integrated
Del: useless -- http
s://master.php.net/note/delete/78673/useless
Del: bad code -- htt
ps://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 -- http
s://master.php.net/note/delete/78673/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78673
Reject -- https://mast
er.php.net/note/reject/78673
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|