Scripts
Formule-Plotter
Scriptje dat een grafiek plot uit een bepaalde formule. Deze formule is in te vullen door showgraph.php te gebruiken. Gelieve geen grote exponenten te gebruiken, een exponent groter dan 4 zorgt al voor een lange berekeningstijd. Een extra pagina genaamd frame.php heb ik erin geinclude die de code laat zien, deze pagina heeft de code Dit kan ook gewoon weggelaten worden. Dit script is door mij gemaakt en is op een aantal andere fora ook geplaats, geen plagiaat dus ;)
formuleplotter
------------------------
----- Graph3.php -----
------------------------
<?php
/*
* +------------------------+
* | Formule-plotter |
* | Clear Webdesign |
* | www.clearwebdesign.nl |
* +------------------------+
*
* Version 0.2A
*
*
* Troubleshooting:
*
* - draw scale by using '$this->drawscale()'
* parameters: 'reds', 'greens', 'blues', 'conf'
* - draw a grid by using '$this->drawgrid()'
* parameters: 'reds', 'greens', 'blues', 'conf'
* - to draw the line according to the function, use '$this->drawline()'
* parameters: 'function', 'reds', 'greens', 'blues', 'conf'
*/
class graph{
function graph(){
error_reporting(E_ALL);
//
// DEFINE GRAPH EXPONENT
//
$CONF = $this->config(3);
$IMG = imageCreate($CONF[ 'width' ], $CONF[ 'height' ]) or die("ERROR::<br>". $CONF['width'] . " . " . $CONF['height']);
$BgColor = ImageColorAllocate($IMG, 255, 255, 255);
ImageFilledRectangle($IMG, 0, 0, $CONF[ 'width' ], $CONF[ 'height' ], $BgColor);
//
// DRAW SCALE
//
if($CONF[ 'DrawSc' ] == 1){
$this->drawscale(0, 0, 0, $CONF, $IMG);
}
//
// DRAW GRID
//
if($CONF[ 'DrawGd' ] == 1){
$this->drawgrid(130,130,130, $CONF, $IMG);
}
//
// DRAW GRAPH
//
$this->drawgraph(255, 140, 130, $CONF, $IMG, "");
header("Content-Type: image/png");
imagePng($IMG);
imageDestroy($IMG);
}
function config($exp){
/*
* -----------------
* Configuration
* Graph Properties
* ------------------
*/
$GraphConf[ 'height' ] = 800;
$GraphConf[ 'width' ] = 800;
$GraphConf[ 'exp' ] = $exp;
$GraphConf[ 'Xminus' ] = -5;
$GraphConf[ 'Xmaxus' ] = 5;
$GraphConf[ 'Yminus' ] = 5;
$GraphConf[ 'Ymaxus' ] = -5;
$GraphConf[ 'DrawSc' ] = 1; // 1: scale, 0: no scale
$GraphConf[ 'DrawGd' ] = 1; // 1: grid, 0: no grid
$GraphConf[ 'Xcoords'] = 1; // 1: x-coords, 0: no x-choords
$GraphConf[ 'Ycoords'] = 1; // 1: y-coords, 0: no y-coords
return $GraphConf;
}
function drawscale($red, $green, $blue, $CONF, $IMG){
$Xscalar = ($CONF[ 'Xminus' ] - $CONF[ 'Xmaxus' ])*-1;
$Yscalar = ($CONF[ 'Yminus' ] - $CONF[ 'Ymaxus' ])*1;
$Xmiddle = round(($CONF[ 'height' ]/2), 0);
$Ymiddle = round(($CONF[ 'width' ]/2), 0);
$XperBar = $CONF[ 'width' ]/$Xscalar;
$YperBar = $CONF[ 'height']/$Yscalar;
$Color = ImageColorAllocate($IMG, $red, $green, $blue);
ImageLine($IMG, $Xmiddle, 0, $Xmiddle, $CONF[ 'height' ], $Color);
ImageLine($IMG, 0, $Ymiddle, $CONF[ 'width' ], $Ymiddle, $Color);
ImageLine($IMG, $Xmiddle+1, 0, $Xmiddle+1, $CONF[ 'height' ], $Color);
ImageLine($IMG, 0, $Ymiddle+1, $CONF[ 'width' ], $Ymiddle+1, $Color);
$j=$CONF['Xminus'];
$i=0;
while($Xscalar>$i){
ImageLine($IMG, $XperBar*$i, $Ymiddle-3, $XperBar*$i, $Ymiddle+4, $Color);
ImageString($IMG, 0, ($XperBar*$i)-10, $Ymiddle+6, $j, $Color);
$i++;
$j++;
}
ImageString($IMG, 3, ($XperBar*$i)-30, $Ymiddle-15, "X-as", $Color);
$j=$CONF['Yminus'];
$i=0;
while($Yscalar>$i){
if($j==0){
$j="";
}
ImageLine($IMG, $Xmiddle-3, $YperBar*$i, $Xmiddle+3, $YperBar*$i, $Color);
imagestring($IMG, 1, $Xmiddle-12, $YperBar*$i+5, $j, $Color);
if($j==""){
$j=0;
}
$i++;
$j--;
}
ImageString($IMG, 3, $Xmiddle+5, $YperBar*$i-15, "Y-as", $Color);
}
function drawgrid($red, $green, $blue, $CONF, $IMG){
$Xscalar = ($CONF[ 'Xminus' ] - $CONF[ 'Xmaxus' ])*-1;
$Yscalar = ($CONF[ 'Yminus' ] - $CONF[ 'Ymaxus' ])*1;
$XperBar = $CONF[ 'width' ]/$Xscalar;
$YperBar = $CONF[ 'height']/$Yscalar;
$Color = ImageColorAllocate($IMG, $red, $green, $blue);
$i=0;
while($Xscalar>$i){
ImageLine($IMG, $XperBar*$i, 0, $XperBar*$i, $CONF['height'], $Color);
$i++;
}
$i=0;
while($Yscalar>$i){
ImageLine($IMG, 0, $YperBar*$i, $CONF['width'], $YperBar*$i, $Color);
$i++;
}
}
function drawgraph($red, $green, $blue, $CONF, $IMG, $function){
$Xscalar = ($CONF[ 'Xminus' ] - $CONF[ 'Xmaxus' ])*-1;
$Yscalar = ($CONF[ 'Yminus' ] - $CONF[ 'Ymaxus' ])*1;
$XperBar = round($CONF[ 'width' ]/$Xscalar,0);
$YperBar = round($CONF[ 'height']/$Yscalar,0);
$Color = ImageColorAllocate($IMG, $red, $green, $blue);
$Xmiddle = round(($CONF[ 'height' ]/2), 0);
$Ymiddle = round(($CONF[ 'width' ]/2), 0);
$Xvalue = 0;
$x = $CONF[ 'Xminus' ];
$i = 0;
$d = 0;
$CModes = array( 'cos' => acos($Xvalue),
'tan' => atan($Xvalue),
'sin' => asin($Xvalue),
'sqrt' => pow($Xvalue, 2),
'pow' => sqrt($Xvalue),
'pi' => pi($Xvalue)/1
);
while($x<$CONF[ 'Xmaxus' ] ){
$GModes = array( 'cos' => $_GET['gr'] * pow(cos($x), $_GET['exp']) + $_GET['plus'],
'tan' => $_GET['gr'] * pow(tan($x), $_GET['exp']) + $_GET['plus'],
'sin' => $_GET['gr'] * pow(sin($x), $_GET['exp']) + $_GET['plus'],
'sqrt' => $_GET['gr'] * pow(sqrt($x), $_GET['exp']) + $_GET['plus'],
'pow' => $_GET['gr'] * pow($x, $_GET['exp']) + $_GET['plus'],
'pi' => $_GET['gr'] * pow(pi($x), $_GET['exp']) + $_GET['plus']
);
$value = $GModes[ $_GET['mode'] ];
$Xpix = $XperBar*$i;
$Ypix = (($YperBar*($CONF['Ymaxus']+($value*-1))) + $CONF['height']);
ImageFilledRectangle($IMG, $Xpix-1, $Ypix-1, $Xpix, $Ypix, $Color);
if(round($x,1) == 1.0 && $d != 1 && $d != 2 && $d != 3 && $d != 4){
$Tformule = "f(x): " . $_GET['gr'] . $_GET['mode'] . "(x)^" . $_GET['exp'];
$TColor = ImageColorAllocate($IMG, 255, 80, 0);
ImageString($IMG, 2, $Xpix+8, $Ypix+10, $Tformule, $TColor) or die("ERROR");
$d=1;
}
if($CONF[ 'Xcoords' ] == 1){
if(round($x,2) == 0.00 && $d != 2){
$XCoordColor = ImageColorAllocate($IMG, 0, 150, 150);
ImageString($IMG, 2, $Xmiddle-75, $Ypix-20, "A (0; " . round($value,2) . ")", $XCoordColor);
$d=2;
}
}
//
// Calculate x-values
//
$Xvalue=0;
if($CONF[ 'Ycoords' ] == 1){
//
// Calculate 'c'-vars
//
if(isset($_GET['plus']) && is_numeric($_GET['plus'])){
if($_GET['plus'] > 0){
$Xvalue -= $_GET['plus'];
}
else{
$Xvalue -= $_GET['plus'];
}
}
//
// Calculate 'a'-vars
//
if(isset($_GET['gr']) && is_numeric($_GET['gr'])){
if($_GET['gr'] > 1){
$Xvalue /= $_GET['gr'];
}
elseif($_GET['gr'] < -1){
$Xvalue /= $_GET['gr'];
}
elseif(($_GET['gr'] > -1) && ($_GET['gr'] < 1)){
$Xvalue *= (1/$_GET['gr']);
}
}
//
// Calculate 'b'-vars
//
if(isset($_GET['exp'])){
if(fmod($_GET['exp'], 2) == 0){
if($_GET['exp'] == 2){
$dump = $Xvalue;
$Xpvalue[0] = sqrt($dump);
$Xpvalue[1] = sqrt($dump)*-1;
}else{
$dump = $Xvalue;
$Xpvalue[0] = pow($dump, (1/round($_GET['exp'],5)));
$Xpvalue[1] = pow($dump, (1/round($_GET['exp'],5)))*-1;
}
}else{
$dump = $Xvalue;
$Xpvalue[0] = pow($dump, (1/round($_GET['exp'],5)));
$Xpvalue[1] = "";
}
}
if($_GET['mode'] == "cos"){
unset($dump);
$dump[0] = $Xpvalue[0];
$dump[1] = $Xpvalue[1];
unset($Xvalue);
$Xvalue[0] = acos($dump[0]);
$Xvalue[1] = acos($dump[1]);
if($d == 2){
if(round($x,2) == round($Xvalue[0], 2)){
$BcoColor = ImageColorAllocate($IMG, 0, 80, 255);
ImageString($IMG, 2, $Xpix, $Ypix-35, "B1(" . round($Xvalue[0],2) . "; 0)", $BcoColor);
}
if(fmod($_GET['exp'], 2) == 0){
if(round($x,2) == round($Xvalue[1], 2)){
$BcoColor = ImageColorAllocate($IMG, 0, 80, 255);
ImageString($IMG, 2, $Xpix, $Ypix+35, "B2(" . round($Xvalue[1],2). "; 0)", $BcoColor);
$d=3;
}
}
}
}
elseif($_GET['mode'] == "pow"){
unset($dump);
$dump[0] = $Xpvalue[0];
$dump[1] = $Xpvalue[1];
unset($Xvalue);
$Xvalue[0] = $dump[0];
$Xvalue[1] = $dump[1];
if($d != 4){
if(round($x, 2) == round($Xvalue[0], 2)){
$BcoColor = ImageColorAllocate($IMG, 0, 80, 255);
ImageString($IMG, 2, $Xpix+15, $Ypix-15, "B1(" . round($Xvalue[0],2) . "; 0)", $BcoColor);
$d+=0.5;
}
if(fmod($_GET['exp'], 2) == 0){
if(round($x,2) == round($Xvalue[1], 2)){
$BcoColor = ImageColorAllocate($IMG, 0, 80, 255);
ImageString($IMG, 2, $Xpix-50, $Ypix+15, "B2(" . round($Xvalue[1],2). "; 0)", $BcoColor);
$d+=0.5;
}
}
}
}
}
$x+=0.01;
$i+=0.01;
}
}
}
$do = new graph;
?>
------------------------
-----Showgraph.php------
------------------------
<html>
<head>
<title>GraphMaker</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if(!isset($_GET['gr'])){
?>
<form method="GET" action="<?= $_SERVER['PHP_SELF']; ?>">
<strong>f(x): </strong>
<input type="text" name="gr">
<select name="mode" id="mode">
<option value="cos">Cos(x)</option>
<option value="tan">Tan(x)</option>
<option value="sin">Sin(x)</option>
<option value="sqrt">Squareroot(x)</option>
<option value="pow">x</option>
<option value="pi">Pi(x)</option>
</select>
^
<input type="text" name="exp">
+
<input name="plus" type="text" id="plus">
<br>
:
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}else{
?>
<table>
<tr>
<td><img src="graph3.php?gr=<?= $_GET['gr']; ?>&exp=<?= $_GET['exp']; ?>&plus=<?= $_GET['plus']; ?>&mode=<?= $_GET['mode']; ?>"></td>
<td> <strong>A:</strong> Snijpunt Y-As<br>
<strong>B:</strong> Snijpunt(en) X-As<br>
<strong>C:</strong> Top<strong><br>
D:</strong> Dal<br></td>
</tr>
</table>
<?php
}
?>
<br>
<br>
Graph Code:
</blockquote>
<iframe src="frame.php" height="400" width="130%"></iframe>
</body>
</html>
Reacties
0