Affichage d'une ellipse configurable.

Contenu du snippet

Bon ce code crée l'image d'une ellipse , que l'on peut parametrer sans pêine.

il sagit dune simple fonction : qui a comme argument l'axe A et lexentricité E

Source / Exemple :


<?
function ellipse($a, $e){
$param[height] = "500";
$param[width] = "500";
if ($e == "") $e = "0.8";
if ($a == "") $a = "100";

$c = $e*$a;
$b = sqrt(($a*$a)-($c*$c));

$im = ImageCreate($param[width], $param[height]);

//colors
$black = ImageColorAllocate ($im, 0,0,0);
$red = ImageColorAllocate ($im, 220,0,0);
$green = ImageColorAllocate ($im, 0,100,0);
$blue = ImageColorAllocate ($im, 0,0,220);
$backgr = ImageColorAllocate ($im, 220,220,220);
$grey_title = ImageColorAllocate ($im, 190,190,190);
$grey_sombre = ImageColorAllocate ($im, 100,50,50);
$box_border_top = ImageColorAllocate ($im, 100,100,100);
$box_border_left = $box_border_top;
$box_border_right = ImageColorAllocate ($im, 50,50,50);
$box_border_bottom = $box_border_right;
//Box
ImageFill($im, 0, 0, $backgr);
	//border
	ImageLine($im, 0, 0, 0, $param[height], $box_border_top);
	ImageLine($im, 0, 0, $param[width], 0, $box_border_left);
	ImageLine($im, ($param[width]-1), 0, ($param[width]-1), ($param[height]-1), $black);
	ImageLine($im, ($param[width]-1), ($param[height]-1), 0, ($param[height]-1), $black);
	
	//text
	ImageString($im, 3, 210, 10, "** Ellipse **", $grey_title);
	ImageString($im, 2, 10, 35, "exentricité (e) : $e", $grey_sombre);
	ImageString($im, 2, 10, 55, "grand axe : ".(2*$a), $grey_sombre);
	ImageString($im, 2, 10, 75, "petit axe : ".(2*$b), $grey_sombre);
	ImageString($im, 2, 10, 95, "distance focale : ".(2*$c), $grey_sombre);
	ImageString($im, 3, 250, 480, " v.1 (c) by Colder - www.colder.ch", $grey_title);

	//center show
	ImageLine($im, ($param[width]/2), ($param[height]/2)-3, ($param[width]/2), ($param[height]/2)+3, $box_border_top);
	ImageLine($im, ($param[width]/2)-3, ($param[height]/2), ($param[width]/2)+3, ($param[height]/2), $box_border_top);
	
	//point view
	ImageLine($im, ($param[width]/2), ($param[height]/2)+$b, ($param[width]/2), ($param[height]/2)+$b, $red);
	ImageLine($im, ($param[width]/2), ($param[height]/2)-$b, ($param[width]/2), ($param[height]/2)-$b, $red);
	ImageLine($im, ($param[width]/2)+$c, ($param[height]/2), ($param[width]/2)+$c, ($param[height]/2), $green);
	ImageLine($im, ($param[width]/2)-$c, ($param[height]/2), ($param[width]/2)-$c, ($param[height]/2), $green);
	ImageLine($im, ($param[width]/2)+$a, ($param[height]/2), ($param[width]/2)+$a, ($param[height]/2), $blue);
	ImageLine($im, ($param[width]/2)-$a, ($param[height]/2), ($param[width]/2)-$a, ($param[height]/2), $blue);

	for ($i = -1*$a; $i < "0"; $i ++){
	$y = sqrt(($b*$b)*(1- (($i*$i)/($a*$a))));
	$y2 = sqrt(($b*$b)*(1- ((($i+1)*($i+1))/($a*$a))));
	ImageLine($im, ($param[width]/2)+$i, ($param[height]/2)+$y, ($param[width]/2)+$i, ($param[height]/2)+$y2, $red);
	ImageLine($im, ($param[width]/2)+$i, ($param[height]/2)-$y, ($param[width]/2)+$i, ($param[height]/2)-$y2, $red);
	} 
	for ($i = $a; $i > "0"; $i--){
	$y = sqrt(($b*$b)*(1- (($i*$i)/($a*$a))));
	$y2 = sqrt(($b*$b)*(1- ((($i-1)*($i-1))/($a*$a))));
	ImageLine($im, ($param[width]/2)+$i, ($param[height]/2)+$y, ($param[width]/2)+$i, ($param[height]/2)+$y2, $red);
	ImageLine($im, ($param[width]/2)+$i, ($param[height]/2)-$y, ($param[width]/2)+$i, ($param[height]/2)-$y2, $red);
	} 
Header ("Content-type: image/png");
ImagePng($im);

ImageDestroy($im);
}
ellipse("100", "0.5");
?>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.