Sierpinski\'s Triangle is a simple and famous example of a fractal image. It is
ID: 3528491 • Letter: S
Question
Sierpinski's Triangle is a simple and famous example of a fractal image. It is built recursively from a simple set of rules, illustrated in Figure 2. Your task will be to create an application that illustrates a perfect Sierpinski triangle, regardless of how large the application frame is. As the user moves and resizes the application window, the triangle should be redrawn and made larger or smaller as appropriate. Your program should display a frame that is based on the size of the user's screen. The paintComponent method of the panel on which you are drawing will be called whenever the frame is resized, so that happens automagically and you don't have to worry about it. JPanels include a getHeight() and getWidth() method that you will be able to use to get the information you need for passing to a recursive draw function that you will write. The draw algorithm takes the coordinates of a square area of the screen as input. If that square is the size of a single pixel, it should call drawRect() on the Graphics object passed into paintComponent, drawing a one-pixel square somewhere on the screen. If larger, it should call the draw method three times recursively, once on the lower left quadrant of the square, once on the lower right, and once on an area centered above the other two (as illustrated in Figure 2). The solution will look a lot like Figure 3, with the largest triangle tting into the largest square area of the frame, and the smallest triangles being three pixels in size.Explanation / Answer
// Draws the Sierpinski's Triangle fractal image.
import java.awt.*;
import java.util.*;
public class Sierpinski {
public static final int SIZE = 256;
public static void main(String[] args) {
// prompt for level
Scanner console = new Scanner(System.in);
System.out.print("What level do you want? ");
int level = console.nextInt();
// initialize drawing panel
DrawingPanel p = new DrawingPanel(SIZE, SIZE);
p.setBackground(Color.CYAN);
Graphics g = p.getGraphics();
// compute triangle endpoints and begin recursion
int triangleHeight = (int) Math.round(SIZE * Math.sqrt(3.0) / 2.0);
Point p1 = new Point(0, triangleHeight);
Point p2 = new Point(SIZE / 2, 0);
Point p3 = new Point(SIZE, triangleHeight);
drawFigure(level, g, p1, p2, p3);
}
// Draws a Sierpinski fractal to the given level inside the triangle
// whose vertices are (p1, p2, p3).
public static void drawFigure(int level, Graphics g,
Point p1, Point p2, Point p3) {
if (level == 1) {
// base case: simple triangle
Polygon p = new Polygon();
p.addPoint(p1.x, p1.y);
p.addPoint(p2.x, p2.y);
p.addPoint(p3.x, p3.y);
g.fillPolygon(p);
} else {
// recursive case, split into 3 triangles
Point p4 = midpoint(p1, p2);
Point p5 = midpoint(p2, p3);
Point p6 = midpoint(p1, p3);
// recurse on 3 triangular areas
drawFigure(level - 1, g, p1, p4, p6);
drawFigure(level - 1, g, p4, p2, p5);
drawFigure(level - 1, g, p6, p5, p3);
}
}
// returns the midpoint of p1 and p2
public static Point midpoint(Point p1, Point p2) {
return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.