2017-04-17 114 views
0

這是我一直在做我試圖得到對方的三角形的代碼,但我無法得到它..遞歸謝爾賓斯基三角形

import java.awt.*; 
 
import javax.swing.*; 
 

 
public class Sierpinski_Triangle extends JPanel { 
 
    private static int numberLevelsOfRecursion; 
 

 
    public Sierpinski_Triangle(int numLevels) { 
 
     numberLevelsOfRecursion = numLevels; 
 
    } 
 

 
    public void paintComponent(Graphics computerScreen) { 
 
     super.paintComponent(computerScreen); 
 
     Point top = new Point(250, 50); 
 
     Point left = new Point(50, 450); 
 
     Point right = new Point(450, 450); 
 
     drawTriangle(computerScreen, numberLevelsOfRecursion, top, left, right); 
 
    } 
 

 
    /** 
 
    * Draw a Sierpinski triangle 
 
    * 
 
    * @param screen 
 
    *   the surface on which to draw the Sierpinski image 
 
    * @param levels 
 
    *   number of levels of triangles-within-triangles 
 
    * @param top 
 
    *   coordinates of the top point of the triangle 
 
    * @param left 
 
    *   coordinates of the lower-left point of the triangle 
 
    * @param right 
 
    *   coordinates of the lower-right point of the triangle 
 
    */ 
 
    public static void drawTriangle(Graphics g, int levels, Point top, Point left, Point right) { 
 
     /** 
 
     * You must COMPLETER THE CODE HERE to draw the Sierpinski Triangle 
 
     * recursive code needed to draw the Sierpinski Triangle 
 
     */ 
 
     Point p1 = top; 
 
     Point p2 = left; 
 
     Point p3 = right; 
 
     if (levels == 2) { 
 
      // base case: simple triangle 
 
      Polygon tri = new Polygon(); 
 
      tri.addPoint(250, 50); 
 
      tri.addPoint(50, 450); 
 
      tri.addPoint(450, 450); 
 
      g.setColor(Color.RED); 
 
      g.fillPolygon(tri); 
 
     } else { 
 
      // Get the midpoint on each edge in the triangle 
 
      Point p12 = midpoint(p1, p2); 
 
      Point p23 = midpoint(p2, p3); 
 
      Point p31 = midpoint(p3, p1); 
 
      // recurse on 3 triangular areas 
 
      drawTriangle(g, levels - 1, p1, p12, p31); 
 
      drawTriangle(g, levels - 1, p12, p2, p23); 
 
      drawTriangle(g, levels - 1, p31, p23, p3); 
 
     } 
 
    } 
 

 
    private static Point midpoint(Point p1, Point p2) { 
 
     return new Point((p1.x + p2.x)/2, (p1.y + p2.y)/2); 
 
    } 
 

 
    public static void main(String[] args) { 
 
     JFrame frame = new JFrame("SierpinskiTriangle"); 
 
     Sierpinski_Triangle applet = new Sierpinski_Triangle(1); 
 
     frame.add(applet); 
 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
     frame.setSize(450, 450); 
 
     frame.setVisible(true); 
 
    } 
 
}

我真的很感激任何提示,使這個

enter image description here

PS我是韓國的,所以如果你寫我會很感激德感謝修復的尾巴。

回答

2

首先要解決的問題是drawTriangle必須在某處有return聲明。停止標準。
否則它永遠不會停止。 也有一些其他的變化,看評論:

public class Sierpinski_Triangle extends JPanel { 
    private static int numberLevelsOfRecursion; 

    //will take long time on numLevels > 12 
    public Sierpinski_Triangle(int numLevels) { 
     numberLevelsOfRecursion = numLevels; 
    } 

    @Override 
    public void paintComponent(Graphics computerScreen) { 

     super.paintComponent(computerScreen); 
     Point top = new Point(250, 50); 
     Point left = new Point(50, 450); 
     Point right = new Point(450, 450); 
     drawTriangle(computerScreen, numberLevelsOfRecursion, top, left, right); 
    } 

    /** 
    * Draw a Sierpinski triangle 
    * 
    * @param g 
    *   the surface on which to draw the Sierpinski image 
    * @param levels 
    *   number of levels of triangles-within-triangles 
    * @param top 
    *   coordinates of the top point of the triangle 
    * @param left 
    *   coordinates of the lower-left point of the triangle 
    * @param right 
    *   coordinates of the lower-right point of the triangle 
    */ 
    public static void drawTriangle(Graphics g, int levels, Point top, Point left, Point right) { 
     /** 
     * You must COMPLETER THE CODE HERE to draw the Sierpinski Triangle 
     * recursive code needed to draw the Sierpinski Triangle 
     */ 
     if(levels < 0) {//add stop criteria 
      return; 
     } 

     g.setColor(Color.RED); 

     Polygon tri = new Polygon(); 
     tri.addPoint(top.x, top.y); //use top,left right rather than fixed points 
     tri.addPoint(left.x, left.y); 
     tri.addPoint(right.x, right.y); 

     //using g.fillPolygon(tri); you'll be painting on red polygon 
     //on top of another red polygon 
     g.drawPolygon(tri); 

     // Get the midpoint on each edge in the triangle 
     Point p12 = midpoint(top, left); 
     Point p23 = midpoint(left, right); 
     Point p31 = midpoint(right, top); 

     // recurse on 3 triangular areas 
     drawTriangle(bi, levels - 1, top, p12, p31); 
     drawTriangle(bi, levels - 1, p12, left, p23); 
     drawTriangle(bi, levels - 1, p31, p23, right); 
    } 

    private static Point midpoint(Point p1, Point p2) { 
     return new Point((p1.x + p2.x)/2, (p1.y + p2.y)/2); 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("SierpinskiTriangle"); 
     Sierpinski_Triangle applet = new Sierpinski_Triangle(5); 
     frame.add(applet); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(550, 550); 
     frame.setVisible(true); 
    } 
} 

enter image description here