RandomLinesBackgroundDrawer.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 RandomLinesBackgroundDrawer extends BackgroundDrawer
00016     {
00017         private $count = null;
00018         
00019         public function __construct($count)
00020         {
00021             $this->count = $count;
00022         }
00023         
00027         public function draw()
00028         {
00029             $imageId = $this->getTuringImage()->getImageId();
00030             
00031             $height = $this->getTuringImage()->getHeight();
00032             $width = $this->getTuringImage()->getWidth();
00033             
00034             for ($i = 0; $i < $this->count; ++$i) {
00035                 $color = $this->makeColor();
00036                 $colorId = $this->getTuringImage()->getColorIdentifier($color);
00037                 
00038                 $y = mt_rand(1, $height - 1);
00039                 $x = mt_rand(1, $width - 1);
00040                 
00041                 $angle = mt_rand(0, 180);
00042                 
00043                 while ($angle == 90)
00044                     $angle = mt_rand(0, 180);
00045                 
00046                 $angleRad = deg2rad($angle);
00047                 
00048                 $dy = ($width - $x) * tan($angleRad);
00049                 
00050                 if ($dy < $y) {
00051                     $xEnd = $width;
00052                     $yEnd = $y - $dy;
00053                 } else {
00054                     $yEnd = 0;
00055                     $xEnd = $x + tan($angleRad) / $y;
00056                 }
00057                 
00058                 $dy = $x * tan($angleRad);
00059                 
00060                 if ($dy <= ($height - $y)) {
00061                     $xStart = 0;
00062                     $yStart = $y + $dy;
00063                 } else {
00064                     $yStart = $height;
00065                     $xStart = $x - tan($angleRad) / ($height - $y);
00066                 }
00067                 
00068                 imageline(
00069                     $imageId,
00070                     $xStart,
00071                     $yStart,
00072                     $xEnd,
00073                     $yEnd,
00074                     $colorId
00075                 );
00076             }
00077             
00078             return $this;
00079         }
00080     }
00081 ?>