eqGraph

Equation Graph.

Defined (1)

The class is defined in the following location(s).

/deprecated/includes/eos.class.php  
  1. class eqGraph extends eqEOS { 
  2. private $width; 
  3. private $height; 
  4. //GD Image reference 
  5. private $image; 
  6.  
  7. /** 
  8. * Constructor 
  9. * Sets up the Graph class with an image width and height defaults to 
  10. * 640x480 
  11. * @param Integer $width Image width 
  12. * @param Integer $height Image height 
  13. */ 
  14. public function __construct($width=640, $height=480) { 
  15. // default width and height equal to that of a poor monitor (in early 2000s) 
  16. $this->width = $width; 
  17. $this->height = $height; 
  18. //initialize main class variables 
  19. parent::__construct(); 
  20. } //end function eqGraph 
  21.  
  22.  
  23. /** 
  24. * Create GD Graph Image 
  25. * Creates a GD image based on the equation given with the parameters that are set 
  26. * @param String $eq Equation to use. Needs variable in equation to create graph, all variables are interpreted as 'x' 
  27. * @param Integer $xLow Lower x-bound for graph 
  28. * @param Integer $xHigh Upper x-bound for graph 
  29. * @param Float $xStep Stepping points while solving, the lower, the better precision. Slow if lower than .01 
  30. * @param Boolean $xyGrid Draw gridlines? 
  31. * @param Boolean $yGuess Guess the upper/lower yBounds? 
  32. * @param Integer $yLow Lower y-bound 
  33. * @param Integer $yHigh Upper y-bound 
  34. * @return Null 
  35. */ 
  36. public function graph($eq, $xLow, $xHigh, $xStep, $xyGrid = false, $yGuess = true, $yLow=false, $yHigh=false) { 
  37. //create our image and allocate the two colors 
  38. $img = ImageCreate($this->width, $this->height); 
  39. $white = ImageColorAllocate($img, 255, 255, 255); 
  40. $black = ImageColorAllocate($img, 0, 0, 0); 
  41. $grey = ImageColorAllocate($img, 220, 220, 220); 
  42. $xStep = abs($xStep); 
  43. //DEVELOPER, UNCOMMENT NEXT LINE IF WANTING TO PREVENT SLOW GRAPHS 
  44. //$xStep = ($xStep > .01) ? $xStep : 0.01; 
  45. if($xLow > $xHigh) 
  46. list($xLow, $xHigh) = array($xHigh, $xLow); //swap function 
  47.  
  48. $xScale = $this->width/($xHigh-$xLow); 
  49. $counter = 0; 
  50. if(DEBUG) { 
  51. $hand=fopen("eqgraph.txt", "w"); 
  52. fwrite($hand, "$eq\n"); 
  53. for($i=$xLow;$i<=$xHigh;$i+=$xStep) { 
  54. $tester = sprintf("%10.3f", $i); 
  55. if($tester == "-0.000") $i = 0; 
  56. $y = $this->solveIF($eq, $i); 
  57. //eval('$y='. str_replace('&x', $i, $eq).";"); /* used to debug my eqEOS class results */ 
  58. if(DEBUG) { 
  59. $tmp1 = sprintf("y(%5.3f) = %10.3f\n", $i, $y); 
  60. fwrite($hand, $tmp1); 
  61.  
  62. // If developer asked us to find the upper and lower bounds for y...  
  63. if($yGuess==true) { 
  64. $yLow = ($yLow===false || ($y<$yLow)) ? $y : $yLow; 
  65. $yHigh = ($yHigh===false || $y>$yHigh) ? $y : $yHigh; 
  66. $xVars[$counter] = $y; 
  67. $counter++;  
  68. if(DEBUG) 
  69. fclose($hand); 
  70. // add 0.01 to each side so that if y is from 1 to 5, the lines at 1 and 5 are seen  
  71. $yLow-=0.01;$yHigh+=0.01; 
  72.  
  73. //Now that we have all the variables stored...find the yScale 
  74. $yScale = $this->height/(($yHigh)-($yLow)); 
  75.  
  76. // if developer wanted a grid on the graph, add it now  
  77. if($xyGrid==true) { 
  78. for($i=ceil($yLow);$i<=floor($yHigh);$i++) { 
  79. $i0 = abs($yHigh-$i); 
  80. ImageLine($img, 0, $i0*$yScale, $this->width, $i0*$yScale, $grey); 
  81. for($i=ceil($xLow);$i<=floor($xHigh);$i++) { 
  82. $i0 = abs($xLow-$i); 
  83. ImageLine($img, $i0*$xScale, 0, $i0*$xScale, $this->height, $grey); 
  84.  
  85. //Now that we have the scales, let's see if we can draw an x/y-axis 
  86. if($xLow <= 0 && $xHigh >= 0) { 
  87. //the y-axis is within our range - draw it. 
  88. $x0 = abs($xLow)*$xScale; 
  89. ImageLine($img, $x0, 0, $x0, $this->height, $black); 
  90. for($i=ceil($yLow);$i<=floor($yHigh);$i++) { 
  91. $i0 = abs($yHigh-$i); 
  92. ImageLine($img, $x0-3, $i0*$yScale, $x0+3, $i0*$yScale, $black); 
  93. if($yLow <= 0 && $yHigh >= 0) { 
  94. //the x-axis is within our range - draw it. 
  95. $y0 = abs($yHigh)*$yScale; 
  96. ImageLine($img, 0, $y0, $this->width, $y0, $black); 
  97. for($i=ceil($xLow);$i<=floor($xHigh);$i++) { 
  98. $i0 = abs($xLow-$i); 
  99. ImageLine($img, $i0*$xScale, $y0-3, $i0*$xScale, $y0+3, $black); 
  100. $counter=2; 
  101.  
  102. //now graph it all ;] 
  103. for($i=$xLow+$xStep;$i<=$xHigh;$i+=$xStep) { 
  104. $x1 = (abs($xLow - ($i - $xStep)))*$xScale; 
  105. $y1 = (($xVars[$counter-1]<$yLow) || ($xVars[$counter-1] > $yHigh)) ? -1 : (abs($yHigh - $xVars[$counter-1]))*$yScale; 
  106. $x2 = (abs($xLow - $i))*$xScale; 
  107. $y2 = (($xVars[$counter]<$yLow) || ($xVars[$counter] > $yHigh)) ? -1 : (abs($yHigh - $xVars[$counter]))*$yScale; 
  108.  
  109. // if any of the y values were found to be off of the y-bounds, don't graph those connecting lines  
  110. if($y1!=-1 && $y2!=-1) 
  111. ImageLine($img, $x1, $y1, $x2, $y2, $black); 
  112. $counter++; 
  113. $this->image = $img; 
  114. } //end function 'graph' 
  115.  
  116. /** 
  117. * Sends JPG to browser 
  118. * Sends a JPG image with proper header to output 
  119. */ 
  120. public function outJPG() { 
  121. header("Content-type: image/jpeg"); 
  122. ImageJpeg($this->image); 
  123.  
  124. /** 
  125. * Sends PNG to browser 
  126. * Sends a PNG image with proper header to output 
  127. */ 
  128. function outPNG() { 
  129. header("Content-type: image/png"); 
  130. ImagePng($this->image); 
  131.  
  132. /** 
  133. * Output GD Image 
  134. * Will give the developer the GD resource for the graph that 
  135. * can be used to store the graph to the FS or other media 
  136. * @return Resource GD Image Resource 
  137. */ 
  138. public function getImage() { 
  139. return $this->image; 
  140.  
  141. /** 
  142. * Output GD Image 
  143. * Alias for eqGraph::getImage() 
  144. * @return Resource GD Image resource 
  145. */ 
  146. public function outGD() { 
  147. return $this->getImage(); 
  148. } //end class 'eqGraph'