PDA

View Full Version : Nice fractals



ErosOlmi
18-07-2007, 17:42
http://content.techrepublic.com.com/2346-10877_11-94666.html?tag=nl.e099.dl071807

Michael Clease
18-07-2007, 23:18
Try this

kryton9
19-07-2007, 00:40
Thanks nice link and program. Fractals are amazing and always fun to look at!

ErosOlmi
19-07-2007, 08:53
Thanks Abraxas.
Those kind of scripts shows the limit in interpreted languages.
I will use it to study more tricks to add more execution speed.

Petr Schreiber
19-07-2007, 10:25
Nice fractals!,

Mandelbrot is good, I tried to optimize Abraxas code a bit, but I got only 20% increase :(


SUB DoBRot()
local t1, t2 as long
local wminone as long = width - 1
local hminone as long = height - 1

TBGL_BeginPoly %GL_POINTS
xmin = -2.0
ymin = -1.3
xmax = 1.0
ymax = 1.3
integralX = (xmax - xmin) / width
integralY = (ymax - ymin) / height
x = xmin
For S = 1 To wminone
y = ymin
For Z = 1 To hminone
x1 = 0
y1 = 0
looper = 0
While (looper < 1000 And Sqr( x1*x1 + y1*y1 ) < 2)
Looper += 10
xx = x1*x1 - y1*y1 + x
y1 = 2*x1*y1 + y
x1 = xx
WEND
TBGL_Color ColorMap_R(Looper), ColorMap_G(looper), ColorMap_B(looper)
TBGL_Vertex s,z
y += integralY
Next
x += integralX
Next
TBGL_EndPOLY
END SUB


I just put tbgl_BeginPoly/tbgl_EndPoly out of the loops as it is not necessary for each point, added += on two places, removed () where not needed. I am sure it could be speeded up even more somehow, but I am don't how to do it :)


Bye,
Petr

Petr Schreiber
19-07-2007, 10:32
Ok,

here is version which should run 47% faster - using FOR/NEXT with step !


SUB DoBRot()
local t1, t2 as long
local wminone as long = width - 1
local hminone as long = height - 1

TBGL_BeginPoly %GL_POINTS
xmin = -2.0
ymin = -1.3
xmax = 1.0
ymax = 1.3
integralX = (xmax - xmin) / width
integralY = (ymax - ymin) / height
x = xmin
For S = 1 To wminone
y = ymin
For Z = 1 To hminone
x1 = 0
y1 = 0
for looper = 0 to 1000 step 10
if Sqr( x1*x1 + y1*y1 ) >= 2 then exit for
xx = x1*x1 - y1*y1 + x
y1 = 2*x1*y1 + y
x1 = xx
next
TBGL_Color ColorMap_R(Looper), ColorMap_G(looper), ColorMap_B(looper)
TBGL_Vertex s,z
y += integralY
Next
x += integralX
Next
TBGL_EndPOLY
END SUB



Bye,
Petr

Michael Clease
19-07-2007, 12:02
thanks Petr.

Ive added some different sub routines to give you timing examples.

DoBrot1() '= 14.5 seconds
DoBrot2() '= 14.2 seconds
DoBrot3() '= 11.2 seconds
DoBrot4() '= 14.0 seconds

how does it compare on your machine?

ErosOlmi
19-07-2007, 12:38
Similar time here:
1: 12,6
2: 14,2
3: 10,3
4: 12,7

A great example to be used to make optimization.

Thanks a lot
Eros

Petr Schreiber
19-07-2007, 12:46
Hi,

grr, worst results :D
CPU is AMD Sempron 64 3400+ ( 1.8GHz real freq )


18.68
18.03
14.77
18.33


Really nice benchmark!

Bye,
Petr

ErosOlmi
19-07-2007, 13:05
The biggest time is loosed in


While (lo < 1000 And Sqr( x1 * x1) + (y1 * y1) < 2)
Lo += 10 ' the loop counter
xx = x1 * x1 - y1 * y1 + x
y1 = 2 * x1 * y1 + y
x1 = xx
incr counter
WEND

Commentig out the full WHILE/WEND it takes just half second.

I've added a counter to be able to check how many times it is executed that while.
In DoBRot1 WHILE/WEND is executed 3.729.844 times. A lot for an interpreter but I'm sure I will get some more optimization.

matthew
19-07-2007, 15:17
Nice Fractals. :)

Maybe it's about time I got around to converting some of my Fractal Programmes to thinBASIC.

kryton9
20-07-2007, 01:52
Couldn't that routine that takes the time be written in a compiled language and used in thinBasic? Would it need to be a dll or could it be called another way?

Michael Clease
20-07-2007, 01:54
How about in 3D.


SUB DoBRot6()
local xmin As DOUBLE value -2.0
local xmax As DOUBLE value +1.0
local ymin As DOUBLE value -1.3
local ymax As DOUBLE value +1.3
local x As DOUBLE value xmin
local y As DOUBLE
local x1 As DOUBLE
local y1 As DOUBLE
local xx As DOUBLE
local loo As IntEGER value 0
local s As IntEGER
local z As IntEGER
local integralX As DOUBLE value (xmax - xmin) / %ScreenWidth
local integralY As DOUBLE value (ymax - ymin) / %ScreenHeight

TBGL_RenderMatrix3D
TBGL_RESETMATRIX
TBGL_Camera 20,20,20,0,0,0
TBGL_ROTATE 30,0,1,1

TBGL_BeginPoly %GL_POINTS
For S = 1 To %ScreenWidth - 1
y = ymin
For Z = 1 To %ScreenHeight - 1
x1 = 0
y1 = 0
loo = 0
DO
if Sqr( x1*x1 + y1*y1 ) >= 2 then exit DO
xx = x1*x1 - y1*y1 + x
y1 = 2*x1*y1 + y
x1 = xx
loo += 100
LOOP until loo >= 1000
TBGL_Color ColorMap_R(Loo), ColorMap_G(loo), ColorMap_B(loo)
TBGL_Vertex s/100,z/100,loo/60
y += integralY
Next
x += integralX
Next
TBGL_EndPOLY
END SUB


Kryton i have seen a few ASM versions around.

ErosOlmi
20-07-2007, 08:18
Ken,

do you want to see the difference with a compiled version?
See attached files. It is the same script of Abraxas but I've created a little thinBasic module called MyMandel that contains just one function that substitute the inner WHILE/WEND part of DoBRot1 function. Module is compilable using Power Basic.

Can you see the difference? In my box I passed from about 13 secs to 0.9 secs

Because in those cases we are dealing just with numbers, it is quite easy to develop those kind of modules using Power Basic, C, Delphi or ASM following thinBasic SDK indications.

Ciao
Eros

ErosOlmi
20-07-2007, 11:03
An this is another way not using a thinBasic module but a standard DLL you can create with any language (in this case I've used Power Basic) and declaring the needed functions using DECLARE statement.

Ciao
Eros

Michael Clease
20-07-2007, 11:38
Found some OLD examples using asm.

ErosOlmi
20-07-2007, 11:43
Another little speed improve can be obtained if



TBGL_Color ColorMap_R(Lo), ColorMap_G(lo), ColorMap_B(lo)


could be



TBGL_RGBColor lColor


where lColoris already a precalculated LONG value containing a RGB color. In this case thinBasic has to parse just 1 numeric expression instead of 3

I will ask Petr if not a problem to add it.

Ciao
Eros

ErosOlmi
20-07-2007, 11:44
Thanks Abraxas.
I've been always fascinated by fractals. The more sources, the more help.

I will create a dedicated forum for fractals and move this post

Petr Schreiber
20-07-2007, 11:48
Hi,

good idea with RGB. I will see if it won't slow down on TBGL compiled part then to decompose again to R,G,B; but it should be ok. I am adding it on wishlist!


Bye,
Petr

ErosOlmi
20-07-2007, 14:35
Hi all.

Here again with a fast Mandelbrot fractal using an external DLL developed with FreeBasic.
You will found DLL sources with a DLL project using FbEdit you can get at: http://www.radasm.com/fbedit/
Compile using FbEdit or by command line you can get into commented line.

Have fun
Eros

matthew
20-07-2007, 14:51
Most of the Fractals I've written have been in Basic4GL.

But here (http://gamedesign.wikidot.com/forum/t-9736/simple-freebasic-fractal) is One that I wrote in FreeBASIC. :)

kryton9
20-07-2007, 18:23
Thanks guys will check all this out tonight, have to run now. Lots of good things to come home to tonight, new topdown and all these nice fractal examples, thanks!

kryton9
20-07-2007, 23:45
Thanks Eros for the examples. The non module dll is smaller than the one called from within the program, so that is interesting.
But neat to know we can use dll's for these kind of loops.

Abraxas the demos are really nice. I need to find this link to an online book about fractals, it was really nice. I just skimmed through it and didn't try it out yet.
http://sprott.physics.wisc.edu/sa.htm