miriam_e: from my drawing MoonGirl (Default)
miriam_e ([personal profile] miriam_e) wrote2026-04-26 11:57 pm

Another cute little graphics program

Recently I stayed up all night fiddling with a very cool little program that was explained in the Computer Recreations column of September 1986 issue of Scientific American.

My program was written in my current favorite computer language, awk, and used my graphics server which lets any computer language do graphics, even awk, which has no graphics commands.
#! /bin/awk -f

# hopalong
#
# by Miriam
# Friday 2026-04-24 01:36:14 am
# adapted from my CoCo program
# which was adapted from
# Scientific American, Computer Recreations (Sept '86)

# awk doesn't have abs() or sign() functions, but they're easy to add
function abs(num) {return num < 0 ? -num : num;}
function sign(num) {return num==0 ? 0 : num<0 ? -1 : +1;}

BEGIN{
    # Read canvas size
    "echo \"size\" | candraw" | getline
    wide = $1
    high = $2

    # clear the canvas
    print "clear"

    xv=yv=0
    loops=200000000
    a=-1000 ; b=0.1 ; c=-10
    reduction=0.05 ; soffset=-500

    for (n=1 ; n<loops ; n++) {
        x=(xv/reduction)+soffset
        y=(yv/reduction)+soffset
        if (x>0 && x<wide && y>0 && y<high) print "pset " x, y
        xx=yv-sign(xv)*(abs(b*xv-c))^0.5
        yy=a-xv
        xv=xx
        yv=yy
    }
}
The program is meant to be invoked as:
hopalong.awk | candraw
It pipes the print statements out to my graphics server through candraw, which acts as a bridge between the awk program and the server.

Here is the "hopalong.bas" program that I wrote in BASIC way back in 1986 for my Tandy Color Computer (CoCo):
1 ' adapted from Computer Recreations
2 ' Scientific American Sept. '86
10 XV=0:YV=0
20 LOOPS=600000
30 A=-1000:B=.1:C=-10
32 PMODE4,1:COLOR0,1:PCLS
34 SCREEN1,1
35 LINE(0,0)-(255,191),PSET,B
36 REDUCTION=.2:SOFFSET=128
40 FORL=1TO LOOPS
42 X=(XV/RED)+SOFFSET
44 Y=(YV/RED)+SOFFSET
46 IF X>255 OR X<0 OR Y>191 OR Y<0 THEN 60
50 PSET(X,Y)
60 XX=YV-SGN(XV)*(ABS(B*XV-C))^.5
70 YY=A-XV
80 XV=XX
90 YV=YY
100 NEXT
110 GOTO110

The awk program is so much clearer and easier to read. It amazes me that I learned anything in that dense BASIC language, but it is actually surprisingly capable. I still occasionally use a CoCo emulator and a TRS-80 emulator on my computer. Sometimes it is just easier.

It is funny to think that awk is so much more modern... it was initially released in 1977 -- the same year as the first Tandy TRS-80.