Recursive graphics: The Sierpinski Triangle
The Sierpinski triangle is a very nice example of a recursive pattern (fractal). It was described by the mathematician Sierpinski in 1915. Although it looks complex, it can be generated with a very short recursive method.
First, let's try to understand the recursion.
Do you see the pattern? Imagine that you have a method called
//draws a black-filled triangle with vertices (x1, y1), (x2,y2), (x3,y3) drawFilledTriangle(int x1, int y1, int x2, int y2, int x3, int y3)When drawFilledTriangle(a1, a2, b1, b2, c1, c2) is called, it draws for you on the screen a black-filled triangle with the specified vertices (a1,a2), (b1,b2), (c1,c2). Using it, try to sketch down the code for a method that draws the Sierpinski triangle with vertices (x1, y1), (x2,y2), (x3,y3):
//draws a Sierpinski triangle with vertices (x1, y1), (x2, y2), (x3, y3) render(int x1, int y1, int x2, int y2, int x3, int y3, int depth) { ... }