Code archives/Graphics/Mandelbrot in a nutshell

This code has been declared by its author to be Public Domain code.

Download source code

Mandelbrot in a nutshell by BlitzProg2012
This tiny piece of code will produce the mandelbrot set from coordinates (-2,-1) to (1,1), with 64 iterations.
w=800:h=600:Graphics w,h,16,2:LockBuffer For x=0 To w-1 For y=0 To h-1
WritePixel x,y,m((3.0*x/w)-2,(2.0*y/h)-1):Next:Next:UnlockBuffer:Flip:MouseWait
Function m(k#,l#,i=63):For c=0 To i:If (x#*x)+(y#*y)>4 Return c*263168+128
t#=(x*x)-(y*y)+k y=(2*x*y)+l x=t:Next:End Function

Comments

Yasha2012
...every time you run this code, an angel stands up and applauds.


Jesse2012
I had to try it out:
w=800h=600Graphics w,h For x=0 To w-1 For y=0 To h-1 c = m((3.0*x/w)-2,(2.0*y/h)-1)SetColor c Shr 16&$FF,c Shr 8&$FF,c&$FF
Plot x,y Next Next Flip()WaitKey()Function m(k#,l#,i=63)For c=0 To i If (x#*x)+(y#*y)>4 Return c*263168+128
t#=(x*x)-(y*y)+k y=(2*x*y)+l x=t Next End Function


Nice!


Matty2012
Cool.


Guy Fawkes2012
Easier version of mandlebrot code :P




CyBeRGoth2012
Elegant, maths can do some amazing things, not in my hands though (unless I have a calculator handy :P)


BlitzProg2012
this one is particular and years old, the idea behind it is apply a formula on a dot repeatedly and check if it would escapes to infinity, if it fail, put a black dot, if success, color depending on how long it took.

The function only look difficult to understand because you got to write complex numbers as real numbers and do the appropriate maths to match the results.

Real function in this code
Function m(k#,l#,i=63)
For c=0 To i:If (x#*x)+(y#*y)>4 Return c*263168+128
t#=(x*x)-(y*y)+k y=(2*x*y)+l x=t:Next:End Function


Its complex equivalent would be
Function m(d,i=63):For c=0 To i:If abs(z)>2 Return c*263168+128
z=z*z+d:Next:End Function



_PJ_2012
I still cannot grasp how the complex numbers are represented by real ones in the various code examples (across all manner of languages) for fractal calculation.

yeah.. even with that clear example above!
I assume that complex conjugates are squared to give real negatives.
Shame, cos I really love fractals, they are perfect for many areas of games, especially when procedural generation is required and may be scaled - i.e. landscapes, trees/vegetation, Other forms of LOD


BlitzProg2012
_PJ_ Well, if you know what a complex value is, it shouldn't actually be too complicated to replace!

If you studied complex numbers, you just focus on doing arithmetic operations and storing x and y with separate variables. x is the real part and y, the imaginary part.

A complex number has the form z = x+iy. You're only interested in x and y to draw stuff. So you want to know the new x+iy that will be next.

Assuming i² = -1
z² = (x+iy)² = x² + 2xyi + i²y² = x² - y² + i(2xy)

And now you add that "d" value. let d = k + il just like in my code
z² + d = (x²+2xyi+i²y²) + (k + il) = x² - y² + k + i(2xy + l)

So if you do z²+d, x becomes x² - y² + k and y becomes 2xy + l

The use of a temp variable (t#) avoid the loss of the original value of x too early, allowing to compute the new y

After this you got your new z = x + iy and you can now do it again ^_^


Code Archives Forum