' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2024.09.09.00.00]) on 2024.10.29 at 21:24 (Coordinated Universal Time)

_Title "S Virjee Fractal"
' port and mod by Charlie Veniot'
' port to QBJS by dbox'
' b+ trans from SmallBASIC (to QB64) 2024-10-02  animated zoom, dp, dq added
' Written for Smallbasic Aug 2013 - S Virjee


Screen _NewImage(450, 250, 27)

Dim col As ULong
Dim red, green, blue

Function max& (a&, b&)
    If a& > b& Then max& = a& Else max& = b&
End Function

centerx = xmax / 2
centery = ymax / 2
iter = 20 ' orig 20
zoom = 2.8 '  orig 2.8
p = -.745 ' orig -.745
q = .113 '  orig .113
'Line (0, 0)-(xmax, ymax), _, BF
oldi = 1
pqsq = (p * p + q * q)
L = (Sqr(p * p + q * q) - 1 / iter) * (Sqr(p * p + q * q) - 1 / iter)
H = (Sqr(p * p + q * q) + 1 / iter) * (Sqr(p * p + q * q) + 1 / iter)
dp = .001: dq = -.001

Do
    For x = -centerx To centerx
        For y = 0 To ymax
            c = x / xmax * (1 - zoom * 1.5)
            d = y / ymax * (1 - zoom)
            ztot = 0
            i = 1
            z = 1
            zoom15 = zoom * 1.5
            While i < iter And z < zoom15 ' zoom * 1.5
                real = c * c - d * d + p
                imag = 2 * c * d + q
                c = real / Sgn(d - i)
                d = imag
                z = (c * c + d * d)
                If (z < H) Then
                    If (z > L) And (i > 0) Then
                        ztot = ztot + (1 - (Abs((z - pqsq) / z) / (i / (iter))))
                        oldi = i
                        'if i<25 and z>0 then i=abs((c*d+q))*i
                    End If
                End If
                i = i + 1
            Wend
            If ztot >= 0 Then i = Int(Sqr(ztot) * 256)
            If i < 256 Then red = i: green = 0: blue = 0
            If i > 255 And i <= 512 Then green = i - 256: blue = 0: red = 255
            If i > 511 Then green = 255: blue = 0: red = 255
            If i > 511 And i <= 768 Then blue = i - 512: red = 255: green = 255
            If i > 768 And i <= 1026 Then blue = 255: red = 255: green = 255
            If i > 1026 Then blue = 55: red = 255: green = 55
            gray = Int((red + green + blue) * .33)

            ' hot = if(max(red,green,blue)<255,max(red,green,blue),0)   ' what???
            hot = max(red, max(green, blue))
            If hot > 255 Then hot = -128
            If hot < 0 Then hot = -255
            ' ???

            If oldi = 1 Then red = hot: 'Outer Circle 1 Figure 8
            If oldi = 2 Then green = hot 'Outer Circle 2
            If oldi = 3 Then red = hot: blue = gray 'Inner Circle Figure 8 - Yellow
            If oldi = 4 Then blue = hot: green = red: red = gray 'Inner to 2/Outer Circle 4 Loops top
            If oldi >= 5 Then red = Int((hot + blue + green) * .33) / (oldi): 'This is main color
            'col = -red + (256 * -green) + (256 * 256 * -blue)
            col = _RGB32(red, green, blue)
            PSet (centerx + x, centery - y), col
            PSet (xmax - centerx - x, centery + y), col
        Next y
    Next x
    _Title "S Virjee Fractal" + Str$(zoom) + Space$(2) + Str$(p) + Space$(2) + Str$(q)
    '_Limit 60
    _Display
    zoom = zoom * .999
    If zoom < 1.4 Then zoom = 5
    p = p + dp
    If p < -.8 Then dp = -dp: p = -.8
    If p > -.7 Then dp = -dp: p = -.7
    q = q + dq
    If q > .13 Then dq = -dq: q = .13
    If q < .11 Then dq = -dq: q = .11
Loop