CircleBackgroundDrawer.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Dmitry E. Demidov                          *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class CircleBackgroundDrawer extends BackgroundDrawer
00016     {
00017         const VERTEX_COUNT  = 20;
00018         
00019         private $minRadius  = null;
00020         private $maxRadius  = null;
00021         private $count      = null;
00022     
00023         public function __construct($count, $minRadius, $maxRadius = null)
00024         {
00025             if ($maxRadius === null)
00026                 $maxRadius = $minRadius;
00027             
00028             $this->maxRadius = $maxRadius;
00029             $this->minRadius = $minRadius;
00030             $this->count = $count;
00031         }
00032         
00036         public function draw()
00037         {
00038             for ($i = 0; $i < $this->count; ++$i) {
00039                 $y = mt_rand(0, $this->getTuringImage()->getHeight());
00040                 $x = mt_rand(0, $this->getTuringImage()->getWidth());
00041                 
00042                 $radius = mt_rand($this->minRadius, $this->maxRadius);
00043             
00044                 $this->drawCircle($x, $y, $radius);
00045             }
00046             
00047             return $this;
00048         }
00049 
00050         /* void */ private function drawCircle($x, $y, $radius)
00051         {
00052             $vertexArray = array();
00053             
00054             $angleStep = 360 / CircleBackgroundDrawer::VERTEX_COUNT;
00055             $angle = 0;
00056             
00057             for ($i = 0; $i < CircleBackgroundDrawer::VERTEX_COUNT; ++$i) {
00058                 $color = $this->makeColor();
00059                 $colorId = $this->getTuringImage()->getColorIdentifier($color);
00060                 
00061                 $angleRad = deg2rad($angle);
00062                 
00063                 $dx = sin($angleRad) * $radius;
00064                 $dy = cos($angleRad) * $radius;
00065                 
00066                 $vertexArray[] = $x + $dx;
00067                 $vertexArray[] = $y + $dy;
00068                 
00069                 $angle += $angleStep;
00070             }
00071             
00072             imagefilledpolygon(
00073                 $this->getTuringImage()->getImageId(),
00074                 $vertexArray,
00075                 CircleBackgroundDrawer::VERTEX_COUNT,
00076                 $colorId
00077             );
00078         }
00079     }
00080 ?>