Has anyone tried creating a fractal application using BCX?

Started by djsb, April 21, 2024, 06:46:37 AM

Previous topic - Next topic

djsb

Just wondering if anyone has ever created a fractal application (i.e. that shows a graphical representation of fractal mathematics). I wondered about this whilst reading about Sierpinki triangles

https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle

and how they have just been discovered for the first time in a virus. I read many years ago about Julia sets and their like in the book by James Gleick and thought it would be fun to use BCX to demonstrate them graphically or using ASCII characters.
Unfortunately, I don't know enough about either BCX or Maths to try this myself, or I would give it a try. Just an idea.

MrBcx

The following is from the GUI_DEMOs collection. 
It simply displays the Mandelbrot fractal, cycling through 3 colors.  It is a tiny example.
One way fractals have been explained is that they are functions that take their output and use that as their input.
Which implies fractals are recursive.

There are some stunning 3-D fly-through videos of fractals on youtube like this one:
https://youtu.be/S530Vwa33G0?t=80



GUI "mandelbrot"

CONST Red    = RGB(10, 0, 0)
CONST Green  = RGB(0, 10, 0)
CONST Blue   = RGB(0, 0, 10)

GLOBAL Form  AS CONTROL
GLOBAL Kolor AS INTEGER


SUB FORMLOAD
    Kolor = Blue
    Form  = BCX_FORM ("Mandelbrot  ~ Made with BCX ~")
    BCX_SET_FORM_COLOR (Form, 0)
    CENTER (Form)
    SHOW (Form)
END SUB


BEGIN EVENTS
    SELECT CASE CBMSG
        '*******************
        CASE WM_CREATE
        '*******************
  IF NOT SetTimer(hWnd, 1, 3000, 0)  THEN
            MessageBox (hWnd, "Timer Error", "Error", MB_OK)
            PostQuitMessage (0)
        END IF
        '*******************
        CASE WM_PAINT
        '*******************
        DIM RAW ps  AS PAINTSTRUCT
        DIM RAW hdc AS HDC
        hdc = BeginPaint (hWnd, &ps)
        DrawMandelbrot(hdc)
        DeleteDC (hdc)
        EndPaint (hWnd, &ps)
        '*******************
        CASE WM_TIMER
        '*******************
        SELECT CASE Kolor
            CASE Red
            Kolor = Green
            CASE Green
            Kolor = Blue
            CASE Blue
            Kolor = Red
            CASE ELSE
            Kolor = Blue
        END SELECT
        InvalidateRect(Form, 0, 1)
        '*******************
    END SELECT
END EVENTS




SUB DrawMandelbrot (hdc AS HDC)
    DIM RAW Count AS INTEGER
    DIM RAW A AS SINGLE, B AS SINGLE, C AS SINGLE
    DIM RAW I AS SINGLE, R AS SINGLE
    FOR I = -1.3 TO 1.3 STEP .01
        DOEVENTS
        FOR R = -2.2 TO 1 STEP .01
            A = B = C = Count = 0
            WHILE ABS(A)<=2 AND ABS(B)<=2 AND Count<128
                C = A*A-B*B+R
                B = 2*A*B+I
                A = C
                INCR Count
            WEND
            SetPixelV (hdc, 50+(230+R*100), 140+I*100, Count*Kolor)
        NEXT
    NEXT
    UpdateWindow(Form)
END SUB


Robert



'********************************************************************
'    Julia Set demonstration                 License - PUBLIC DOMAIN
'    Converted and modified from GW-BASIC by MrBcx    March 11, 2020
'********************************************************************
GUI "Julia Set",PIXELS

SUB FORMLOAD
   GLOBAL Form1 AS HWND
   Form1 = BCX_FORM ("Julia", 0, 0,640, 410)
   MODSTYLE(Form1, 0, WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE)
   BCX_SET_FORM_COLOR (Form1, QBCOLOR(0))
   CENTER Form1
   SHOW   Form1
   CALL   Drawit
END SUB

BEGIN EVENTS
END EVENTS


SUB Drawit
   DIM AS DOUBLE SCALE, ZEROX, ZEROY, MAXIT, CR, CI, ZR, ZI, BR
   DIM AS INT X, Y, I
   SCALE  = 1.0/81
   ZEROX  = 160
   ZEROY  = 100
   MAXIT  =  32
   CR#    = -0.798
   CI#    =  0.1618
   FOR X = 0 TO 2 * ZEROX - 1
      FOR Y = 0 TO 2 * ZEROY - 1
         ZR  = (X - ZEROX) * SCALE
         ZI  = (ZEROY - Y) * SCALE
         FOR I = 1 TO MAXIT
            BR = CR + ZR *ZR - ZI * ZI
            ZI = CI + 2 * ZR *ZI
            ZR = BR
            IF ZR * ZR + ZI * ZI > 4  THEN GOTO L160
         NEXT I
         GOTO L170
         L160:
         BCX_PSET (Form1, X*2+1, Y*2+1, QBCOLOR (5 + MOD(I,3)))
         BCX_PSET (Form1, X*2-1, Y*2-1, QBCOLOR (4 + MOD(I,3)))
         BCX_PSET (Form1, X*2,   Y*2,   QBCOLOR (3 + MOD(I,3)))
         L170:
      NEXT Y
   NEXT X
END SUB



$COMMENT
'************************************************
' Original GW-BASIC listing from RosettaCode.org
'          Provided for reference only
' https://rosettacode.org/wiki/Julia_set#GW-BASIC
'************************************************
10 SCALE# = 1/81 : ZEROX = 160
20 ZEROY = 100 : MAXIT = 32
30 CR# = -.798 : CI# = .1618
40 SCREEN 1
50 FOR X = 0 TO 2*ZEROX - 1
60 FOR Y = 0 TO 2*ZEROY - 1
70 ZR# = (X-ZEROX)*SCALE#
80 ZI# = (ZEROY-Y)*SCALE#
90 FOR I = 1 TO MAXIT
100 BR# = CR# + ZR#*ZR# - ZI#*ZI#
110 ZI# = CI# + 2*ZR#*ZI#
120 ZR# = BR#
130 IF ZR#*ZR# + ZI#*ZI# > 4! THEN GOTO 160
140 NEXT I
150 GOTO 170
160 PSET (X, Y), 1 + (I MOD 3)
170 NEXT Y
180 NEXT X
$COMMENT


djsb

Thanks for the above. Before I ask co-pilot AI how would a sierpinski triangle be created? I suppose that I could study the code in great detail, but I'm scared of maths and still unfamiliar with BCX (and just plain bone idle I suppose). Here is some C code from the video linked to below


#include <stdio.h>

/* size = 4
             *
* *
   *   *
  * * * *
 
*/

*/ size = 8
             *
    * *
   *   *
  *     *
*       *
*         *
   *           *
  * * * * * * * *
 
/* Algorithm
   Step 1: Each row shall be filled with spaces equal to current row value
   Step 2: When row equal to column value, initialise x = 0
   Step 3: If ( x & row ) is true, print " ". Otherwise print"* "
   Step 4: Increment x value and repeat step 3 until (x + row ) < size
   
int main()
{
    int row,i,x;
int size = 4;

for(row = size - 1; row >=0; row--)
   {
   for(i = 0;i < row;i++)
      {
  printf(" ");
  }
   for(x = 0; (x+row) < size; x++)
      {
  printf((x&row)? "  ": "* ");
  }
   printf("\n");
   
   
   }


return 0;

}


How would this be implemented in BCX in GUI format? Thanks.





PS Here is a video showing how to do this using a basic star pattern in the console using C
https://www.youtube.com/watch?v=Uzse_HpUwM4
I might put the algorithm and code here later. See above.