# Draw a triangle with GD?

Hi all experts
I want draw a triangle with GD library with PHP. One soulation is using imagefilledpolygon function for draw a filled polygon. The second parameters in this function is an array containing the x and y coordinates of the polygons vertices consecutively. But I wnat draw with lenght of side! Type of triangles and their angles not important.

For example with three value in pixel unit I have a triangle in output!
Anyone help me?

you can use imageline() function.

Have a sample? I want fill triangle with a RGB color…

so, why imagefilledpolygon won’t suit you?

Here’s the PHP manual page for imagefilledpolygon that demonstrates its use.

I described in my first post:
The second parameters in this function is an array containing the x and y coordinates of the polygons vertices consecutively. But I wnat draw with lenght of side! Type of triangles and their angles not important.
For example with three value in pixel unit I have a triangle in output!

You will need then to convert things to pixel units in order for PHP to draw the polygons.

well, even for the imageline function, you need a set of x/y coordinates for the beginning of a line, and another for the end, so that won’t help you, either. I suppose you can create your own function that takes a starting point, length in pixels, and an angle to draw the line in, but that’s an awful lot of math to do, with sine, cosine and tangent (in some cases) calculations. Sounds like a lot of work.

Off Topic:

Go to bed, Paul. How can you expect me to catch your post count, if you stay up and post more? In fact, you deserve a break. Take a week off. You deserve it. Better yet, take 2. You already have two points, just need a little math to calc third

``````\$triangle = array(300, 400, 500);
sort(\$triangle);

\$c = \$triangle; // base
\$b = \$triangle; // sides
\$a = \$triangle;

// calculate angle, cosine rule
\$alpha = acos((pow(\$b,2) + pow(\$c,2) - pow(\$a,2)) / (2 * \$b * \$c));

// pythag to calc height and y distance
\$height = abs(sin(\$alpha)) * \$b;
\$width = abs(cos(\$alpha)) * \$b;

\$x = 20; // start point
\$y = 580;

\$points = array(
\$x, \$y,				// start
\$x+\$c, \$y,				// base
\$x+\$width, \$y-\$height 	// apex
);

// draw
\$image = imagecreatetruecolor(600, 600);
\$blue = imagecolorallocate(\$image, 0, 0, 255);
imagefilledpolygon(\$image, \$points, 3, \$blue);