Exemplo n.º 1
0
  void drawFunction(Graphics g) {

    // Just plot 301 points with lines between them.
    double x, y;
    double prevx, prevy;
    double dx;
    int totalX = (int) (Xmax + ((-1) * Xmin));

    dx = totalX / 300.0;

    g.setColor(Color.red);

    x = Xmin;
    y = funct.equation(x);

    /* Compute each of the other 300 points, and draw a line segment
    between each consecutive pair of points.  Note that if the function
    is undefined at one of the points in a pair, then the line
    segment is not drawn.  */

    for (int i = 1; i <= 300; i++) {

      prevx = x; // Save the coords of the previous point.
      prevy = y;

      x += dx; // Get the coords of the next point.
      y = funct.equation(x);

      if ((!Double.isNaN(y)) && (!Double.isNaN(prevy))) {
        // Draw a line segment between the two points.
        putLine(g, prevx, prevy, x, y);
      }
    }
  }