Drawing mandalas with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Prints a mondala | |
$points = $_GET['points']; | |
$size = $_GET['size']; | |
// Test the size | |
if($size < 1 || $size > 1024){ | |
echo "Please use a better size"; | |
// Test the number of points | |
}elseif($points < 1 || $points > 100){ | |
echo "Please use a more reasonable number or points"; | |
}else{ | |
// Set the radius to half of the size of the image, so it fills the image | |
$radius = $size/2; | |
// Find the number of degrees between points (in radians) | |
$degrees = deg2rad(360/$points); | |
// The offset used to push the whole drawing into the first quadrant (because our math would | |
// normally put it centered on 0,0 | |
$offset = $size/2; | |
// Create the image | |
$im = imagecreatetruecolor($size, $size); | |
// Set the background color white | |
$background = imagecolorallocate($im, 255, 255, 255); | |
imagefill($im, 0, 0, $background); | |
// Set the color of the lines black | |
$lines = imagecolorallocate($im, 0, 0, 0); | |
// Until we have made our way around the circle | |
while($angle < deg2rad(360)){ | |
// Increase the angle to point to the next point | |
$angle = $angle + $degrees; | |
// Find the new points | |
$x = $radius * cos($angle) + $offset; | |
$y = $radius * sin($angle) + $offset; | |
// Add those points to the array of points | |
$pointsArr[] = array($x, $y); | |
} | |
// For each point | |
while($from = array_pop($pointsArr)){ | |
// For each point that has not yet been plotted | |
foreach($pointsArr as $to){ | |
// Draw a line | |
imageline($im, $from[0], $from[1], $to[0], $to[1], $lines); | |
} | |
} | |
// Output the image | |
header ("Content-type: image/png"); | |
imagepng($im); | |
// Clear the image form memory | |
imagedestroy($im); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment