Jump to content
Science Forums

Recursive Fractal


Recommended Posts

hello everybody

 

I am writing a program in java that draws a square fractal recursively. The program starts by drawing a rectangle in the center of the window (1/4 of the window). Then I need to draw 4 smaller rectangles attached to the corners of the first rectangle. Then I need to draw 3 smaller rectangles attached to the corners to the corners of the previous rectangle and so on until it reaches to 1 pixel. Each one of the succeeding rectangles is half the width and height or the preceding rectangle. The output should be like the picture at the end of the post

 

post-30479-0-25530400-1333224206_thumb.jpg

 

I am new to computer science so please help even with a pseudo code that can explain the steps and calculations to draw the rectangles.

 

thank you in advance

Link to comment
Share on other sites

Welcome to hypography, Mahdi! :)

 

Some simple pseudocode:

Draw(Top,Left,Length,Depth,MaxDepth){
 if Depth<=MaxDepth {
   Rectangle (Top,Left)-(Top+Length,Left+Length,Depth+1,MaxDepth)
   Draw(Top-Length/2,Left-Length/2,Length/2,Depth+1,MaxDepth)
   Draw(Top+Length,Left-Length/2,Length/2,Depth+1,MaxDepth)
   Draw(Top-Length/2,Left+Length,Length/2,Depth+1,MaxDepth)
   Draw(Top+Length,Left+Length,Length/2,Depth+1,MaxDepth)
 }
}
Draw(-1,-1,2,1,whatever)

Replace "whatever" with an integer for how "deep" you want the fractal image to be: 1 draws 1 box, 2 draws 5 boxes, 3 draws 17 visible boxes, 4 draws 53, etc.

 

The existence of a primitive rectangle drawing command, "Rectangle", is assumed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...