# Thread: Mandelbrot using thinBasic FreeBasic Interop

1. ## Mandelbrot using thinBasic FreeBasic Interop

An example using FreeBasic inside thinBasic.
Just open script in thinAir and execute. thinBasic will do the rest.
Very fast execution

thinBasic 1.11.1.x minimum version required

```#MinVersion 1.11.1

#compiled "===Mandelbrot Example==="

'----------------------------------------------------------------------------
function Mandelbrot_Screen Cdecl (byval XMax as long, byval YMax as long, ByVal depth As Long) As long Export
'----------------------------------------------------------------------------
ScreenRes XMax, YMax, depth
end function

'----------------------------------------------------------------------------
function Mandelbrot_Show Cdecl (byval XMax as long, byval YMax as long, byval lLimit as long) As long Export
'----------------------------------------------------------------------------
dim lx  as double
dim ly  as DOUBLE
dim x   as long
dim y   as long
dim a   as DOUBLE
dim b   as DOUBLE
dim c   as DOUBLE
dim x2  as DOUBLE
dim y2  as DOUBLE
dim a2  as DOUBLE
dim b2  as DOUBLE
dim z   as double

lx = 3 / XMax
ly = 2 / YMax

For x = 1 To XMax
For y = 1 To YMax

a = 0
b = 0
c = 0
x2 = lx * x - 2
y2 = ly * y - 1

While c < 50
a2 = a * a - b * b
b2 = 2 * a * b
a = a2 + x2
b = b2 + y2
z = a * a + b * b
If z >= 4 Then Exit While
c = C + 1
Wend

Select Case c
Case 50
PSet (x, y), Rgb(0, 0, 0)
Case is < lLimit
PSet (x, y), Rgb(245-((17 - c) * 3), 0, 0)
Case is < 33
PSet (x, y), Rgb(245, ((c - 16) * 16) - 16, 0)
Case Else
PSet (x, y), Rgb(641-(c * 12), 245, ((c - 32) * 15))
End Select

Next
Next

return 0

end Function

'----------------------------------------------------------------------------
function Mandelbrot_Sleep Cdecl () As long Export
'----------------------------------------------------------------------------
sleep
end function

#endcompiled

long lWidth   = 1024
long lHeight  =  768
long lDepth   =   32
long l
'---Call FreeBasic functions
Mandelbrot_Screen(lWidth, lHeight, lDepth)
for l = 2 to 21
Mandelbrot_Show(lWidth, lHeight, l)
next
Mandelbrot_Sleep
```

Capture.PNG

2. Thanks Eros, this speed was a dream for A. K. Dewdney in August 1985 issue of Scientific American under the title "Computer Recreations: A computer microscope zooms in for a look at the most complex object in mathematics".
https://static.scientificamerican.co...Mandelbrot.pdf
the microcomputer was used is IBM PC uses the 8088 microprocessor as told in page 10 ( pages 2 to 7 is advs.) if you are fortunate and rich you will have a color monitor pre VGA era. and it takes a Mandelbrot set to calculate the whole night on such computers.
i will read some parts of the article to see the number ranges in which Mandelbrot set is active

3. i have prepared this in a hurry, so it may needs tuning, i have used Eros example above as a template
i want to refer to the excelent example by largo_winch in 2011 here http://www.thinbasic.com/community/s...el-(mandelbrot
near the end of that page there is mandelbrot_canvas1e.tbasic example
it is using explicitly the real Mandelbrot set coordinates in which we can zoom or fly at any part of the set, the example is using the coordinates:
xmin = -.65 : xmax = -.45 : ymin = .52 : ymax = .72 : numiter = 215
but to view the whole set we can use
xmin = -2.0 : xmax = 1 : ymin = -.8 : ymax = 1 : numiter = 215
you must must not choose the dark area, (it is the Hell) only choose the colored.
of course Eros example can be adjusted to refer to the real coordinates explicitly.
i almost copied the thinbasic code inside compiled block, and using the nice debugger it works quickly
but it needs too much elaborations, i haven't thought about it but the results is amazing
to see the whole mandel set use instead of line 22
xmin = -2.0 : xmax = 1 : ymin = -.8 : ymax = 1 : numiter = 215
so this code is a microscope to different countries in the planet Mandelbrot set
now why i need to add a small loop: ??:
for i = 1 to 1 Line 31 else i will get error msg from FB:
error 4: Duplicated definition, color_map in 'ReDim color_map(216)
```#MinVersion 1.11.1

#compiled "===Mandelbrot Example==="

'----------------------------------------------------------------------------
function Mandelbrot_Screen Cdecl (byval xXMax as long, byval yYMax as long, ByVal depth As Long) As long Export
'----------------------------------------------------------------------------
ScreenRes xXMax, yYMax, depth
end function

'----------------------------------------------------------------------------
function Mandelbrot_Show Cdecl (byval xXMax as long, byval yYMax as long, byval lLimit as long) As long Export
'----------------------------------------
dim hwin As long
dim as single tmr, xmin, xmax, ymin, ymax, cx, cy, dcx, dcy, x, y, zr, zi
dim as long xi, j,k, numiter, numxpix, numypix, te, ts
dim color_map(216) As Long
dim stuff_done As Long
dim s As String

xmin = -.65 : xmax = -.45 : ymin = .52 : ymax = .72 : numiter = 215
'xmin = -2.0 : xmax = 1 : ymin = -.8 : ymax = 1 : numiter = 215
numxpix = 800'400 '800
numypix = 600'300 '600
dcx = (xmax - xmin)/(numxpix - 1)
dcy = (ymin - ymax)/(numypix - 1)

dim i as long
for i = 1 to 1
'-----------------------> problem 1 solved array dimension ! --------------------------->
ReDim color_map(216) '(6*6*6) 216
For xi = 1 To 6
For j = 1 To 6
For k = 1 To 6     'but "6*j + 36*k" doesn't work here (I don't grasp it!)
color_map(xi + 5*j + 30*k) = Rgb(51*j,51*xi,51*k)
Next
Next
Next
'-----------------------> problem 1 solved array dimension ! --------------------------->
ts  = Timer
tmr = Timer
cx  = xmin
'-----------------------> 2 --------------------------->
For xi = 1 To numxpix
cy = ymax
For j = 1 To numypix
x = cx
y = cy
For k = 1 To numiter
zr = x*x - y*y + cx
zi = 2*x*y + cy

'-----------> important thingy solved !------------->
If (zr*zr + zi*zi) > 4 Then Exit For
x = zr : y = zi
'Console_WriteLine "FOR/NEXT zr=" + Format\$(zr)
'Console_WriteLine "FOR/NEXT zi=" + Format\$(zi)
'-----------> important thingy solved !------------->
Next
cy += dcy
'-------------------> problem zone 2 solved! --------------------------------->
'Canvas_SetPixel(xi, j, color_map(numiter+2-k) ) ' testing ok
PSet (xi, j), color_map(numiter+2-k) 'Rgb(0, 0, 0)

'-------------------> problem zone 2 solved --------------------------------->
Next
cx += dcx
Next
'Canvas_Clear(color_map(numiter+2-k)) 'starts rendering again ;)

'Canvas_Color(Rgb(255,10,10))
'Canvas_SetPos(20,120)
's = Format\$(("Took ##.## seconds ") + Format\$(Timer - tmr) )
'Canvas_Print s
'MsgBox 0, "testing ok?" ' example works

'Canvas_Redraw
'Loop
next

'te = Timer
'Canvas_WaitKey(27)

'Canvas_Window End
return 0

End Function
function Mandelbrot_Sleep Cdecl () As long Export
'----------------------------------------------------------------------------
sleep
end function
#endcompiled
long lWidth   = 1024
long lHeight  = 768
long lDepth   =   32
long l
'---Call FreeBasic functions
Mandelbrot_Screen(lWidth, lHeight, lDepth)
'for l = 2 to 21
Mandelbrot_Show(lWidth, lHeight, l)
'next
Mandelbrot_Sleep
'console_close
```
mand.PNG

4. Nice theme, fractals... I remember back in the DOS era, I used to compute them with fractint (which already had a large list of formulas), and today, I just discovered that it's still slowly being developed, more than 30 years after first release !

5. no need to the line 32: ReDim color_map(216)
now no need to line 30: For i = 1 to 1 and line 80: Next
so remove it

There are no members to list at the moment.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•