Author Topic: Lorenz system in color  (Read 4203 times)

0 Members and 1 Guest are viewing this topic.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Lorenz system in color
« on: January 13, 2022, 03:31:11 am »
Lorenz system ( https://en.wikipedia.org/wiki/Lorenz_system ) flattened out to color domain.  Each pixel (x,y) becomes a particle (x,y,z) with random z and we colour it based on how long it takes to get to the center of the 'butterfly' using a poor Euler's method.  This image can be zoomed and explored

Code: [Select]
defdbl a-z

const sw = 640
const sh = 480

dim shared mx,my,mbl,mbr,mw

dim u as double, v as double
dim uu as double, vv as double
dim xx as double, yy as double
dim x0 as double, y0 as double
dim z as double, zz as double
z = 1
zz = 0.1

dim p1 as long
p1 = _newimage(sw, sh, 32)
screen _newimage(sw, sh, 32)

redraw = -1
iter = 500

dim c(100) as long, cc as long

for i=0 to 100
        if i < iter/6 then
                r = 155
                g = (i mod (iter/6))*(255/(iter/6))
                b = 0
        elseif i < 2*iter/6 then
                r = 155 - (i mod (iter/6))*(255/(iter/6))
                g = 155
                b = 0
        elseif i < 3*iter/6 then
                r = 0
                g = 155
                b = (i mod (iter/6))*(255/(iter/6))
        elseif i < 4*iter/6 then
                r = 0
                g = 155 - (i mod (iter/6))*(255/(iter/6))
                b = 155
        elseif i < 5*iter/6 then
                r = (i mod (iter/6))*(255/(iter/6))
                g = 0
                b = 155
        else
                r = 155
                g = 0
                b = 155 - (i mod (iter/6))*(255/(iter/6))
        end if
        c(i) = _rgb(r, g, b)
next

ppp=28
sss=10
bbb=8/3
zzz=0.456
hh=0.01

do
        mw = 0
        getmouse

        if redraw then
                for yy = 0 to sh-1
                for xx = 0 to sw-1
                        u = 0
                        v = 0

                        x = (xx - sw/2)*z + x0
                        y = (yy - sh/2)*z + y0
z2 = zzz

                        for i = 0 to iter
x = x + hh*sss*(y - x)
y = y + hh*(x*(ppp - z2) - y)
z2=z2 + hh*(x*y - bbb*z2)

                                if (x*x + y*y) < 2 then exit for
                        next

                        if i > iter then
                                pset(xx, yy), _rgb(0,0,0)
                        else
                                pset(xx, yy), c(i mod 100)
                        end if
                next
                next

                'locate 1,1
                'print "iter =";iter
                _title str$(iter)

                _dest p1
                _putimage , 0
                _dest 0

                _putimage , p1
                _autodisplay

                redraw = 0
        end if

        if mw < 0 then
                zz = zz + 0.01
        elseif mw > 0 then
                if zz > 0.01 then zz = zz - 0.01
        end if

        'draw box
        if omx <> mx or omy <> my or mw <> 0 then
                _putimage , p1
                line (mx - (sw*zz/2), my - (sh*zz/2))-step(sw*zz,sh*zz),_rgb(255,255,255),b
                _autodisplay

                omx = mx
                omy = my
        end if

        if mbl then
                do
                        getmouse
                loop while mbl

                x0 = x0 + (mx - sw/2)*z
                y0 = y0 - (sh/2 - my)*z
                z = z*zz

                iter = iter + 50

                redraw = -1
        elseif mbr then
                do
                        getMouse
                loop while mbr

                x0 = x0 + (mx - sw/2)*z
                y0 = y0 - (sh/2 - my)*z
                z = z/zz
                redraw = -1
        end if

        k = _keyhit
        if k = 43 then
                iter = iter + 50
                redraw = -1
        elseif k = 45 then
                if iter > 50 then iter = iter - 50
                redraw = -1
        end if

loop until k = 27
system

sub getmouse ()
        do
                mx = _mousex
                my = _mousey
                mbl = _mousebutton(1)
                mbr = _mousebutton(2)
                mw = mw + _mousewheel
        loop while _mouseinput
end sub

'u + iv = (x + iy) ^ (a + ib)
sub cexp (u as double, v as double, x as double, y as double, a as double, b as double)
        dim mag as double, arg as double
        dim lnz as double, argz as double

        lnz = 0.5*log((x*x + y*y)+0.00001)
        argz = _atan2(y, x)

        mag = exp(a*lnz - b*argz)
        arg = a*argz + b*lnz

        u = mag * cos(arg)
        v = mag * sin(arg)
end sub


 
lzfrac.PNG
« Last Edit: January 13, 2022, 05:49:07 am by _vince »

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Lorenz system in color
« Reply #1 on: January 14, 2022, 03:11:17 am »
for the sake of completion, here's the Lorenz attractor plotted in 2D, and 3D with rotation

Code: [Select]
const sw = 640
const sh = 480

dim shared pi
pi = 4*atn(1)

rr = 500
h = 1200

screen _newimage(sw, sh, 32)

p = 28
s = 10
b = 8/3
h = 0.01


x = 0.3
y = 0.3
z = 0.456

pset (sw/2 + 10*x, sh - 10*z),_rgba(255,255,0,100)

for i=0 to 14000
x = x + h*s*(y - x)
y = y + h*(x*(p - z) - y)
z = z + h*(x*y - b*z)

line -(sw/2 + 10*x, sh - 10*z),_rgba(255,255,0,100)
next

do
loop until _keyhit = 27

rot = 0

do
rot = rot + 0.01

line (0,0)-(sw,sh),_rgb(0,0,0),bf

x = 0.3
y = 0.3
z = 0.456

xx = x*cos(rot) - y*sin(rot)
yy = x*sin(rot) + y*cos(rot)

pset (sw/2 + 35*xx*700/(yy + 2500), sh - 35*z*700/(yy + 2500)),_rgba(255,255,0,100)

for i=0 to 14000
x = x + h*s*(y - x)
y = y + h*(x*(p - z) - y)
z = z + h*(x*y - b*z)

xx = x*cos(rot) - y*sin(rot)
yy = x*sin(rot) + y*cos(rot)

line -(sw/2 + 35*xx*700/(yy + 2500), sh - 35*z*700/(yy + 2500)),_rgba(255,255,0,100)
next


_display
_limit 60
loop until _keyhit=27

rot = 0

do
rot = rot + 0.01

line (0,0)-(sw,sh),_rgb(0,0,0),bf

x = 0.3
y = 0.3
z = 0.456

yy = y*cos(rot) - (z - 35)*sin(rot)
zz = y*sin(rot) + (z - 35)*cos(rot)

pset (sw/2 + 35*x*700/(yy + 2500), sh/2 - 35*zz*700/(yy + 2500)),_rgba(255,255,0,100)

for i=0 to 14000
x = x + h*s*(y - x)
y = y + h*(x*(p - z) - y)
z = z + h*(x*y - b*z)

yy = y*cos(rot) - (z - 35)*sin(rot)
zz = y*sin(rot) + (z - 35)*cos(rot)

line -(sw/2 + 35*x*700/(yy + 2500), sh/2 - 35*zz*700/(yy + 2500)),_rgba(255,255,0,100)
next


_display
_limit 60
loop until _keyhit=27

sleep
system

 
lzattractor.PNG

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lorenz system in color
« Reply #2 on: January 14, 2022, 06:31:57 am »
Here is 2 views of live particle as it traces out it's path at various random settings:
Code: QB64: [Select]
  1. _Title "Lorenz Attractor Colorized" 'b+ 2020-04-08
  2. ' https://www.algosome.com/articles/lorenz-attractor-programming-code.html
  3. Const xmax = 1280, ymax = 640
  4. DefDbl A-Z
  5. Dim Shared cN, pR, pG, pB 'plasma
  6. Screen _NewImage(xmax, ymax, 32)
  7. a = 100: b = 300: c = 1
  8. restart:
  9. resetPlasma
  10. Color &HFFBBBBBB
  11. Print "        a ="; a; "        b ="; b; "        c ="; c
  12. x = .1: y = 0: z = 0
  13. While t < .002
  14.     xt = x + t * (a * (y - x))
  15.     yt = y + t * (x * (b - z) - y)
  16.     zt = z + t * (x * y - c * z)
  17.     If y >= -320 And y <= 320 Then
  18.         If x >= -320 And x <= 320 Then Circle (x + 320, y + 320), 1, plasma
  19.         If z >= -320 And z <= 640 Then Circle (z + 640, y + 320), 1, plasma
  20.     End If
  21.     x = xt
  22.     y = yt
  23.     z = zt
  24.     t = t + .0000001
  25.     cN = cN + .005
  26.     Line (0, 0)-(_Width, _Height), &H02000000, BF '<<< focus on particle or not? update 2022-01-14
  27.     _Limit 500
  28.  
  29. a = Rnd * 200: b = Rnd * 300 + a: c = Rnd * 30: t = 0
  30. If c > a / 3 Then c = a / 3 * Rnd
  31. GoTo restart
  32.  
  33. Function plasma~&
  34.     plasma~& = _RGB32(127 + 127 * Sin(pR * cN), 127 + 127 * Sin(pG * cN), 127 + 127 * Sin(pB * cN))
  35.  
  36. Sub resetPlasma ()
  37.     pR = Rnd ^ 2: pG = Rnd ^ 2: pB = Rnd ^ 2: cN = 0
  38.  
  39.  
  40.