Submitted on 2009-06-17 01:04:02.
Username:
Title:
Code:<?php //Mandlebrot set drawing thing //Set up vars $width = 271; $height = 236; $point = array('r' => 0.0, 'i' => 0.0); $Z = array('r' => 0.0, 'i' => 0.0); $Z2 = array('r' => 0.0, 'i' => 0.0); $maxIterations = 60; //Define coordinate limits $MinRe = -2.0; $MaxRe = 1.0; $MinIm = -1.2; $MaxIm = $MinIm+($MaxRe-$MinRe)*$height/$width; //Define constants $realFactor = ($MaxRe-$MinRe)/($width-1); $imaginaryFactor = ($MaxIm-$MinIm)/($height-1); //Generate blank canvas $image = imagecreatetruecolor($width, $height); $colBlack = imagecolorallocate($image, 0, 0, 0); $colWhite = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $colWhite); for($y = 0; $y < $height; ++$y){ //Set imaginary part of complex number for the point $point['i'] = $MaxIm - $y*$imaginaryFactor; for($x = 0; $x < $width; ++$x){ //Set real part $point['r'] = $MinRe + $x*$realFactor; $Z['r'] = $point['r']; $Z['i'] = $point['i']; //Z = c $isInside = true; for($i = 0; $i < $maxIterations; ++$i){ //Optimised by finding z^2 values here $Z2['r'] = $Z['r']*$Z['r']; $Z2['i'] = $Z['i']*$Z['i']; if($Z2['r'] + $Z2['i'] > 4){ //Check distance from 0,0 isn't above 2 by using z^2 values and no sqrt but a 4 instead $isInside = false; break; } //Calc z=z^2+c: //(a+bi)^2 = (a+bi)(a+bi) = a^2 + abi + abi + (bi)^2 = a^2 - b^2 + 2abi //Basically, multiplied real bits = a^2-b^2 and imaginary bits = 2ab $Z['i'] = 2*$Z['r']*$Z['i'] + $point['i']; $Z['r'] = $Z2['r'] - $Z2['i'] + $point['r']; } //Draw pixel if($isInside){ //imagesetpixel($image, $x, $y, $colBlack); imagesetpixel($image, $x, $y, imagecolorallocate($image, 0, 0, (100/(2/sqrt($point['r']*$point['r']+$point['i']*$point['i']))))); }else{ imagesetpixel($image, $x, $y, imagecolorallocate($image, (255/($maxIterations/($i+1))), 0, 0)); } } } //Output image header("Content-Type: image/png"); imagepng($image); ?>
(Edit the above source them submit it as a reply)
Your username:
Download here