## Porting Fractal program to FB

New to FreeBASIC? Post your questions here.
ganache
Posts: 44
Joined: Aug 04, 2016 9:25

### Porting Fractal program to FB

Here is some code for a cool fractal image, written in another version of Basic. A button allows one to generate a new image.
Here the code:

'set up some random colors
for c=0 to 12
col(c)=int(rnd(0)*256)+" "&int(rnd(0)*256)+" "+int(rnd(0)*256)
next c

'set up some random starting positions
a=rnd(0)
b=0.9998
c=2-2*a
dots=12000
x=j=0
y=rnd(0)*12+0.1

'calculate and draw the points
for i=0 to dots
z=x
x=b*y+j
j=a*x+c*(x^2)/(1+x^2)
y=j-z
xp=(x*20)+midx
yp=(y*20)+midy
print #1, "color ";col(i/1000)
print #1, "set ";xp;" ";yp
next i

There are a ton of variables. Can this be ported to FB?
BasicCoder2
Posts: 3402
Joined: Jan 01, 2009 7:03

### Re: Porting Fractal program to FB

Do not understand the math but this sort of produces a pattern.

Code: Select all

`screenres 640,480,32dim as integer midx = 320dim as integer midy = 240'set up some random starting positionsdim as single  a = rnd(1)dim as single  b = 0.9998dim as single  c = 2-2*adim as integer dots = 12000dim as single  x,y,z,jdim as single  xp,ypx = j =0y = rnd(1)'calculate and draw the pointsfor i as integer = 0 to dots    z = x    x = b*y+j    j = a*x+c*(x^2)/(1+x^2)    y = j-z    xp = x*20+midx    yp = y*20+midy    pset (xp,yp),rgb(255,0,0)next isleep`
ganache
Posts: 44
Joined: Aug 04, 2016 9:25

### Re: Porting Fractal program to FB

[quote]
BasicCode2 said "Do not understand the math.."

Yes, good attempt. I'll try to describe the original. Basically the drawing of the image starts from several random positions on the left
and right side of the widow, drawing big "arcs" until the centre of the window is reached. Then the process begins all over again. It does not actually stop unless you click the button to restart or exit the program. The different colours look really nice and the images are slightly different each time.
By the way, if you care to look at the original just download Just Basic v1.01. It's a very small sized interpreter for Basic. Navigate to a file named "coolFractal" and open it. Select run from the menu options to see the output.
BasicCoder2
Posts: 3402
Joined: Jan 01, 2009 7:03

### Re: Porting Fractal program to FB

You seem to have trouble using the code and quote blocks. The buttons are just above the area where you type your post.
Insert text or code in between the resulting code blocks.

Downloaded and tried the program as you suggested. Without learning the Just Basic way of doing it I am not sure yet how to translate.
This is a little bit closer. Hit ESC key to end and space key to generate another pattern.

Code: Select all

`const SCRW = 800const SCRH = 480screenres SCRW,SCRH,32dim as integer midx = SCRW\2dim as integer midy = SCRH\2'set up some random starting positionsdim as single  a = rnd(1)dim as single  b = 0.9998dim as single  c = 2-2*adim as integer dots = 12000dim as single  x,y,z,jdim as single  xp,ypdim as ulong   colors(0 to 15)do    cls    'set up some random colors    for i as integer = 0 to 12        colors(i)= rgb(int(rnd(1)*256),int(rnd(1)*256),int(rnd(1)*256))    next i    a = rnd(1)    b = 0.9998    c = 2-2*a    dots = 12000    x = j =0    y = rnd(1)    'calculate and draw the points    for i as integer = 0 to dots        z = x        x = b*y+j        j = a*x+c*(x^2)/(1+x^2)        y = j-z        xp = x*50+midx        yp = y*50+midy        circle (xp,yp),1,colors(i\1000),,,,f    next i    sleep    loop until multikey(&H01)`
MrSwiss
Posts: 3303
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: Porting Fractal program to FB

Anything (at least theoretically) can be ported, to any other language, given the know-how.
(in one of the languages = good know-how, and in the other at least "reading" ability)

One such port is below, a Mandelbrot implementation from Rosetta Code (original in PureBasic)
converted to FreeBASIC:

Code: Select all

`' Mandelbrot.bas -- recoded for FB -- 2017, MrSwiss' original code from: Rosetta Code (PureBasic)Const size = 500, half = 250, Wid = size * 1.5, Hei = sizeScreenRes(Wid, Hei, 32)WindowTitle "Mandelbrot"Dim As Single   x_0, y_0, x_1, y_1, x, y, cDim As UInteger px, py, iFor px = 1 To Wid    x_0 = px / half - 2    For py = 1 To size        y_0 = py / half - 1        x = x_0        y = y_0        i = 0        While (c <= 2 AndAlso i < 100)            x_1 = (x * x) - (y * y) + x_0            y_1 = 2 * x * y + y_0            c = (x * x) + (y * y) * 0.5            x = x_1            y = y_1            i += 1        Wend        If i < 99 Then            PSet (px, py), RGB( (255/25)*i, (255/25)*i, (255/5)*i )        Else             PSet (px, py), &hFF000000        EndIf        c = 0    NextNextSleep`
AndyA
Posts: 11
Joined: Feb 14, 2008 14:56

### Re: Porting Fractal program to FB

I haven't posted anything here in years but, I do know the other Basic language used.

Here's the FB port

Code: Select all

`'Hopalong-Martin Process Like Fractal by ganache'Set screen size heredim as integer sw=1024, sh=768, depth=32screenres sw,sh,depthWindowTitle "Press key for Fractal  -  Press [ESC] to exit"randomize timerdim as integer midx = sw\2dim as integer midy = sh\2dim as integer dots = 75000 '<--- set number of dots to plot heredim as single scale = 40 '<--- make plot bigger or smaller here (on average)dim ans as stringdim as single a,b,c,x,y,z,j,xp,yp'set up some random colorsDim clr(24) as integerFor i as integer = 0 to 24    clr(i) = RGB(Int(rnd(1)*256),Int(rnd(1)*256),Int(rnd(1)*256))Next'ans = ""While ans <> Chr(27)    Cls    'set up some random starting positions    a = rnd(1)    b = 0.9998    c = 2-2*a    x = 0    j = 0    y = rnd(1)*12 + 0.1    'calculate and draw the points    for i as integer = 0 to dots        z = x        x = b*y+j        j = a*x+c*(x^2)/(1+x^2)        y = j-z        xp = x*scale+midx        yp = y*scale+midy        pset (xp,yp),clr((i\1000) Mod 25) 'rgb(255,0,0)    next i    sleep    ans = InkeyWend`
ganache
Posts: 44
Joined: Aug 04, 2016 9:25

### Re: Porting Fractal program to FB

Thank you BasicCoder2, Mr.Swiss and AndyA.
tinram
Posts: 88
Joined: Nov 30, 2006 13:35
Location: UK

### Re: Porting Fractal program to FB

I expect some readers may have already recognized the patterns generated by ganache's and BasicCoder2's programs.

The equation generates a Gumowski-Mira 'fractal',
which was developed at CERN in 1980 to calculate the trajectories of sub-atomic particles.

A long time ago I used FreeBASIC to generate a few patterns: Keith Peters and Tom Beddard went on to do a lot more with Gumowski-Miras
e.g. http://www.artfromcode.com/?s=mira