On Friday, May 5, 2017 at 2:44:24 PM UTC-7, Michael J. Mahon wrote:
Nice optimization Michael!
I noticed a few missing pixels at the top rim and bottom brim of the hat, so I kept the first 128 calc as full floating point which fixed the missing pixels:
10 DIM S%(128),T%(384): FOR I = 1 TO 128:I% = I:S%(I) = I% * I%: NEXT
15 FOR I = 1 TO 385:D = I * R3:T%(I - 1) = D * D - 1: NEXT
20 DIM Y(128):Q% = 7807:E = 57:F = .4 * E:D = - 2 * 3.141592654 / 512
25 D3 = 3 * D:ID = 0: FOR I = 0 TO 43:Y = F * SIN (ID)
30 Y(I) = Y:Y(85 - I) = Y:Y(85 + I) = - Y:ID = ID + D3: NEXT
35 ID = 0: FOR I = 0 TO 128:Y% = E * SIN (ID) + Y(I):NY% = 128 - Y%
40 Y% = 128 + Y%: POKE Q% + I,Y%: POKE Q% + 256 - I,Y%
45 POKE Q% + 256 + I,NY%:ID = ID + D: NEXT
50 Z% = 640: FOR I = 0 TO 279: POKE Z% + I,255: NEXT :P% = 16384
55 DIM PB%(57): FOR I = 0 TO 57:PB%(I) = P%:P% = P% + 128: NEXT
60 HGR : POKE 49234,0: HCOLOR= 3: DIM N%(57):N% = 0
64 REM --- Main loop ---
65 FOR J = - E TO E:J% = J:K% = ABS (J%):B% = PB%(K%)
70 IF J% > 0 THEN N% = N%(K%): GOTO 100
74 REM --- Calc one isoline ---
75 U% = S%(K%) * 5:X% = K% * R5:T% = T%(X%)
80 IF U% + S%(N% + 1) < 16246 THEN N% = N% + 1: GOTO 80
85 N%(K%) = N%: FOR I = 0 TO N%:D% = S%(I) + U%
90 IF D% > T% THEN X% = X% + 1:T% = T%(X%): GOTO 90
95 POKE B% + I, PEEK (Q% + X%): NEXT
99 REM --- Draw one isoline ---
100 C% = Z% + 140 - J%:D% = 87 - J% - 128
110 FOR I = 0 TO N%:Y% = D% + PEEK (B% + I)
120 L% = C% - I: IF Y% < PEEK (L%) THEN HPLOT L% - Z%,Y%: POKE L%,Y%
130 R% = C% + I: IF Y% < PEEK (R%) THEN HPLOT R% - Z%,Y%: POKE R%,Y%
140 NEXT : NEXT : PRINT CHR$ (7)
This version improves all the inner loops:
1) Per-isoline sqr replaced with incremental-square calculation
2) Mul 128 in main loop replaced with incremental-add table build
3) Arrays of 16-bit ints replaced with byte-arrays via peek/poke (Zbuffer & point cache)
4) Isoline draw routine does fewer math ops by calculating left & right pos & reusing them
Execution time at 1 MHz with Beagle compiled program is down to 1 min, 1 sec!
Compiled size is 653 bytes vs 950 byte original.
I think Beagle Compiler should have been called Bird's Better Basic.
Using the GSplus emulator at full speed on my Macbook Pro, the compiled Fast Hat finishes in ~500ms (1/2 second).
-JB
@JBrooksBSI