Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with recursive squares

799403Apr 18 2011 — edited Apr 20 2011
Hello folks,
I've posted my question at this forum as well but since I got no response so far I've decided to post in this community here. Hopefully somebody has an idea or suggestion to my problem.

I am trying to make a program that produces some recursive squares as shown here
public class RecursiveSquares {
	
	static void drawSquare(double x, double y, double size){
			
                        // Using Cartesian coordinate system, where 
                        // (x0, y0) is the lower left corner 
                        // and (x1,y1) is the upper right corner		
			double x0 = x/2;  
			double y0 = y/2;
			double x1 = x + x/2;
			double y1 = y + y/2;
			size = size/2.2;
			
                        // I am using pre-written class StdDraw
			StdDraw.setPenColor(StdDraw.GRAY);
			StdDraw.filledSquare(x0, y1, size);
						
			StdDraw.setPenColor(StdDraw.BLACK);
			StdDraw.square(x0, y1, size);
			
			StdDraw.setPenColor(StdDraw.GRAY);
			StdDraw.filledSquare(x1, y1, size);
						
			StdDraw.setPenColor(StdDraw.BLACK);
			StdDraw.square(x1, y1, size);
			
			StdDraw.setPenColor(StdDraw.GRAY);
			StdDraw.filledSquare(x1, y0, size);
						
			StdDraw.setPenColor(StdDraw.BLACK);
			StdDraw.square(x1, y0, size);
			
			StdDraw.setPenColor(StdDraw.GRAY);
			StdDraw.filledSquare(x0, y0, size);
						
			StdDraw.setPenColor(StdDraw.BLACK);
			StdDraw.square(x0, y0, size);
			
		}
	
	static void draw(int n, double x, double y, double size){
		if(n==0){
			return;
		}
		else{
			drawSquare(x,y,size);
			                                                 
			double x0 = x/2;                        
			double y0 = y/2;                                                           
			double x1 = x + x/2;                                                      
			double y1 = y + y/2;                                                    
			size = size/2.2;                                                           
			                                                                             
			draw(n-1, x0, y1, size);                                                
			draw(n-1, x1, y1, size);                                           
			draw(n-1, x1, y0, size);            
			draw(n-1, x0, y0, size); 
		}                                   
		
	}
	
	
	public static void main(String[] args) {
		
		draw(4, .5, .5, .25);

	}

}
So, the above code doesn't actually produce what I need. The result is here

Any ideas or suggestions are greatly appreciated.

Thank you in advance.
Luke

Edited by: Luke on Apr 18, 2011 6:42 PM

Edited by: Luke on Apr 18, 2011 6:43 PM
This post has been answered by walterln on Apr 20 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2011
Added on Apr 18 2011
15 comments
1,088 views