Results 1 to 2 of 2
  1. #1
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Question Help with recursive squares

    Hello, I have to write a program that produces the recursive pattern shown below.

    I tried different recursions but I cannot get it done. This is what I've got so far:

    Java Code:
    static void drawSquare(int n, double x, double y, double size){
    		if(n <= 0){  // n is number of squares
                           
                            // I use predefined class StdDraw
    			StdDraw.setPenColor(StdDraw.GRAY);
    			StdDraw.filledSquare(x, y, size);
    			StdDraw.setPenColor(StdDraw.BLACK);
    			StdDraw.square(x, y, size);
    		}
    		else{
    			
                            // x0, y0, x1, y1 - coordinates of the squares, produced after each recursive step
                            x0 = x/2;
    			y0 = y/2;
    			x1 = x + x/2;
    			y1 = y + y/2;
    			size = size/2.2;
    			
    		}
    	}
    I haven't included the recursive calls in the else statement because they don't make sense.

    The pseudocode would be something like:

    Java Code:
    draw the first big square;
    change x, y, size;
    draw first smaller square;
    change x, y, size;
    draw second smaller square;
    change x, y, size;
    draw third smaller square;
    change x, y, size;
    draw fourth smaller square;
    .
    .
    .
    so on
    but I am not able to put this pseudodoce into real recursion and actually I am not sure if this would be the proper sequence of steps to draw the squares.

    Any ideas or suggestions are greatly appreciated.

    Thank you in advance.
    Luke
    Attached Thumbnails Attached Thumbnails Help with recursive squares-squares.jpg  

  2. #2
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Magic squares help
    By mjpam in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 03:24 PM
  2. Magic squares LUX method
    By gandalf5166 in forum New To Java
    Replies: 14
    Last Post: 04-17-2010, 05:30 PM
  3. Calculating Squares and Cubes in a table
    By aldorfski_17 in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 08:17 PM
  4. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 08:44 PM
  5. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 10:21 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
  •