Zend - The PHP Company




Graphics

Add Code


AutoMondrian  

Type: code fragment
Added by: onion2k
Entered: 24/06/2004
Last modified: 07/12/2003
Rating: ***** (3 votes)
Views: 4547
Generate "Mondrian" art randomly.


<?php

    srand
((double)microtime()*1000000);

    
$output imagecreate(400,400);

    
$white imagecolorallocate($output,255,255,255);
    
$black imagecolorallocate($output,0,0,0);
    
$fill[] = imagecolorallocate($output,255,0,0);
    
$fill[] = imagecolorallocate($output,255,255,0);
    
$fill[] = imagecolorallocate($output,0,0,255);

    
imagesetthickness($output,2);
    
imagefill($output,0,0,$white);
    
$segments rand(2,6);

    
$building 0;
    
$counter 0;

    while (
$building == 0) {

        if (
$c++ > 20 and count($block) > 0) { $building 1; }
        if (
count($block) >= $segments)      { $building 1; }

        
$x rand(1,39)*10;
        
$y rand(1,39)*10;
        
$w rand(3,10)*10;
        
$h rand(3,10)*10;

        
$add 0;

        if (
is_array($block)) {
            foreach (
$block as $bl) {

                if (  !( (
$x     >=$bl['x']) and ($x     <=($bl['x']+$bl['w'])) )
                  and !( ((
$x+$w)>=$bl['x']) and (($x+$w)<=($bl['x']+$bl['w'])) )
                  and !( (
$x     <=$bl['x']) and (($x+w) >=($bl['x']+$bl['w'])) )
                  and !( (
$y     >=$bl['y']) and ($y     <=($bl['y']+$bl['h'])) )
                  and !( ((
$y+$h)>=$bl['y']) and (($y+$h)<=($bl['y']+$bl['h'])) )
                  and !( (
$y     <=$bl['y']) and (($y+h) >=($bl['y']+$bl['h'])) )
                   )
                {
                    
$add 1;
                } else {
                    
$add 0;
                    break;
                }

            }
        } else {
            
$add 1;
        }

        if (
$add == 1) {
            
$block[$counter]['x'] = $x;
            
$block[$counter]['y'] = $y;
            
$block[$counter]['w'] = $w;
            
$block[$counter]['h'] = $h;
            
$counter++;
        }
    }

    for (
$counter=0;$counter<$segments;$counter++) {
        if (
$block[$counter]['x'] > or $block[$counter]['y'] > 0) {
            
imageline($output,0,$block[$counter]['y'],400,$block[$counter]['y'],$black);
            
imageline($output,0,$block[$counter]['y']+$block[$counter]['h'],400,$block[$counter]['y']+$block[$counter]['h'],$black);
            
imageline($output,$block[$counter]['x'],0,$block[$counter]['x'],400,$black);
            
imageline($output,$block[$counter]['x']+$block[$counter]['w'],0,$block[$counter]['x']+$block[$counter]['w'],400,$black);
        }
    }

    for (
$counter=0;$counter<$segments;$counter++) {
        
imagefilledrectangle($output,$block[$counter]['x']+1,$block[$counter]['y']+1,$block[$counter]['x']+$block[$counter]['w']-2,$block[$counter]['y']+$block[$counter]['h']-2,$fill[rand(0,2)]);
    }


    
header("Content-Type: image/png");
    
imagepng($output);

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories