# Draw Triangle

```<?php

/**
* draw_triangle
*
* Draw a filled triangle.
*
* @version 0.4
* @author Contributors at eXorithm
* @link https://www.exorithm.com/algorithm/view/draw_triangle Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/draw_triangle History at eXorithm
*
* @param array \$points This should be a list of six numbers that define the points of the triange (x1, y1, x2, y2, x3, y3)
* @param string \$color (hex color code) The color of the triangle
* @return resource GD image
*/
function draw_triangle(\$points=array(0=>'0',1=>'0',2=>'0',3=>'8',4=>'8',5=>'4'),\$color='000000')
{
if (count(\$points)!=6) {
throw new Exception('The points must be an array of 6 integers.');
}

\$image = image_create_alpha(max(\$points[0], \$points[2], \$points[4])+1, max(\$points[1], \$points[3], \$points[5])+1);

\$red = hexdec(substr(\$color, 0, 2));
\$green  = hexdec(substr(\$color, 2, 2));
\$blue  = hexdec(substr(\$color, 4, 2));

\$color = imagecolorallocatealpha(\$image, \$red, \$green, \$blue, 0);

imagefilledpolygon(\$image, \$points, 3, \$color);

return \$image;
}

/**
* image_create_alpha
*
* Helper function to create a new blank image with transparency.
*
* @version 0.1
* @author Contributors at eXorithm
* @link https://www.exorithm.com/algorithm/view/image_create_alpha Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/image_create_alpha History at eXorithm
*
* @param mixed \$width
* @param mixed \$height
* @return resource GD image
*/
function image_create_alpha(\$width='',\$height='')
{
// Create a normal image and apply required settings
\$img = imagecreatetruecolor(\$width, \$height);
imagealphablending(\$img, false);
imagesavealpha(\$img, true);

// Apply the transparent background
\$trans = imagecolorallocatealpha(\$img, 0, 0, 0, 127);
for (\$x = 0; \$x < \$width; \$x++)
{
for (\$y = 0; \$y < \$height; \$y++)
{
imagesetpixel(\$img, \$x, \$y, \$trans);
}
}

return \$img;
}

?>
```