Results 1 to 2 of 2
Thread: Recursive Sierpinski's Carpet
- 04-16-2011, 01:11 PM #1
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
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.
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.
Java 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); } }
Click on REP and add to member reputation, if you find their advices/solutions effective.
- 04-16-2011, 03:26 PM #2
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
very frustrating.. recursive
By Yakg in forum New To JavaReplies: 5Last Post: 01-06-2011, 10:25 PM -
recursive function
By jayden in forum New To JavaReplies: 11Last Post: 09-02-2010, 03:00 PM -
Recursive Counting
By zlwilly in forum New To JavaReplies: 1Last Post: 01-29-2009, 08:42 PM -
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11-29-2007, 04:45 AM -
Help with recursive implementation
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:57 AM
Bookmarks