Location: PHPKode > scripts > splx_graph > splx_graph.php
<?php
/**
 * splx_graph.php created on 14/5/09
 *
 * @author Alessandro Vernassa <hide@address.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 */
/**
 * 
 * @author Alessandro Vernassa <hide@address.com>
 *
 */
class splx_draw
{
	var $defaultcolor;
	var $x;
	var $y;
	/**
	 * 
	 * @param $csscolor
	 */
	function splx_draw($csscolor = "#000000")
	{
		$this->defaultcolor = $csscolor;
		$this->x = 0;
		$this->y = 0;
	}
	/**
	 * 
	 * @param $csscolor
	 */
	function setcolor($csscolor)
	{
		$this->defaultcolor = $csscolor;
	}
	/**
	 * 
	 * @param $x
	 * @param $y
	 */
	function moveto($x, $y)
	{
		$this->x = $x;
		$this->y = $y;
	}
	/**
	 * 
	 * @param $x
	 * @param $y
	 */
	function lineto($x, $y)
	{
		$this->line($this->x,$this->y,$x,$y);
		$this->x = $x;
		$this->y = $y;
	}
	/**
	 * 
	 * @param int $x
	 * @param int $y
	 * @param string $csscolor
	 */
	function set_pixel($x, $y, $csscolor = "")
	{
		$csscolor = ($csscolor == "") ? $this->defaultcolor : $csscolor;
		echo "\n<div style=\"border:0px;position:absolute;left:{$x}px;top:{$y}px;height:1px;width:1px;overflow:hidden;background-color:{$csscolor}\"></div>";
	}
	/**
	 * 
	 * @param string $x
	 * @param string $y
	 * @param string $w
	 * @param string $h
	 * @param string $csscolor
	 */
	function rectangle($x, $y, $w, $h, $csscolor = "")
	{
		$csscolor = ($csscolor == "") ? $this->defaultcolor : $csscolor;
		$this->line($x,$y,$x + $w,$y,$csscolor);
		$this->line($x,$y,$x,$y + $h,$csscolor);
		$this->line($x + $w,$y,$x + $w,$y + $h,$csscolor);
		$this->line($x,$y + $h,$x + $w,$y + $h,$csscolor);
	}
	/**
	 * 
	 * @param int $x1
	 * @param int $y1
	 * @param int $x2
	 * @param int $y2
	 * @param string $csscolor
	 */
	function line($x1, $y1, $x2, $y2, $csscolor = "")
	{
		$csscolor = ($csscolor == "") ? $this->defaultcolor : $csscolor;
		if ( $x1 > $x2 )
		{
			$t = $x1;
			$x1 = $x2;
			$x2 = $t;
			
			$t = $y1;
			$y1 = $y2;
			$y2 = $t;
		}
		$stepy = 1;
		if ( $y1 > $y2 )
		{
			$stepy =  -1;
		}
		$dx = abs($x2 - $x1);
		$dy = abs($y2 - $y1);
		//vertical line
		if ( $x1 == $x2 )
		{
			if ( $dy == 0 )
				$dy = 1;
			echo "\n<div style=\"border:0px;position:absolute;left:{$x1}px;top:{$y1}px;height:{$dy}px;width:1px;overflow:hidden;background-color:{$csscolor}\"></div>";
			return;
		}
		//horizontal line
		if ( $y1 == $y2 )
		{
			if ( $dx == 0 )
				$dx = 1;
			echo "\n<div style=\"border:0px;position:absolute;left:{$x1}px;top:{$y1}px;height:1px;width:{$dx}px;overflow:hidden;background-color:{$csscolor}\"></div>";
			return;
		}
		$stepx = $dx / $dy;
		$startx = $x1;
		for ( $iy = $y1;$iy != $y2;$iy += $stepy )
		{
			$endx = $startx + $stepx;
			$this->line(intval($startx),$iy,intval($endx),$iy,$csscolor);
			$startx = $endx;
		}
	}
	/**
	 * 
	 * @param $xcenter
	 * @param $ycenter
	 * @param $radius
	 * @param $csscolor
	 */
	function circle($xcenter, $ycenter, $radius, $csscolor = "")
	{
		$csscolor = ($csscolor == "") ? $this->defaultcolor : $csscolor;
		$r2 = $radius * $radius;
		$oldx = 0;
		$oldy = 0;
		$oldy2 = 0;
		for ( $x =  -($radius);$x <= $radius;$x++  )
		{
			$y = intval((sqrt($r2 - $x * $x)) + 0.5);
			$newx = $x + $xcenter;
			$newy = $y + $ycenter;
			$newy2 = $ycenter - $y;
			if ( $oldx == 0 && $oldy == 0 )
			{
				$this->set_pixel($newx,$newy,$csscolor);
				$this->set_pixel($newx,$newy2,$csscolor);
			}
			else
			{
				$this->line($oldx,$oldy,$newx,$newy,$csscolor);
				$this->line($oldx,$oldy2,$newx,$newy2,$csscolor);
			}
			$oldx = $newx;
			$oldy = $newy;
			$oldy2 = $newy2;
		}
	}
	/**
	 * 
	 * @param array $poynts
	 * @param string $csscolor
	 */
	function polygon($points, $csscolor = "")
	{
		$csscolor = ($csscolor == "") ? $this->defaultcolor : $csscolor;
		$x1 = $points[0]['x'];
		$y1 = $points[0]['y'];
		for ( $i = 1;isset($points[$i]);$i++  )
		{
			$this->line($x1,$y1,$points[$i]['x'],$points[$i]['y'],$csscolor);
			$x1 = $points[$i]['x'];
			$y1 = $points[$i]['y'];
		}
		$this->line($x1,$y1,$points[0]['x'],$points[0]['y'],$csscolor);
	}
}
?>
Return current item: splx_graph