Skip to content

Instantly share code, notes, and snippets.

@eparadis
Last active January 12, 2022 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eparadis/d4a242c8cc149f3583c303efa80532c4 to your computer and use it in GitHub Desktop.
Save eparadis/d4a242c8cc149f3583c303efa80532c4 to your computer and use it in GitHub Desktop.
ascii art mandelbrot drawing program in BASIC
10 x1=59
11 y1=21
20 i1=-1.0
21 i2=1.0
22 r1=-2.0
23 r2=1.0
30 s1=(r2-r1)/x1
31 s2=(i2-i1)/y1
40 for y=0 to y1
50 i3=i1+s2*y
60 for x=0 to x1
70 r3=r1+s1*x
71 z1=r3
72 z2=i3
80 for n=0 to 29
90 a=z1*z1
91 b=z2*z2
100 if a+b>4.0 goto 130
110 z2=2*z1*z2+i3
111 z1=a-b+r3
120 next n
130 print chr$(62-n);
140 next x
150 print
160 next y
170 end
@eparadis
Copy link
Author

another similar listing is here https://www.retrobrewcomputers.org/forum/index.php?t=msg&th=201&goto=4704&#msg_4704

10 FOR Y=-12 TO 12
20 FOR X=-39 TO 39
30 CA=X*0.0458
40 CB= Y*0.08333
50 A=CA
60 B=CB
70 FOR I=0 TO 15
80 T=A*A-B*B+CA
90 B=2*A*B+CB
100 A=T
110 IF (A*A+B*B)>4 THEN GOTO 200
120 NEXT I
130 PRINT " ";
140 GOTO 210
200 IF I>9 THEN I=I+7
205 PRINT CHR$(48+I);
210 NEXT X
220 PRINT
230 NEXT Y
Ok
run

000000011111111111111111122222233347E7AB322222111100000000000000000000000000000
000001111111111111111122222222333557BF75433222211111000000000000000000000000000
000111111111111111112222222233445C      643332222111110000000000000000000000000
011111111111111111222222233444556C      654433332211111100000000000000000000000
11111111111111112222233346 D978 BCF    DF9 6556F4221111110000000000000000000000
111111111111122223333334469                 D   6322111111000000000000000000000
1111111111222333333334457DB                    85332111111100000000000000000000
11111122234B744444455556A                      96532211111110000000000000000000
122222233347BAA7AB776679                         A32211111110000000000000000000
2222233334567        9A                         A532221111111000000000000000000
222333346679                                    9432221111111000000000000000000
234445568  F                                   B5432221111111000000000000000000
                                              864332221111111000000000000000000
234445568  F                                   B5432221111111000000000000000000
222333346679                                    9432221111111000000000000000000
2222233334567        9A                         A532221111111000000000000000000
122222233347BAA7AB776679                         A32211111110000000000000000000
11111122234B744444455556A                      96532211111110000000000000000000
1111111111222333333334457DB                    85332111111100000000000000000000
111111111111122223333334469                 D   6322111111000000000000000000000
11111111111111112222233346 D978 BCF    DF9 6556F4221111110000000000000000000000
011111111111111111222222233444556C      654433332211111100000000000000000000000
000111111111111111112222222233445C      643332222111110000000000000000000000000
000001111111111111111122222222333557BF75433222211111000000000000000000000000000
000000011111111111111111122222233347E7AB322222111100000000000000000000000000000

@eparadis
Copy link
Author

Add some ANSI color to your drawing, if you terminal supports it, with these changes to the original listing:
130 PRINT CHR$(27);CHR$(91);CHR$(51);CHR$(N-INT(N/6)*6+49);CHR$(109);
131 PRINT CHR$(62-N);

@eparadis
Copy link
Author

With ANSI color modification:
image

@eparadis
Copy link
Author

If you want that cool "decoding the matrix" look, this version cycles through each hex character as it prints. It's way slower, but you can use that time to admire the cool effect.

10 FOR Y=-12 TO 12
20 FOR X=-19 TO 19
30 CA=X*0.0458
40 CB= Y*0.08333
50 A=CA
60 B=CB
70 FOR I=0 TO 15
75 GOSUB 1000
80 T=A*A-B*B+CA
90 B=2*A*B+CB
100 A=T
110 IF (A*A+B*B)>4 THEN GOTO 210
115 PRINT CHR$(8);
120 NEXT I
130 PRINT " ";
140 GOTO 210
210 NEXT X
220 PRINT
230 NEXT Y
900 END
1000 REM print a single hex digit stored in I
1010 IF I>9 THEN J=I+7
1020 IF I<=9 THEN J=I
1030 PRINT CHR$(48+J);
1040 RETURN

@eparadis
Copy link
Author

eparadis commented Jan 12, 2022

A very naive re-implementation of the original version from BASIC into FORTH is:

variable x1
variable y1

variable i1
variable i2
variable i3

variable r1
variable r2
variable r3

variable s1
variable s2

variable z1
variable z2

variable a
variable b

variable n

59 x1 !
21 y1 !
-1e0 i1 f!
1e0 i2 f!
-2e0 r1 f!
1e0 r2 f!
r2 f@ r1 f@ f- x1 @ s>f f/ s1 f! \ L30
i2 f@ i1 f@ f- y1 @ s>f f/ s2 f! \ L31

: mandel
y1 @ 0 do                         \ L40
  i1 f@ s2 f@ i s>f f* f+ i3 f!   \ L50
  x1 @ 0 do                       \ L60
    r1 f@ s1 f@ i s>f f* f+ r3 f! \ L70
    r3 f@ z1 f!                   \ L71
    i3 f@ z2 f!                   \ L72
    0 n !
    30 0 do              \ L80
      z1 f@ fdup f* a f! \ L90
      z2 f@ fdup f* b f! \ L91
      a f@ b f@ f+ 4e0 f> if leave then  \ line 100
      2e0 z1 f@ z2 f@ f* f* i3 f@ f+ z2 f! \ L110
      a f@ b f@ f- r3 f@ f+ z1 f! \ L111
      n @ 1 + n !
    loop \ L120
    62 n @ - emit \ L130
  loop \ L140
  cr \ L150
loop \ L160
;

mandel

bye

Note that I had to use 30 instead of 29 in "line 80" aka 30 0 do to produce the proper output.

>>>>>>=====<<<<<<<<<<<<<<<;;;;;;:::96032:;;;;<<<<==========
>>>>>===<<<<<<<<<<<<<<<<;;;;;;;:::873*079::;;;;<<<<<=======
>>>>===<<<<<<<<<<<<<<<;;;;;;;::9974    (.9::::;;<<<<<======
>>>==<<<<<<<<<<<<<<<;;;;;;:98888764     5789999:;;<<<<<====
>>==<<<<<<<<<<<<<;;;;::::996. &2           45335:;<<<<<<===
>>=<<<<<<<<<<<;;;::::::999752                 *79:;<<<<<<==
>=<<<<<<<<;;;:599999999886                    %78:;;<<<<<<=
><<<<;;;;;:::972456-567763                      +9;;<<<<<<<
><;;;;;;::::9875&      .3                       *9;;;<<<<<<
>;;;;;;::997564'        '                       8:;;;<<<<<<
>::988897735/                                 &89:;;;<<<<<<
>::988897735/                                 &89:;;;<<<<<<
>;;;;;;::997564'        '                       8:;;;<<<<<<
><;;;;;;::::9875&      .3                       *9;;;<<<<<<
><<<<;;;;;:::972456-567763                      +9;;<<<<<<<
>=<<<<<<<<;;;:599999999886                    %78:;;<<<<<<=
>>=<<<<<<<<<<<;;;::::::999752                 *79:;<<<<<<==
>>==<<<<<<<<<<<<<;;;;::::996. &2           45335:;<<<<<<===
>>>==<<<<<<<<<<<<<<<;;;;;;:98888764     5789999:;;<<<<<====
>>>>===<<<<<<<<<<<<<<<;;;;;;;::9974    (.9::::;;<<<<<======
>>>>>===<<<<<<<<<<<<<<<<;;;;;;;:::873*079::;;;;<<<<<=======

Here's another gist for the refactor of this into something that's more idiomatic FORTH. I'm interested to find a FORTH for my Z80 retrocomputer to compare speed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment