We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Unintentional fractal generator
Page Index Toggle Pages: 1
Unintentional fractal generator (Read 993 times)
Unintentional fractal generator
Feb 1st, 2010, 11:16pm
 
Hi there...

I'm writing a program which is a sort of game where you can create "cities" made up of different materials, and then apply different "ruin" types to them and see them disintegrate. The quality of the disintegration depends on the material and the ruin type - e.g ice behaves very differently form lead when you apply an "earthquake" to it.

The program (not finished) works with squares - starting off with large squares, which are fragmented into smaller squares with varying velocities and probabilities, depending on the parameters. The square objects are contained in an arraylist. At the moment, the program just creates one big square which can be fragmented by "earthquake" or "freeze" methods. I am working on a "hurricane" method which will make use of a helper method called splitsquare which will fragment a square into four parts.

This is the method I am having problems with. Applying splitsquare repeatedly (using the 's' key) does not fragment all the squares every time as required, but only certain ones - and a familiar fractal pattern emerges in the big square ( it has a name, but I forget ....).

Well, if anyone can be bothered wading through this explanation and the code, you can find it here: http://pastebin.ca/1775306

And if you do....thanks!!!

Re: Unintentional fractal generator
Reply #1 - Feb 2nd, 2010, 1:43am
 
Your splitting square function adds new squares into the ArrayList of squares. You'd expect it would add then at the end of the ArrayList, but it doesn't. It adds them at the front. So when the split square function is called again for the next square to be split, it's sometimes called on one of the smaller squares that was just added due to the last split.

According to this, this seems like it's not behaving as it should. Oh well.

But I have good news: It sounds worse than it is.
There is a simple solution:

Quote:
  else if(key=='s')
  {
    for(int i=0; i<n; i++)
    {  
      getsquare(n-i-1).splitsquare(n-i-1);
    } 
  }



That's right, just do them in reverse order.  Shocked
Re: Unintentional fractal generator
Reply #2 - Feb 2nd, 2010, 2:04am
 
> Your splitting square function adds new squares into the ArrayList of squares. You'd expect it would add then at the end of the ArrayList, but it doesn't. It adds them at the front.

yes, that's the cause, updating the array you're iterating through, but the description isn't quite accurate and is a bit subtler than that. it is adding them to the end. but it's also removing the original square so everything after it gets shifted up one, meaning the first of the newly added squares is now in the list of things to be processed.

A B C D

four squares to iterate through

remove A, add A1 A2 A3 A4

B C D A1 A2 A3 A4

A1 is now square 4

you'll also find it's skipping B as B is new 1st square but you're now onto the second...

same solution though - start at the end and move forwards so the only squares getting shifted are ones that've already been processed. or use two arrays.

(edit - typo - changed 'not' to 'now'!)
Re: Unintentional fractal generator
Reply #3 - Feb 2nd, 2010, 8:14am
 
That's great... makes perfect sense.  Thanks for your help, guys, much appreciated.
Re: Unintentional fractal generator
Reply #4 - Feb 3rd, 2010, 5:12am
 
Great piece of code!

I like it a lot!
Page Index Toggle Pages: 1