Results 1 to 2 of 2
  1. #1
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Smile 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);
            }
        }
    hope anyone can help me find my error, thanks
    Attached Thumbnails Attached Thumbnails Recursive Sierpinski's Carpet-untitled.jpg  
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Never mind i solved my problem :) thanks any ways.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. very frustrating.. recursive
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 01-06-2011, 11:25 PM
  2. recursive function
    By jayden in forum New To Java
    Replies: 11
    Last Post: 09-02-2010, 04:00 PM
  3. Recursive Counting
    By zlwilly in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 09:42 PM
  4. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 05:45 AM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •