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-28  เมท็อดวาดเส้นโค้งและเกล็ดหิมะของ von Koch
  public static void kochCurve(DWindow w, double x, double y,  
                               double d, double a, int depth) {
    if (depth <= 0) {
      w.drawLine(x, y, x + d * cos(a), y - d * sin(a));
    } else {
      double d3 = d / 3;
      kochCurve(w, x, y, d3, a, depth - 1);  
      x = x + d3 * cos(a);  y = y - d3 * sin(a);
      a = a + 60;
      kochCurve(w, x, y, d3, a, depth - 1);
      x = x + d3 * cos(a);  y = y - d3 * sin(a);
      a = a - 120;
      kochCurve(w, x, y, d3, a, depth - 1);
      x = x + d3 * cos(a);  y = y - d3 * sin(a);
      a = a + 60;
      kochCurve(w, x, y, d3, a, depth - 1);
    }
  }
  public static void kochSnowFlake(DWindow w, double x, double y,  
                                   double d, int depth) {
    double a = 60;
    for (int i = 0; i < 3; i++) {
      kochCurve(w, x, y, d, a, depth);
      x = x + d * cos(a);  y = y - d * sin(a);
      a = a - 120;
    }
  }
}
©2009 S.Prasitjutrakul