Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Help with fractals

Author
Message
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 20th Jan 2010 15:57 Edited at: 20th Jan 2010 16:46
I'm struggling to get my head around fractals, much less actually put one into code. There's a lot of mathematical theory out there but I'm not up to scratch and would like someone to explain what is actually happening.
Most of my confusion is from my unfamiliarity with mathematical notation.
Thanks,
My aim is to learn the ins and outs of fractals and then write a tutorial for DBC users.

Any help is gratefully appreciated

I've made a start with probably the simplest type of fractal/procedural generation (I don't know if this is actually a fractal or not). It is a model of the growth pattern of algae. No graphics, just a simple string with simple rules applied.


Here's the same program with slightly different rules.


[edit]

The strings are helping to simplify what is going on but now I will try to convert this to a recursive function as strings are limited to 255 characters and parsing them is slow.

[edit]

Okay I've done it. It doesn't bare much resemblence to the string version but it isn't limited to twelve generations and the code is much shorter.


Next I will try the Sierpinski Triangle...

"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 20th Jan 2010 18:49 Edited at: 20th Jan 2010 18:51
I was just looking into these the other day. What puzzles me most is not how they work, but how they turn them into 2D or sometimes 3D images. Let alone art. I'll be glad to help you if I can figure these out for myself lol.

EDIT:

I managed to find this in the extras folder:



I know its the mandelbrot fractal but it doesn't look anything like the equation you would find if you look up the mandelbrot equation. I'm going to keep looking into this and see if I can possibly be of any help.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st Jan 2010 04:32 Edited at: 21st Jan 2010 04:33
I tidied the code up a bit and make a zooming animation.
It is really slow though but maybe that is my machine.
Takes about a minute to generate the fractals.


"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 22nd Jan 2010 01:28
Takes about 30 seconds on my computer. Fractals are a bit slow usually. I have a book about them, and Mandelbrots, it's usually easy to convert the code to DB. I think that I did the same one that you did about 6 years ago.

BigDan256
20
Years of Service
User Offline
Joined: 29th Oct 2003
Location: NSW, Australia
Posted: 22nd Jan 2010 05:58 Edited at: 22nd Jan 2010 06:03
Excuse me if my DarkBASIC is a little rusty, but I whipped this up yesterday from the formulas on the wikipedia page:
http://en.wikipedia.org/wiki/L-system



From memory you should be able to find a fair bit of information by looking up "iterated function systems". They work by building formula(s) which have "attractors":
http://en.wikipedia.org/wiki/Attractor

For example, the formula f(x)=x*x:
If you start with x=1, f(f(f(f(f(x)))))=1 because there's an attractor at x=1.
If you start with x=0.5, f(f(f(f(x)))) approaches 0 because there's an attractor at x=0.
If you start with x=2, f(f(f(f(x)))) approaches infinite because the attractor at x=1 "repels above".
Depending on what number you start with, you may end up with a completely different number after many iterations.
By modifying the formula the slightest bit, you can create different types of effects, even a number that jumps up and down in a loop around an attractor. Something like f(x)=-x*x can have completely different attractors.

The L-System fractals "turtle fractals" illustrate where these attractors are in a geometric way.

Fractals "pretty fractals" illustrate how many times you have to iterate in order to break free of these attractors, and instead of using integers or real numbers, it's usually complex numbers (from x,y coordinates).
The mandelbrot formula is something like f(n)=f(n-1)*f(n-1)+c where f(0)=0, and c is your coordinate (between (-1,-1)-(1,1) is the most useful). At each coordinate you calculate how many iterations it takes to "break-free" from an attractor and then give it a colour.

Another way to draw the sierpinski shapes (triangle, square etc.), is to find the midpoint between a point and another point. You can randomly, but quickly draw it using the following code:


I hope I was a little helpful. Just reading it back now I don't think I've made much sense, at least I've hopefully shown a relationship between the pretty fractals and the turtle ones.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd Jan 2010 12:30 Edited at: 22nd Jan 2010 17:24
Thanks BigDan,
I made a nice screen saver out of those.
(If you want to use this as your screen saver build an executable from it, change the extension to .scr and put the file in with the rest of your screen savers, mine are in system32.)


[edit]
I came back to my pc after a while and it was covered in fractals!
It becomes strangely 3D, like there are multiple layers, if you stare at it

"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
BigDan256
20
Years of Service
User Offline
Joined: 29th Oct 2003
Location: NSW, Australia
Posted: 22nd Jan 2010 23:49 Edited at: 23rd Jan 2010 00:31
Nice, I got impatient and removed the Sync line to make it appear faster.

Try making a copy of that koch curve and modify it into the koch snowflake curve:
start:F--F--F
rulesF=F+F--F+F)
angle:60 degrees
http://en.wikipedia.org/wiki/Koch_snowflake

You should just have to change the angle in the plus/minus functions, and remove the middle koch_f in the koch_f function, and then optionally update the koch_start to match.
It produces a nice snowflake-like pattern.

I also found this other one on wikipedia you could use for something (Maybe a screen wipe or something, instead of drawing a line, copy a section of an image to the screen):
http://en.wikipedia.org/wiki/Hilbert_curve

If you want text instead of pictures like your first post mentions, you can replace the drawing commands with string results:


Edit:
In the dark fractal example above, the calc_pixel looks to be the same formula as f(n)=f(n-1)*f(n-1)+c, f(0)=0
Since it's using complex numbers (formed like a+b*i), you have to treat the real and imaginary part seperately as a# and b#.

They've just expanded the equation out a bit:
f(n)=(f_a(n-1)+f_b(n-1)*i)*(f_a(n-1)+f_b(n-1)*i)+c_a+c_b*i
=> f_a(n-1)*f_a(n-1) + f_b(n-1)*f_b(n-1)*i*i + 2*f_a(n-1)*f_b(n-1)*i + c_a + c_b*i
replace the i*i with -1 (i is the square root of -1 )
=> f_a(n-1)*f_a(n-1) - f_b(n-1)*f_b(n-1) + 2*f_a(n-1)*f_b(n-1)*i + c_a + c_b*i
and then you have to split the function into real and imaginary:
=>
f_a(n)=f_a(n-1)*f_a(n-1) - f_b(n-1)*f_b(n-1) + c_a
f_b(n)=2*f_a(n-1)*f_b(n-1) + c_b

Login to post a reply

Server time is: 2024-03-29 15:46:51
Your offset time is: 2024-03-29 15:46:51