MahdiAlQaffas Posted March 31, 2012 Report Posted March 31, 2012 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 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 Quote
CraigD Posted April 5, 2012 Report Posted April 5, 2012 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.