Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Recursive Fractal

recursive

  • Please log in to reply
3 replies to this topic

#1 MahdiAlQaffas

MahdiAlQaffas

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 10 April 2012 - 03:45 PM

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 it draws 4 smaller rectangles attached to the corners of the first rectangle. Then it draws 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 of the program should be like this picture

Untitled.jpg

This is the code I wrote

import java.awt.*;
import java.awt.event.*;

public class lab32b
{
		public static void main(String args[])
		{
				GfxApp gfx = new GfxApp();
				gfx.setSize(1000,750);
				gfx.addWindowListener(new WindowAdapter() {public void
				windowClosing(WindowEvent e) {System.exit(0);}});
				gfx.show();
		}
}
class GfxApp extends Frame
{
		public void paint(Graphics g)
		{
				int xCo = (1024/2) - (1024/4)/2;
				int yCo = (768/2) - (768/4)/2;
				int length = 1024/4;
				int width = 768/4;
				g.fillRect(xCo, yCo, length, width);
				Draw(g, xCo, yCo, length, width);
		}
		public void Draw(Graphics g, int maxX, int maxY, int length, int width)
		{
				if(width>=1)
				{
						g.fillRect(maxX, maxY, length, width);
						Draw(g,maxX-length/2, maxY-width/2,length/2,width/2);
						Draw(g,maxX+length, maxY-width/2,length/2,width/2);
						Draw(g,maxX-length/2, maxY+width,length/2,width/2);
						Draw(g,maxX+length, maxY+width,length/2,width/2);
				}
		}
}

but instead of the output being like the previous picture my output is

Untitled.png

Can someone explain to me how to fix this

thank you
Mahdi
  • 0

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 10 April 2012 - 05:11 PM

how to fix this

Can you explain the logic you are using to compute the locations of each square you draw?

Here's an idea to help you see what is happening:
Create an array of Color objects and use them to set the color of each rectangle before it is filled. Have an index that you increment and wrap it around to 0 when it gets to the end of the array. You should get an idea of how to change your logic when you see the colored rectangles.
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 10 April 2012 - 07:33 PM

The wrong lies in your algorithm. The number of child rectangles (let us call the corner rectangle) of the root rectangle is only 4. All other cases the number of child rectangles is 3. Here are the steps to draw...
  • Draw root rectangle.
  • Call the recursive method 4 times with the 4 child rectangle of root rectangle.
  • Inside the recursive method, draw the rectangle itself and call the recursive method for other 3 child rectangles.
I write the logic in C# (mostly similar to Java)...
public enum Corner { LeftTop, RightTop, RightBottom, LeftBottom}

protected override void OnPaint(PaintEventArgs e)
{
	int windowWidth = 1024; // better replace 1024 by your windows client rectangle's width
	int windowHeight = 786; // better replace 786 by your windows client rectangle's height
	int w = windowWidth / 4;
	int h = windowHeight / 4;

	int x = (windowWidth - w) / 2;
	int y = (windowHeight - h) / 2;

	e.Graphics.FillRectangle(Brushes.Black, x, y, w, h);

	DrawChildRect(e.Graphics, x - w / 2, y - h / 2, w / 2, h / 2, Corner.LeftTop);
	DrawChildRect(e.Graphics, x + w, y - h / 2, w / 2, h / 2, Corner.RightTop);
	DrawChildRect(e.Graphics, x + w, y + h, w / 2, h / 2, Corner.RightBottom);
	DrawChildRect(e.Graphics, x - w / 2, y + h, w / 2, h / 2, Corner.LeftBottom);
}


public void DrawChildRect(Graphics g, int x, int y, int w, int h, Corner parentCorner)
{
	if (w >= 1) {
		g.FillRectangle(Brushes.Black, x, y, w, h);

		switch(parentCorner){
			case Corner.LeftTop:
				DrawChildRect(g, x - w / 2, y - h / 2, w / 2, h / 2, Corner.LeftTop);
				DrawChildRect(g, x + w, y - h / 2, w / 2, h / 2, Corner.RightTop);
				DrawChildRect(g, x - w / 2, y + h, w / 2, h / 2, Corner.LeftBottom);
				break;
			case Corner.RightTop:
				DrawChildRect(g, x - w / 2, y - h / 2, w / 2, h / 2, Corner.LeftTop);
				DrawChildRect(g, x + w, y - h / 2, w / 2, h / 2, Corner.RightTop);
				DrawChildRect(g, x + w, y + h, w / 2, h / 2, Corner.RightBottom);
				break;
			case Corner.RightBottom:
				DrawChildRect(g, x + w, y - h / 2, w / 2, h / 2, Corner.RightTop);
				DrawChildRect(g, x + w, y + h, w / 2, h / 2, Corner.RightBottom);
				DrawChildRect(g, x - w / 2, y + h, w / 2, h / 2, Corner.LeftBottom);
				break;
			case Corner.LeftBottom:
				DrawChildRect(g, x - w / 2, y - h / 2, w / 2, h / 2, Corner.LeftTop);
				DrawChildRect(g, x + w, y + h, w / 2, h / 2, Corner.RightBottom);
				DrawChildRect(g, x - w / 2, y + h, w / 2, h / 2, Corner.LeftBottom);
				break;
			default:
				break;
		}
	}
}

Hope this will be helpful for you. Note that, it is better to draw the fractal on an image/buffer in a thread and finally draw it on the window.
  • 1

#4 MahdiAlQaffas

MahdiAlQaffas

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 10 April 2012 - 08:20 PM

Thank you very much kernelcoder, the program worked when I added the changes you posted
Thank you again for your help
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download