Created
December 5, 2012 12:31
-
-
Save felipap/4215161 to your computer and use it in GitHub Desktop.
php renato
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$HEIGHT = 600; | |
$WIDTH = 1000; | |
$xfactor = 2; 5; | |
$yfactor = 1; 0.001; 0.5; | |
$CENTER_X = $WIDTH/2; | |
$CENTER_Y = 400; | |
$img = imagecreate($WIDTH, $HEIGHT); | |
imagecolorallocate($img, 255, 255, 255); | |
$COLOR = imagecolorallocate($img, 22, 22, 22); | |
function tY ($y) { | |
global $CENTER_Y; | |
return $CENTER_Y-$y; | |
} | |
function tX ($x) { | |
global $CENTER_X; | |
return $CENTER_X+$x; | |
} | |
function fY ($y) { // transforms vertical | |
global $yfactor; | |
return tY($y*$yfactor); | |
} | |
function fX ($x) { | |
global $xfactor; | |
return tX($x*$xfactor); | |
} | |
function drawPoint ($x, $y, $thick=4) { | |
global $img; | |
imagefilledellipse($img, fX($x), fY($y), $thick, $thick, 1); | |
} | |
// | |
function makeDot ($x, $y) { | |
global $img; | |
imageline($img, fX($x), fY($y), fX($x), fY($y), 1); | |
} | |
function drawLine ($x1, $y1, $x2, $y2) { | |
global $img, $COLOR; | |
imageline($img, fX($x1), fY($y1), fX($x2), fY($y2), $COLOR); | |
} | |
function drawDashedLine_f ($x, $x1, $y1, $x2, $y2, $dsize) { | |
return (($y2-$y1)/($x2-$x1)*$x)+$x1; | |
} | |
function drawDashedLine ($x1, $y1, $x2, $y2, $dsize=2) { | |
if ($y2 < $y1) | |
list($y1,$y2) = array($y2,$y1); | |
if ($x2 < $x1) | |
list($x1,$x2) = array($x2,$x1); | |
if ($x1==$x2) { | |
for ($i=0; $i<($y2-$y1)/$dsize; $i+=2) { | |
drawLine($x1, $y1+$i*$dsize, $x1, $y1+($i+1)*$dsize); | |
} | |
return; | |
} | |
if ($y1==$y2) { | |
global $xfactor; | |
for ($i=0; $i<($x2-$x1)/$dsize; $i+=2) { | |
// echo $i, " ", $x2-$x1; | |
drawLine($x1+$i*$dsize, $y1, $x1+($i+1)*$dsize, $y1); | |
} | |
return; | |
} | |
return; | |
for ($i=0; $i<($y2-$y1)/$dsize; $i+=2) { | |
// echo "<br>$i"; | |
$x = $i*$dsize; | |
$y = drawDashedLine_f($x, $x1, $y1, $x2, $y2, $dsize); | |
$xfinal = ($i+1)*$dsize; | |
$yfinal = drawDashedLine_f($xfinal, $x1, $y1, $x2, $y2, $dsize); | |
// echo "$x, $y: $xfinal, $yfinal<p>"; | |
drawLine($x+$x1, $y, $xfinal+$x1, $yfinal); | |
} | |
} | |
function drawGrid ($dsize=20) { | |
global $img, | |
$CENTER_X, $CENTER_Y, | |
$WIDTH, $HEIGHT, $COLOR, | |
$xfactor, $yfactor; | |
// $dsize /= $xfactor; | |
$COLOR = imagecolorallocate($img, 200, 200, 200); | |
// vertical | |
for ($i=0; $i<$WIDTH/$dsize; $i++) { | |
drawLine($i*$dsize, | |
$CENTER_Y/$yfactor, | |
$i*$dsize, | |
(-$HEIGHT+$CENTER_Y)/$yfactor); | |
} | |
for ($i=0; $i<$WIDTH/$dsize; $i++) { | |
drawLine(-$i*$dsize, | |
$CENTER_Y/$yfactor, | |
-$i*$dsize, | |
(-$HEIGHT+$CENTER_Y)/$yfactor); | |
} | |
// horizontal | |
for ($i=0; $i<$HEIGHT/$dsize/$yfactor; $i++) { | |
drawLine(-$CENTER_X*$xfactor, | |
-$i*$dsize, | |
($WIDTH-$CENTER_X)*$xfactor, | |
-$i*$dsize); | |
} | |
for ($i=0; $i<$HEIGHT/$dsize/$yfactor; $i++) { | |
drawLine(-$CENTER_X*$xfactor, | |
$i*$dsize, | |
($WIDTH-$CENTER_X)*$xfactor, | |
$i*$dsize); | |
} | |
$COLOR = imagecolorallocate($img, 22, 22, 22); | |
} | |
function drawAxes () { | |
global $CENTER_X, $CENTER_Y, $WIDTH, $HEIGHT, $img, $xfactor, $yfactor; | |
$COLOR = imagecolorallocate($img, 200, 200, 200); | |
drawLine(-$CENTER_X/$xfactor, 0, ($WIDTH-$CENTER_X)/$xfactor, 0); // horizontal | |
drawLine(0, $CENTER_Y/$yfactor, 0, (-$HEIGHT+$CENTER_Y)/$yfactor); // vertical | |
$COLOR = imagecolorallocate($img, 22, 22, 22); | |
} | |
$A = .1; | |
$B = 23; | |
$C = 0; | |
function workEquation () { | |
global $CENTER_X, $WIDTH; | |
// $A, $B, $C; | |
function workEquation_f($x) { | |
global $A, $B, $C; | |
return $A*pow($x, 2)+$B*$x+$C; | |
} | |
function workEquation_fy($y) { | |
global $A, $B, $C; | |
return (-$B+sqrt(pow($B,2)-4*$A*$C))/2/$A; | |
} | |
function workEquation_ffy($y) { | |
global $A, $B, $C; | |
return (-$B-sqrt(pow($B,2)-4*$A*$C))/2/$A; | |
} | |
function maxY () { | |
global $A, $B, $C; | |
return -(pow($B, 2)-4*$A*$C)/4/$A; | |
} | |
function maxX () { | |
global $A, $B, $C; | |
return -$B/2/$A; | |
} | |
drawPoint(workEquation_fy($y), 0, 7); | |
drawPoint(workEquation_ffy($y), 0, 7); | |
global $xfactor, $yfactor; | |
$istart = -$CENTER_X/$xfactor; | |
$iend = ($WIDTH-$CENTER_X)/$xfactor; | |
$x = $istart; | |
$y = workEquation_f($x); | |
drawPoint(maxX(), 0); | |
drawPoint(maxX(), maxY()); | |
drawDashedLine(maxX(), 0, maxX(), maxY()); | |
drawDashedLine(0, maxY(), maxX(), maxY()); | |
for ($i=$istart; $i<$iend; $i++) { | |
$lx = $x; | |
$ly = $y; | |
$x = $i; | |
$y = workEquation_f($x); | |
drawLine($lx, $ly, $x, $y); | |
} | |
} | |
drawGrid(); | |
drawAxes(); | |
workEquation(); | |
// drawDashedLine(10, 10, 10, 200); | |
header('Content-Type: image/png'); | |
imagepng($img); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment