8. วัตถุสิ่งของ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 8-44  โปรแกรมแสดงรูปแฟรกทัลที่ได้จากการหารากของนิวตัน
import jlab.graphics.DWindow;
public class NewtonFractal {
  public static void main(String[] args) {
    DWindow w = new DWindow(400, 400);
    double width = w.getWidth(), height = w.getHeight();
    int[][] p = w.getPixmap();
    int maxLoops = 30;
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        Complex z0 = new Complex(-1.5+3*x/width, 1.5-3*y/height);
        int k = numLoops(z0, maxLoops);
        int g = k * 255 / maxLoops;
        p[x][y] = DWindow.mixRGB(g, g, g);
      }
    }
    w.setPixmap(p);
  }
  public static int numLoops(Complex z1, int maxLoops) {
    Complex z0;
    int k = 0;
    do {
      z0 = z1;
      z1 = z0.substract(f(z0).divide(df(z0)));
    } while (++k < maxLoops && z1.substract(z0).abs() > 1e-10);
    return k;
  }
  private static Complex f(Complex z) {
    return z.multiply(z).multiply(z).substract(1);
  }
  private static Complex df(Complex z) {
    return z.multiply(z).multiply(3);
  }
}   
©2009 S.Prasitjutrakul