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-27  เมท็อดวาดสามเหลี่ยมของ Sierpinski
  public static void spskiTriangle(DWindow w,  
                     double x0, double y0, double d, int depth) {
    if (depth <= 0) {
      drawTriangle(w, x0, y0, d);
    } else {
      double d2 = d / 2;
      double x1 = x0 + d2 * cos(60);
      double y1 = y0 - d2 * sin(60);
      spskiTriangle(w, x0,    y0, d2, depth - 1);  
      spskiTriangle(w, x0+d2, y0, d2, depth - 1);  
      spskiTriangle(w, x1,    y1, d2, depth - 1);
    }
  }
  public static void drawTriangle(DWindow w, double x0, double y0,
                                  double d) {
    double a = 0;
    for (int i = 0; i < 3; i++) {
      double x1 = x0 + d * cos(a), y1 = y0 - d * sin(a);
      w.drawLine(x0, y0, x1, y1);
      a = a + 120;
      x0 = x1; y0 = y1;
    }
  }
©2009 S.Prasitjutrakul