6. แยกย่อย

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.

 
รหัสที่ 6-26  เมท็อดวาดรูปต้นไม้
import jlab.graphics.DWindow;

public class Fractal {
  public static void main(String[] args) {
    DWindow w = new DWindow(200, 200);
    drawTree(w, 100, 200, 80, 90, 6);      // วาดต้นไม้ 
    // spskiTriangle(w, 10, 190, 180, 6);  // วาดสามเหลี่ยม Sierpinski
    // kochSnowFlake(w, 20, 150, 160, 4);  // วาดเกล็ดหิมะ von Koch
  }
  public static void drawTree(DWindow w, double x0, double y0,
                              double d, double a, int depth) {
    double x1 = x0 + d * cos(a), y1 = y0 - d * sin(a);
    w.drawLine(x0, y0, x1, y1);
    if (depth <= 0) return;
    drawTree(w, x1, y1, 0.6 * d, a,      depth - 1);
    drawTree(w, x1, y1, 0.5 * d, a + 35, depth - 1);
    drawTree(w, x1, y1, 0.5 * d, a - 35, depth - 1);
  }
  public static double sin(double a) {
    return Math.sin(Math.toRadians(a));
  }
  public static double cos(double a) {
    return Math.cos(Math.toRadians(a));    
  }
©2009 S.Prasitjutrakul