Recursive Sierpinski's Carpet

Printable View

• 04-16-2011, 01:11 PM
baf06
Recursive Sierpinski's Carpet
hello there, I need some help solving this mystery. I was asked to write a recursive method to draw the Sierpinski's Carpet using DrawingPanel. I have done all the work and studied it many times to figure what is wrong. Every thing is working perfectly it is just that i am having a little different drawing.
http://www.java-forums.org/attachmen...1&d=1302952111
the picture to the right is what i am supposed to have, and this is my recursive method but i am sure that if any thing is wrong then it is in the logic maybe.
Code:

```/**     * Recursive method to draw the sqaures of the carpet     * @param level the level of drawing squares     * @param g the Graphics from the panel     * @param x the x-position of the square     * @param y the y-position of the square     * @param width the width of the square     * @param height the height of the square     */     public static void drawFigure(int level, Graphics g,     int x, int y, int width, int height){         if (level == 1) {             g.fillRect(x, y, width, height);         } else {                        int wid = width / 3;             int heig = height / 3;             int x1 = x  - 2 * wid;             int y1 = y - 2 * heig;             int x2 = x + wid;             int y2 = y + heig;             int x3 = x + 4 * wid;             int y3 = y + 4 * heig;             drawFigure(level-1, g, x1, y1, wid, heig);             drawFigure(level-1, g, x2, y1, wid, heig);             drawFigure(level-1, g, x3, y1, wid, heig);             drawFigure(level-1, g, x1, y2, wid, heig);             drawFigure(level-1, g, x1, y3, wid, heig);             drawFigure(level-1, g, x2, y3, wid, heig);             drawFigure(level-1, g, x3, y3, wid, heig);             drawFigure(level-1, g, x3, y2, wid, heig);             g.fillRect(x, y, width, height);         }     }```
hope anyone can help me find my error, thanks
• 04-16-2011, 03:26 PM
baf06
Never mind i solved my problem :) thanks any ways.