Drawing mandalas with PHP for my Papa
The earliest memories I have of computers is a contraption my dad rigged up that would draw mandalas on a screen. Later in my life he wrote software that would plot large mandalas on his huge plotters.
In my last project, I learned about image generation using PHP and realized that it might be possible to use PHP functions to draw mandalas. After a couple hours of relearning trig, I was able to create a script that outputs a .png image of a mandala based on the size and number of points that you give it.
Give it a try:
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?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); } ?> |
