Drawing a Twisted Cylinder on the ZX81
A fun little math program that draws a twisted cylinder.
I wrote this back in August and I’m finally getting around to publishing it. It was first shared in my BASIC group, but the ZX81 lacks a lot of the functionality needed for it to run well. Undeterred, I hacked at the program to not only display the twisted cylinder, but also add a bit of randomness to the demo.
# Needing a line.
The first hurdle to overcome was adding a line-drawing routine. Fortunately, I’ve done this a few times in other programs, so I grabbed the routine from my old drawing program. With that in place, I could replicate the math of the original program and see how it behaved.
I got to work formatting the program. As a teenager I usually didn’t bother, but these days I try to make things a bit more logical. It’s easy enough to use a GOSUB to organize logic. Doesn’t hurt to add a comment or two using REM either. The result is a program that’s a little easier to read.
Cylinder, ZX81 Screenshot #1 by Steven Reid, 2025
# Plotting is so hard.
Although I couldn’t do much about the slowness of the program, I could fix another issue I ran into. The ZX81 plots from the bottom-left corner, which caused the cylinder to appear inverted. Not quite what I planned.
To get around that, I inverted the plots. Fortunately, that only required a bit of math. Instead of LET X1=X1/4.4 I did this: LET X1=64-X1/4.4. Not too difficult, but it looks a lot better. I also mirrored the Y-axis, although I probably didn’t need to.
Cylinder, ZX81 Screenshot #2 by Steven Reid, 2025
This got me to the point where I could test. Although the program worked, it was too big for the screen. To size the cylinder properly, I scaled down the plot points. Thus, the /4.4 in the code above. It fixed the display and kept the cylinder on screen.
# Adding a bit more interest.
Although the program looked decent, it was a bit dull. To spice things up, I added some randomness to the cylinder’s shape. This ensures that each new cylinder looks a bit different, turning the program into something closer to a screen saver.
To make the cylinder more interesting, I added a touch of randomness to a few variables:
1000 REM **SET VARS**
1010 LET D=1.95
1020 LET S=PI/10
1030 RAND 0
1040 LET UR=30+RND*70
1050 LET LR=40+RND*60
The UR and LR determine the twist of the cylinder and make it easier to see on screen.
Cylinder, ZX81 Screenshot #3 by Steven Reid, 2025
And with that, I wrap up this month's program.
---
Want to try it out? You can run the program, or view the code if you’d like to see how it works.