Affichage d'une fonction mathématique sous forme de graphique

Contenu du snippet

voila une application php permettant d'afficher une fonction php de votre choix.

paramètres:

fonction: la fonction, necessite une syntaxe PHP

axex : nombre de pixel pour 1 unité sur laxe X

axey : nombre de pixel pour 1 unité sur laxe X

Source / Exemple :


<?
function graphe($formule, $axex, $axey){
$param[height] = "500";
$param[width] = "500";
$param[gradx] = "20"; //nbr de pixel pour 1 de largeur
$param[grady] = "20"; //nbr de pixel pour 1 de hauteur
$pos_top = "20";
$im = ImageCreate($param[width], $param[height]);
if ($axex != "" AND $axex >= "5" AND $axex <= "100"){ $param[gradx] = $axex; }
else { $param[gradx] = "20"; }
if ($axey != "" AND $axey >= "5" AND $axey <= "100"){ $param[grady] = $axey; }
else { $param[grady] = "20"; }
if ($formule  == "") $formule = 'x*x';

$formule = str_replace(' ', '+', $formule);

//Traitement de la formule
$formule1 = str_replace('x', '$x', $formule);
$formule2 = str_replace('x', '$x2', $formule);

function puis($x, $exposant){
$x2 = $x;
for ($i = "0"; $i< $exposant;$i++){
$x=$x*$x2;
}
return $x;
}

//colors
$white = ImageColorAllocate ($im, 255,255,255);
$black = ImageColorAllocate ($im, 0,0,0);
$red = ImageColorAllocate ($im, 220,0,0);
$backgr = ImageColorAllocate ($im, 220,220,220);
$grey_title = ImageColorAllocate ($im, 190,190,190);
$grey_sombre = ImageColorAllocate ($im, 100,50,50);
$quadrillage = ImageColorAllocate ($im, 200,200,200);
$axe_blue = ImageColorAllocate ($im, 193,161,114);
$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);
		

	//Axes
	ImageLine($im, ($param[width]/2), (30+$pos_top), ($param[width]/2), (($param[height]-180)+$pos_top), $axe_blue);
	ImageLine($im, 100, (((($param[height]-210)/2)+30)+$pos_top), ($param[width]-100), (((($param[height]-210)/2)+30)+$pos_top), $axe_blue);
		//fleches
		ImageLine($im, ($param[width]/2), (30+$pos_top), (($param[width]/2)-5), (40+$pos_top), $axe_blue);
		ImageLine($im, ($param[width]/2), (30+$pos_top), (($param[width]/2)+5), (40+$pos_top), $axe_blue);
		ImageLine($im, ($param[width]-100), (((($param[height]-210)/2)+30)+$pos_top), (($param[width]-100)-10), ((((($param[height]-210)/2)+30)+5)+$pos_top), $axe_blue);
		ImageLine($im, ($param[width]-100), (((($param[height]-210)/2)+30)+$pos_top), (($param[width]-100)-10), ((((($param[height]-210)/2)+30)-5)+$pos_top), $axe_blue);

$center_width = (($param[width]-200)/2)+100;
$center_height = ((($param[height]-210)/2)+30+$pos_top);

for ($i = 1; $i < "200"; $i++ ){
if ($i*$param[gradx] < "150"){
//quadrillage + graduage
ImageLine($im, 100, (((($param[height]-210)/2)+30)+$pos_top+($i*$param[grady])), ($param[width]-100), (((($param[height]-210)/2)+30)+$pos_top+($i*$param[grady])), $quadrillage);
ImageLine($im, 100, (((($param[height]-210)/2)+30)+$pos_top-($i*$param[grady])), ($param[width]-100), (((($param[height]-210)/2)+30)+$pos_top-($i*$param[grady])), $quadrillage);
ImageLine($im, (($param[width]/2)-($i*$param[gradx])), (30+$pos_top), (($param[width]/2)-($i*$param[gradx])), (($param[height]-180)+$pos_top), $quadrillage);
ImageLine($im, (($param[width]/2)+($i*$param[gradx])), (30+$pos_top), (($param[width]/2)+($i*$param[gradx])), (($param[height]-180)+$pos_top), $quadrillage);

ImageLine($im, ($center_width+($i*$param[gradx])), ($center_height-2), ($center_width+($i*$param[gradx])), ($center_height+2), $axe_blue);
ImageLine($im, ($center_width+($i*-$param[gradx])), ($center_height-2), ($center_width+($i*-$param[gradx])), ($center_height+2), $axe_blue);
}
if ($i*$param[grady] < "150"){
ImageLine($im, ($center_width-2), ($center_height+($i*$param[grady])), ($center_width+2), ($center_height+($i*$param[grady])), $axe_blue);
ImageLine($im, ($center_width-2), ($center_height+($i*-$param[grady])), ($center_width+2), ($center_height+($i*-$param[grady])), $axe_blue);

}
}
//texts
	ImageString($im, 3, 130, 10, "** Mathematical Function Shower **", $grey_title);
	ImageString($im, 3, 10, 35+320, "Entries:", $black);
	ImageString($im, 2, 10, 55+320, "function : ", $grey_sombre);
	ImageString($im, 2, 90, 55+320, "$formule", $grey_sombre);
	ImageString($im, 2, 10, 75+320, "Echelle [x] : 1/$param[gradx]", $box_border_top);
	ImageString($im, 2, 10, 90+320, "Echelle [y] : 1/$param[grady]", $box_border_top);
	ImageString($im, 3, 250, 480, " v.1 (c) by Colder - www.colder.ch", $grey_title);
	
//affichage des points
for ($i = "-140"; $i < "140"; $i++ ){
$x = (1/$param[gradx])*$i;
$x2 = (1/$param[gradx])*($i-1);
 eval( "\$result_op = $formule1;" );
 eval( "\$result_op_back = $formule2;" );
$result_op = $result_op*$param[grady]*(-1);
$result_op_back = $result_op_back*$param[grady]*(-1);
if ($result_op < 140 AND $result_op > -140 AND $result_op_back < 140 AND $result_op_back > -140 ){
ImageLine($im, ($center_width+$i), ($center_height+$result_op), ($center_width+($i-1)), ($center_height+$result_op_back), $black);
}
}

//affichange de l'image
Header ("Content-type: image/jpeg");
ImageJpeg($im);

ImageDestroy($im);
}
graphe($formule, $axex, $axey);
?>

Conclusion :


petite imprécision détectée avec la fonction x

j'ai absolument aucune idée d'ou ca peut venir.

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.

Du même auteur (cs_Colder)