Porting Fractal program to FB

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

Porting Fractal program to FB

Post by ganache »

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: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Porting Fractal program to FB

Post by BasicCoder2 »

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

Code: Select all

screenres 640,480,32
dim as integer midx = 320
dim as integer midy = 240

'set up some random starting positions
dim as single  a = rnd(1)
dim as single  b = 0.9998
dim as single  c = 2-2*a
dim as integer dots = 12000
dim as single  x,y,z,j
dim as single  xp,yp

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*20+midx
    yp = y*20+midy
    pset (xp,yp),rgb(255,0,0)
next i

sleep
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: Porting Fractal program to FB

Post by ganache »

[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: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Porting Fractal program to FB

Post by BasicCoder2 »

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 = 800
const SCRH = 480
screenres SCRW,SCRH,32
dim as integer midx = SCRW\2
dim as integer midy = SCRH\2

'set up some random starting positions
dim as single  a = rnd(1)
dim as single  b = 0.9998
dim as single  c = 2-2*a
dim as integer dots = 12000
dim as single  x,y,z,j
dim as single  xp,yp
dim 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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Porting Fractal program to FB

Post by MrSwiss »

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 = size

ScreenRes(Wid, Hei, 32)
WindowTitle "Mandelbrot"

Dim As Single   x_0, y_0, x_1, y_1, x, y, c
Dim As UInteger px, py, i

For 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
    Next
Next

Sleep
AndyA
Posts: 11
Joined: Feb 14, 2008 14:56

Re: Porting Fractal program to FB

Post by AndyA »

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 here
dim as integer sw=1024, sh=768, depth=32
screenres sw,sh,depth

WindowTitle "Press key for Fractal  -  Press [ESC] to exit"
randomize timer
dim as integer midx = sw\2
dim as integer midy = sh\2
dim as integer dots = 75000 '<--- set number of dots to plot here
dim as single scale = 40 '<--- make plot bigger or smaller here (on average)
dim ans as string
dim as single a,b,c,x,y,z,j,xp,yp


'set up some random colors
Dim clr(24) as integer
For 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 = Inkey
Wend
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: Porting Fractal program to FB

Post by ganache »

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

Re: Porting Fractal program to FB

Post by tinram »

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:

Image

Keith Peters and Tom Beddard went on to do a lot more with Gumowski-Miras
e.g. http://www.artfromcode.com/?s=mira
Post Reply