Victory Road  

Go Back   Victory Road > General > Technology

Notices

 
 
Search this Thread
  #1  
Old September 21, 2008, 09:14:42 PM
SpaceMan++'s Avatar
SpaceMan++ SpaceMan++ is offline
Zoroark
 
Join Date: Aug 2008
Location: BC
Posts: 288
Default PHP Polygon Math

I made a PHP script that deals with polygon.
Code:
<?php
class Point
{
	public $x;
	public $y;

	//creates a Point.
	public function __construct($x=0, $y=0)
	{
		$this -> x = $x;
		$this -> y = $y;
	}

	//convert to string
	public function __toString()
	{
		return(
"Point:
 - X: {$this -> x}
 - Y: {$this -> y}");
	}

}

class Line
{
	public $point_1 = NULL;
	public $point_2 = NULL;

	//creates a line
	public function __construct($p1, $p2)
	{
		$this -> point_1 = $p1;
		$this -> point_2 = $p2;
	}

	//convert to string
	public function __toString()
	{
		return(
"Line:
 - Point 1
   - X: {$this -> point_1 -> x}
   - Y: {$this -> point_1 -> y}
 - Point 2
   - X: {$this -> point_2 -> x}
   - Y: {$this -> point_2 -> y}

 - Width: {$this -> Width()}
 - Height: {$this -> Height()}
 - Length: {$this -> Length()}
   ");
	}

	//gets the width of the Bounding Box
	public function Width()
	{
		return $this -> point_2 -> x - $this -> point_1 -> x;
	}

	//gets the height of the Bounding Box
	public function Height()
	{
		return $this -> point_2 -> y - $this -> point_1 -> y;
	}

	//gets the length
	public function Length()
	{
		return (sqrt(pow(abs($this -> Width()), 2) + pow(abs($this -> Height()), 2)));
	}
}
class Polygon
{
	public $points = array(NULL);
	public $lines = array(NULL);

	//creates a polygon.
	//@param array points The list of points
	public function __construct($points)
	{
		$this -> points = $points;
		$this -> lines = $points;

		for($i = 0; $i <= count($points)-1; $i++)
		{
			$this -> lines[$i] = new Line($points[$i], $points[$i+1]);
		}
		$this -> lines[count($this -> lines)] -> point_2 = $points[0];
	}

	//converts to String
	public function __toString()
	{
		$i = 1;
		$str = "Polygon:\n";

		$str .=
" - Sides: {$this -> Points()}\n\n";

		//show all lines' info
		foreach ($this -> points as $v)
		{
			$str .=
" - Point $i:
   - X: ";
			$str .= $v -> x;
			$str .=
"\n   - Y: ";
			$str .= $v -> y;
			$str .= "\n";
		$i++;
		}

		$str .=
"\n - Area: {$this -> Area()}\n";
		$str .=
" - Perimeter: {$this -> Perimeter()}\n";
		return $str;
	}
	//calculates the area
	public function Area()
	{
		//export to $xs and $ys
		foreach ($this -> points as $v)
		{
			$xs[] = $v -> x;
			$ys[] = $v -> y;
		}
		if (count($xs) != count($ys))
		{
			throw new Exception("Invalid parameters");
			return false;
		}

		if (count($xs) < 3)
		{
			throw new Exception("This is a dot/line, not a polygon.");
			return false;
		}

		return (
				($this -> subCalculation($xs, $ys)) -
				($this -> subCalculation($ys, $xs))
				)/2;
	}

	//internal function
	private function subCalculation($a, $b)
	{
		$answer = 0;

		for ($i=0; $i<(count($a)-1); $i++)
		{
			$answer += ($a[$i] * $b[$i+1]);
		}

		$answer += $a[count($a)-1] * $b[0];
		return $answer;
	}

	//calculates the perimeter
	public function Perimeter()
	{
		$perimeter = 0;

		//loop to get Length() of all sides
		for($i = 0; $i < count($this -> lines)-1; $i++)
		{
			$perimeter += $this -> lines[$i] -> Length();
		}
		return $perimeter;
	}

	//returns array for GD polygon functions
	public function GDPolygon($offset_x=0,$offset_y=0)
	{
		$poly;
	
		for ($i = 0; $i < count($this -> points); $i++)
		{
			$poly[] = $this -> points[$i] -> x + $offset_x;
			$poly[] = $this -> points[$i] -> y + $offset_y;
			//var_dump($this -> points[$i] -> y);
		}

		return $poly;
	}
	
	//returns the numbers of points
	public function Points()
	{
		return count($this -> points);
	}
}

//example, remove when using, it generates an image
header("Content-Type: image/png");
$i = imagecreate(100,100);
$p = new Polygon(array(
					   new Point(0,0),
					   new Point(20,0),
					   new Point(20,20),
					   new Point(0,20),
					   ));
$bg = imagecolorallocate($i, 0xCC, 0xFF, 0xFF);
$fg = imagecolorallocate($i, 0x11,0x11,0x44);
$bg_2 = imagecolorallocate($i, 0x99, 0xAA,0xFF);

imagerectangle($i, 0, 0, 99, 59, $fg);
imagefill($i, 3, 3, $bg_2);
imagefilledpolygon($i, $p -> GDPolygon(5,5), $p -> Points(), $fg);
imagestring($i, 5, 1, 59, "Area: {$p -> Area()}", $fg);
imagestring($i, 5, 1, 71, "Perimeter:", $fg);
imagestring($i, 5, 1, 84, $p -> Perimeter(), $fg);
imagepng($i);
?>
It can calculate a Polygon's area, perimeter, and draw as PNG with GD lib
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump


All times are GMT -8.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Victory Road ©2006 - 2024, Scott Cat333Pokémon Cheney
Theme by A'bom and Cat333Pokémon