note 96871 modified in function.imagefilledrectangle by danbrown
| From: | danbrown@php.net | Date: | Sat, 20 Mar 2010 00:58:51 +0000 |
| Subject: | note 96871 modified in function.imagefilledrectangle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167244@lists.php.net to get a copy of this message | ||
That script draws Serpinski's carpet:
<?php
set_time_limit(5);
$i = 4; // Iterations
$xy = 500; // Picture size
$img = imagecreatetruecolor($xy, $xy);
$white = imagecolorallocate($img, 255, 255, 255);
drawCarpet(0, 0, $xy, $xy, $i);
function drawCarpet($a, $b, $c, $d, $n) {
global $img, $white;
if($n <= 0) return;
$a1 = 2 * $a / 3 + $c / 3;
$c1 = $a / 3 + 2 * $c / 3;
$b1 = 2 * $b / 3 + $d / 3;
$d1 = $b / 3 + 2 * $d / 3;
imagefilledrectangle($img, $a1, $b1, $c1, $d1, $white);
drawCarpet($a, $b, $a1, $b1, $n - 1);
drawCarpet($a1, $b, $c1, $b1, $n - 1);
drawCarpet($c1, $b, $c, $b1, $n - 1);
drawCarpet($c1, $b, $c, $b1, $n - 1);
drawCarpet($a, $b1, $a1, $d1, $n - 1);
drawCarpet($c1, $b1, $c, $d1, $n - 1);
drawCarpet($a, $d1, $a1, $d, $n - 1);
drawCarpet($a1, $d1, $c1, $d, $n - 1);
drawCarpet($c1, $d1, $c, $d, $n - 1);
}
header('Content-Type: image/png');
imagepng($img);
?>
--was--
That script draws Serpinski's carpet:
<pre>
<?php
set_time_limit(5);
$i = 4; // Iterations
$xy = 500; // Picture size
$img = imagecreatetruecolor($xy, $xy);
$white = imagecolorallocate($img, 255, 255, 255);
drawCarpet(0, 0, $xy, $xy, $i);
function drawCarpet($a, $b, $c, $d, $n) {
global $img, $white;
if($n <= 0) return;
$a1 = 2 * $a / 3 + $c / 3;
$c1 = $a / 3 + 2 * $c / 3;
$b1 = 2 * $b / 3 + $d / 3;
$d1 = $b / 3 + 2 * $d / 3;
imagefilledrectangle($img, $a1, $b1, $c1, $d1, $white);
drawCarpet($a, $b, $a1, $b1, $n - 1);
drawCarpet($a1, $b, $c1, $b1, $n - 1);
drawCarpet($c1, $b, $c, $b1, $n - 1);
drawCarpet($c1, $b, $c, $b1, $n - 1);
drawCarpet($a, $b1, $a1, $d1, $n - 1);
drawCarpet($c1, $b1, $c, $d1, $n - 1);
drawCarpet($a, $d1, $a1, $d, $n - 1);
drawCarpet($a1, $d1, $c1, $d, $n - 1);
drawCarpet($c1, $d1, $c, $d, $n - 1);
}
header('Content-Type: image/png');
imagepng($img);
?>
</pre>
http://php.net/manual/en/function.imagefilledrectangle.php