The Graph Class III

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

Graph.java
/**
 * Draw a graph of the function using the pen in the canvas.
 *
 * @f             function to be graphed.
 */
public void draw(Function f) {
   double xstep = (x1 - x0) / columns;
   double ystep = (y1 - y0) / rows;

   for (int column = 0; column < columns; ++column) {
      double x = x0 + column * xstep;
      double y = f.evaluate(x);
      if (y >= y0 && y < y1) {
         int row = rows - 1 - (int) ((y - y0) / ystep);
         canvas[row][column] = pen;
      }
   }
}

*xstep and ystep are the granularities of our canvas for the x and the y coordinates, respectively.
 
*The for loop goes through all columns of the graph. The ith column represents x0   +   i · xstep.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002