Page 1 of 1

Imagepolygon tools

Posted: Sun Jan 23, 2005 3:30 pm
by onion2k
Imagepolygon() and imagefilledpolygon() both take an array of points. Sometimes when you're using these you want to move the polygon around and do stuff to it, but it can be tricky to work out the new coordinates.. not any more!

Code: Select all

function translatePolygon($points,$x=0,$y=0) {
		for($i=0;$i<count($points);$i+=2) &#123;
			$points&#1111;$i] = $points&#1111;$i] + $x;
			$points&#1111;$i+1] = $points&#1111;$i+1] + $y;
		&#125;
		return $points;
	&#125;

	function scalePolygon($points,$factor=1) &#123;
		for ($x=0;$x<count($points);$x++) &#123;
			$points&#1111;$x] = $points&#1111;$x]*$factor;
		&#125;
		return $points;
	&#125;

	function rotatePolygon($points,$degrees=0) &#123;
		$cos = cos(deg2rad($degrees));
		$sin = sin(deg2rad($degrees));
		for($i=0;$i<count($points);$i+=2) &#123;
			$minx = ($points&#1111;$i]<$minx or !isset($minx)) ? $points&#1111;$i] : $minx;
			$miny = ($points&#1111;$i+1]<$miny or !isset($miny)) ? $points&#1111;$i+1] : $miny;
			$maxx = ($points&#1111;$i]>$maxx or !isset($maxx)) ? $points&#1111;$i] : $maxx;
			$maxy = ($points&#1111;$i+1]>$maxy or !isset($maxy)) ? $points&#1111;$i+1] : $maxy;
		&#125;
		$mx = ($maxx-$minx)/2;
		$my = ($maxy-$miny)/2;
		for($i=0;$i<count($points);$i+=2) &#123;
			$x = $points&#1111;$i] - $mx - $minx;
			$y = $points&#1111;$i+1] - $my - $miny;
			$xdif = $cos*($x) - $sin*($y);
			$ydif = $sin*($x) + $cos*($y);
			$points&#1111;$i] = $minx + $xdif;
			$points&#1111;$i+1] = $miny + $ydif;
		&#125;
		return $points;
	&#125;
And a script that demonstrates the use of these handy functions:

Code: Select all

$points = array(
					10,  0,
					20,  0,
					20,  10,
					30,  10,
					30,  20,
					40,  20,
					40,  30,
					30,  30,
					30,  40,
					20,  40,
					20,  30,
					10,  30,
					10,  20,
					0,   20,
					0,   10,
					10,  10
				);

	$i = imagecreatetruecolor(400,400);
	$white = imagecolorallocate($i,255,255,255);
	imagefill($i,0,0,$white);
	$red = imagecolorallocate($i,255,0,0);

	$factor = 3;
	$x = 100;
	$y = 100;
	$degrees = 45;

	$points = scalePolygon($points,$factor);
	$points = translatePolygon($points,$x,$y);
	$points = rotatePolygon($points,$degrees);

	imagefilledpolygon($i,$points,(count($points)/2),$red);

	header("Content-type: image/jpeg");
	imagejpeg($i);

	function translatePolygon($points,$x=0,$y=0) &#123;
		for($i=0;$i<count($points);$i+=2) &#123;
			$points&#1111;$i] = $points&#1111;$i] + $x;
			$points&#1111;$i+1] = $points&#1111;$i+1] + $y;
		&#125;
		return $points;
	&#125;

	function scalePolygon($points,$factor=1) &#123;
		for ($x=0;$x<count($points);$x++) &#123;
			$points&#1111;$x] = $points&#1111;$x]*$factor;
		&#125;
		return $points;
	&#125;

	function rotatePolygon($points,$degrees=0) &#123;
		$cos = cos(deg2rad($degrees));
		$sin = sin(deg2rad($degrees));
		for($i=0;$i<count($points);$i+=2) &#123;
			$minx = ($points&#1111;$i]<$minx or !isset($minx)) ? $points&#1111;$i] : $minx;
			$miny = ($points&#1111;$i+1]<$miny or !isset($miny)) ? $points&#1111;$i+1] : $miny;
			$maxx = ($points&#1111;$i]>$maxx or !isset($maxx)) ? $points&#1111;$i] : $maxx;
			$maxy = ($points&#1111;$i+1]>$maxy or !isset($maxy)) ? $points&#1111;$i+1] : $maxy;
		&#125;
		$mx = ($maxx-$minx)/2;
		$my = ($maxy-$miny)/2;
		for($i=0;$i<count($points);$i+=2) &#123;
			$x = $points&#1111;$i] - $mx - $minx;
			$y = $points&#1111;$i+1] - $my - $miny;
			$xdif = $cos*($x) - $sin*($y);
			$ydif = $sin*($x) + $cos*($y);
			$points&#1111;$i] = $minx + $xdif;
			$points&#1111;$i+1] = $miny + $ydif;
		&#125;
		return $points;
	&#125;
Cool!

Posted: Sun Jan 30, 2005 11:27 am
by onion2k
Fixed a slightly silly bug. It'll now rotate anything.

Code: Select all

function rotatePolygon($points,$degrees=0) &#123;

		$cos = cos(deg2rad($degrees));
		$sin = sin(deg2rad($degrees));
		for($i=0;$i<count($points);$i+=2) &#123;
			$minx = ($points&#1111;$i]<$minx or !isset($minx)) ? $points&#1111;$i] : $minx;
			$miny = ($points&#1111;$i+1]<$miny or !isset($miny)) ? $points&#1111;$i+1] : $miny;
			$maxx = ($points&#1111;$i]>$maxx or !isset($maxx)) ? $points&#1111;$i] : $maxx;
			$maxy = ($points&#1111;$i+1]>$maxy or !isset($maxy)) ? $points&#1111;$i+1] : $maxy;
		&#125;
		$mx = ($maxx-$minx)/2;
		$my = ($maxy-$miny)/2;
		for($i=0;$i<count($points);$i+=2) &#123;
			$x = $points&#1111;$i] - $minx - $mx;
			$y = $points&#1111;$i+1] - $miny - $my;
			$xdif = $cos*($x) - $sin*($y);
			$ydif = $sin*($x) + $cos*($y);
			$points&#1111;$i] = round($xdif + $minx + $mx);
			$points&#1111;$i+1] = round($ydif + $miny + $my);
		&#125;
		return $points;
	&#125;