- <?php
-
- /*
- **************************************************
- Class: Polygon.php
- **************************************************
- Author: Tsiavos Chris <jaames@freemail.gr>
- Date: October 2004
- **************************************************/
-
- /**
- *Includes the abstract Shape class
- */
- require_once("Shape.php");
-
- /**
- *Wrapper around gd's Image(Filled)Polygon functions
- *Class used to draw polygons in the chart image
- *Utilized by AreaChart
- *@author Tsiavos Chris <jaames@freemail.gr>
- *@license http://opensource.org/licenses/gpl-license.php GNU Public License
- */
-
- class Polygon extends Shape {
-
- /**
- *the polygon's vertices
- *@access private
- *@var integer
- */
- private $Vertices=array();
-
- /**
- *reference to the font instance used by Polygon::draw_Caption()
- *@access private
- *@var Font
- */
- private $Font;
-
- /**
- *Array holding the caption of each of the polygon's vertices
- *@access private
- *@var private
- */
- private $Caption=array();
-
- /**
- *the font color used to draw the caption of the polygon's vertices
- *@access private
- *@var private
- */
- private $CaptionColor;
-
- /**
- *Constructor
- *@param mixed &$Canvas Reference to the image handler the font will be used in
- *@param ColorAllocator Reference to the ColorAllocator the class will use for allocating
- *tthe font color
- *@param string ("Yes","No") Specifies if antialias functions should be used or not when drawing
- *tthe polygon
- */
- function __construct(&$Canvas,ColorAllocator &$ColorAllocator,$UseAntialias) {
- Shape::__construct($Canvas,$ColorAllocator,$UseAntialias);
- }
-
- /**
- *Adds a vertex to the polygon
- *@access public
- *@return void
- */
- public function add_Vertex($Vertex) {
- array_push($this->Vertices,$Vertex);
- }
-
- /**
- *Returns the polygon's vertices
- *@access public
- *@return string[]
- */
- public function get_Vertices() {
- return $this->Vertices;
- }
-
- /**
- *Specifies the font properties used to draw the captions of the polygon's vertices
- *@access public
- *@return void
- *@param Font &$Font
- *@param string $CaptionColor
- */
- public function set_CaptionProperties(Font &$Font,$CaptionColor) {
- $this->Font=$Font;
- $this->CaptionColor=$CaptionColor;
- }
-
- /**
- *Sets the caption for a vertex
- *@access public
- *@return void
- */
- public function set_CaptionVertex($Caption) {
- array_push($this->Caption,$Caption);
- }
-
- /**
- *Draws the caption for each vertex of the polygon
- *@access private
- *@return void
- */
- private function draw_Caption() {
- if (!($this->Font instanceof Font)) return;
-
- for ($vertex=0;$vertex<count($this->Vertices)-2;$vertex++) {
-
- $FontMetrics=$this->Font->get_FontMetrics($this->Caption[$vertex]);
-
- $VertexPos=explode(",",$this->Vertices[$vertex]);
- $CaptionXPos=$VertexPos[0]-($FontMetrics["FontWidth"]/2);
-
- $CaptionYPos=$VertexPos[1];
- $ZeroPos=explode(",",$this->Vertices[count($this->Vertices)-1]);
-
- if ($CaptionYPos<$ZeroPos[1])
- $CaptionYPos=$VertexPos[1]-$FontMetrics["FontHeight"]-2;
- else
- $CaptionYPos=$VertexPos[1]+2;
-
- $this->Font->draw_String($CaptionXPos,$CaptionYPos,$this->Caption[$vertex],$this->CaptionColor);
- }
- }
-
- /**
- *Draws the border of the polygon
- *@access public
- *@return void
- *@param string $BorderColor
- *@param integer 0-127 $Alpha
- */
- public function draw($BorderColor,$Alpha) {
- if ($this->UseAntialias=="Yes")
- ImageAntialias($this->Canvas,1);
-
- $ProperVertices=array();
-
- for ($vertex=0;$vertex<count($this->Vertices);$vertex++) {
- $Vertex=explode(",",$this->Vertices[$vertex]);
- array_push($ProperVertices,$Vertex[0]);
- array_push($ProperVertices,$Vertex[1]);
- }
-
- ImagePolygon($this->Canvas,$ProperVertices,count($this->Vertices),$ColorHandler);
-
- if ($this->UseAntialias=="Yes")
- ImageAntialias($this->Canvas,0);
- }
-
- /**
- *Draws a filled polygon
- *@access public
- *@return void
- *@param string $StartColor specifies the starting color of the filled polygon
- *@param string $FinishColor specifies the finishing color of the filled polygon.
- *IIf the $StartColor is different from $FinishColor then the polygon will be filled
- *wwith gradient color
- *@param integer 0-127 $Alpha the alpha value of the polygon's color
- */
- public function draw_Filled($StartColor,$FinishColor,$Alpha) {
- $FirstVertex=explode(",",$this->Vertices[count($this->Vertices)-2]);
- $LastVertex=explode(",",$this->Vertices[count($this->Vertices)-1]);
-
- $Range=$LastVertex[0]-$FirstVertex[0];
- $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$StartColor,$FinishColor,$Alpha,$Range);
-
- if (is_array($ColorHandler))
- $this->draw_GradientPolygon($ColorHandler);
- else
- $this->draw_NormalPolygon($ColorHandler);
- }
-
- /**
- *Draws a uniform color filled polygon
- *@access private
- *@return void
- *@param $ColorHandler
- */
- private function draw_NormalPolygon($ColorHandler) {
- $ProperVertices=array();
-
- for ($vertex=0;$vertex<count($this->Vertices);$vertex++) {
- $Vertex=explode(",",$this->Vertices[$vertex]);
- array_push($ProperVertices,$Vertex[0]);
- array_push($ProperVertices,$Vertex[1]);
- }
-
- $this->draw_Caption();
- ImageFilledPolygon($this->Canvas,$ProperVertices,count($this->Vertices),$ColorHandler);
- }
-
- /**
- *Draws a gradient color filled polygon
- *@access private
- *@return void
- *@param $ColorHandler
- */
- private function draw_GradientPolygon($ColorHandler) {
- $ProperVertices=array();
- $CurrentColor=0;
-
- for ($vertex=0;$vertex<count($this->Vertices);$vertex++) {
- $Vertex=explode(",",$this->Vertices[$vertex]);
- array_push($ProperVertices,$Vertex[0]);
- array_push($ProperVertices,$Vertex[1]);
- }
-
- for ($i=0;$i<count($ProperVertices)-6;$i+=2) {
-
- $current=$i;
-
- $StartPointX=$ProperVertices[$i];
- $StartPointY=$ProperVertices[$i+1];
- $FinishPointX=$ProperVertices[$i+2];
- $FinishPointY=$ProperVertices[$i+3];
-
- $RangeX=$FinishPointX-$StartPointX;
- if ($RangeX==0) $RangeX=1;
-
- $Ratio=($FinishPointY-$StartPointY)/$RangeX;
-
- $YPos=$ProperVertices[count($ProperVertices)-1];
-
- for ($XPos=$StartPointX;$XPos<$FinishPointX;$XPos++)
- ImageLine($this->Canvas,$XPos,$StartPointY+$Ratio*($XPos-$StartPointX),$XPos,$YPos,$ColorHandler[$CurrentColor++]);
- }
-
- $this->draw_Caption();
- }
-
- }
-
- ?>