Results 1 to 2 of 2
Thread: Help with recursive squares
- 04-18-2011, 07:32 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
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:
I haven't included the recursive calls in the else statement because they don't make sense.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; } }
The pseudocode would be something like:
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.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
Any ideas or suggestions are greatly appreciated.
Thank you in advance.
Luke
- 04-18-2011, 11:47 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Similar Threads
-
Magic squares help
By mjpam in forum New To JavaReplies: 3Last Post: 06-30-2010, 02:24 PM -
Magic squares LUX method
By gandalf5166 in forum New To JavaReplies: 14Last Post: 04-17-2010, 04:30 PM -
Calculating Squares and Cubes in a table
By aldorfski_17 in forum New To JavaReplies: 4Last Post: 03-22-2010, 07:17 PM -
help with perfect squares
By AmplifiedKid in forum New To JavaReplies: 1Last Post: 09-19-2009, 07:44 PM -
Perfect Squares
By divyachaparala in forum New To JavaReplies: 4Last Post: 02-05-2008, 09:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks