PHP: Generate Right-Angle Triangle


This is a little random, but below is some basic code to generate a random right angle triangle.

<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(250, 250);
$color = imagecolorallocate($im, 255, 255, 255);

do {
	$x1 = rand(0,250);
	$y1 = rand(0,250);

	$x2 = rand(0,250);
	$y2 = rand(0,250);

	$x3 = rand(0,250);
	$y3 = ((($x1 - $x2)*($x1 - $x3))/($y1 - $y2)) + $y1;
} while (($y3 < 0) OR ($y3 > 250));


imageline($im, $x1, $y1, $x2, $y2, $color);
imageline($im, $x1, $y1, $x3, $y3, $color);
imageline($im, $x2, $y2, $x3, $y3, $color);


imagepng($im);
imagedestroy($im);
?>

And in action:

 

Triangle

 

Leave a Reply