Results 1 to 4 of 4
  1. #1
    JavaGirlie is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Arrow Recursive squares exercise java

    Hi guys!
    I am currently taking my first programming course and we have been given an exercise to produce recursive squares.

    Recursive squares exercise java-sieppaa.png

    Here is the code for the first part.
    Java Code:
    public class RecursiveSquares {
    
        // plot a square, centered on (x, y) of the given side length
        // with a light gray background and black border
        public static void drawSquare(double x, double y, double size) {
            StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
            StdDraw.filledSquare(x, y, size/2);
            StdDraw.setPenColor(StdDraw.BLACK);
            StdDraw.square(x, y, size/2);
        }
    
        // plot an order n recursive squares pattern
        // centered on (x, y) of the given side length
        public static void draw(int n, double x, double y, double size) {
            if (n == 0) return;
    
            drawSquare(x, y, size);
    
            // 2.2 ratio looks good
            double ratio = 2.2;
    
            // recursively draw 4 smaller trees of order n-1
            draw(n-1, x - size/2, y - size/2, size/ratio);    // lower left  
            draw(n-1, x - size/2, y + size/2, size/ratio);    // upper left  
            draw(n-1, x + size/2, y - size/2, size/ratio);    // lower right 
            draw(n-1, x + size/2, y + size/2, size/ratio);    // upper right
        }
    
    
        // read in a command-line argument N and plot an order N recursive
        // squares pattern
        public static void main(String[] args) {
            int N = Integer.parseInt(args[0]);
            double x = 0.5, y = 0.5;   // center of square
            double size = 0.5;         // side length of square
            draw(N, x, y, size);
        }
    
    }
    Could you guys think of an algorithm on how I can do the rest of them?

    Thank you!!
    Last edited by JosAH; 11-24-2012 at 03:29 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: Recursive squares exercise java

    For variation a) you first draw the big square and then you draw the four smaller squares on top of it; for variation d) you first draw the four small squares and the big square on top of them; variations b) and c) are similar variations with a different order of drawing ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JavaGirlie is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Recursive squares exercise java

    thank you for the answer

    how can I draw them in different order?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Recursive squares exercise java

    How would you draw them in a different order by hand? Likely re ordering them will come down to just reorganizing the lines of code.

Similar Threads

  1. Java exercise
    By Mbka in forum New To Java
    Replies: 9
    Last Post: 06-28-2012, 09:58 PM
  2. Help with recursive squares
    By luke in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 12:47 AM
  3. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-14-2010, 12:14 AM
  4. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-14-2010, 12:13 AM
  5. exercise of recursive method
    By amexudo in forum New To Java
    Replies: 2
    Last Post: 03-09-2008, 06:55 PM

Tags for this Thread

Posting Permissions

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