
<?
$debut = microtime(true);
$ext_loaded = get_loaded_extensions();
if (!in_array("gd", $ext_loaded)) {
exit('<strong> Pas de librairie GD sur ce serveur.</strong>');
}
else {
if (!is_numeric($_POST['echellex'])) { $echellex = 1; } else { $echellex = $_POST['echellex']; }
if (!is_numeric($_POST['echelley'])) { $echelley = 1; } else { $echelley = $_POST['echelley']; }
if (!is_numeric($_POST['origx']) && $_POST['origx'] != '0') { $origx = 100; } else { $origx = $_POST['origx']; }
if (!is_numeric($_POST['origy']) && $_POST['origy'] != '0') { $origy = 100; } else { $origy = $_POST['origy']; }
if (empty($_POST['equation'])) { $equa = 0; } else { $equa = $_POST['equation']; }
$im = imagecreatetruecolor(200,200);
$blanc = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$orange = imagecolorallocate($im, 0xFF, 0x90, 0x00);
$noir = imagecolorallocate($im, 0x00, 0x00, 0x00);
imageline($im, $origx, 0, $origx, 200, $orange);
imageline($im, 0, $origy, 200, $origy, $orange);
function calcul ($x, $echx, $echy=1, $equa) {
$x *= $echx;
$equa = preg_replace_callback('/(sin|cos)\((.+)\)/U', "transforme", $equa);
$equa = preg_replace_callback('/(r|p)\((.+),(.+)\)/U', "transforme", $equa);
$equa = preg_replace('/(-?)x/', '${1}'.$x, $equa);
$equa = str_replace('--','', $equa);
if (!preg_match('`[a-zA-Z]`', $equa)) {
@eval("\$equa = $equa;");
} else { return false; }
return -bcmul($equa,$echy,2);
}
for ($i=-$origx;$i<=(-$origx)+200;$i = bcadd($i,'0.05',2)) {
$posy = calcul($i,$echellex,$echelley,$equa)+$origy;
$posx = $i+$origx;
if ($posy < 0 || $posy > 200 || $posx < 0 || $posx > 200) { continue; }
else {
imagesetpixel($im, $posx, $posy, $blanc);
}
}
imagepng($im, "graphique.png");
echo '<img src="graphique.png" alt="'.$equa.'" /><br />';
imagedestroy($im);
}
// callback
function transforme ($motif) {
global $i;
global $echellex;
if ($motif[2] == 'x') { $motif[2] = ($i)*($echellex); }
if ($motif[3] == 'x') { $motif[3] = ($i)*($echellex); }
if ($motif[1] == "r") {
return pow($motif[3],1/$motif[2]);
}
else if ($motif[1] == "p") {
return pow($motif[2],$motif[3]);
}
else if ($motif[1] == "sin") {
return sin(deg2rad($motif[2]));
}
else if ($motif[1] == "cos") {
return cos(deg2rad($motif[2]));
}
}
$fin = microtime(true);
echo'<em> Temps d\'exécution : '.($fin-$départ).'s</em>';
?>