One of my favorite videos on the whole internet lately is this hidden gem by Robin. In it, he executes a programming challenge he found on Twitter to draw a pattern without using IF statements.
His approach is one of my favorite math tricks of all time, and I was excited to see him use it in practice. I've only used it for you know... math tests.
Here's the video if you've not seen it. Watch it, we'll wait...
Make sure to give him a thumbs up and all that Youtube jazz.
This is one of my favorite code snippets in BASIC. So simple, but so powerful:
Let's port it
I'm on a VIC-20 kick lately, but I thought it would be fun to do this on the Commodore 64.
Robin uses the Super Expander cartridge to do the draw functions. BASIC 2, used on both VIC-20 and Commodore 64, is curiously missing many graphics features. Two popular cartridges emerged to address this. Commodore put out the Super Expander, and a teenager made Simon's BASIC which I've used extensively in the cipher series on this site.
Since Robin uses Super Expander on the VIC-20, it makes sense to start there.
The Commodore 64 version is very similar in syntax and is basically (no pun intended, or was there?) an extension of the VIC-20 version.
First, let's look at the "final" VIC-20 code by Robin
There are really only a few things we need to address to get this to work on the C-64. First is the screen resolution. The VIC-20 has that strange virtual 1024 pixel thing to deal with that we don't need to do on the 64. We can change line 25 to just use the ratio of 90x80 in the 320x200 pixel range. So line 25 now looks like:
25 SX-3.6:SY2.5 REM 320/90 AND 200/80 30 GRAPHIC 2,1
I added the
,1 to line 30 just to clear the screen.
Other than that it works. If we move line 30 down and add a line to change the colors to the VIC-20 colors, the output is almost identical.
25 25 SX-3.6:SY2.5 REM 320/90 AND 200/80 30 POKE 53280,3:POKE 53281,1:COLOR 1,6 35 GRAPHIC 2,1
That's it. Now when we run it we get:
I have a preference for Simon's BASIC. It's nothing big, it's just what I happen to have had in the 80s so it's familiar to me. Robin's code will work fine with a few similar tweaks. The same line 25 change still applies, and the command for the High resolution mode selection is different. The LINE command very similar but is missing the TO in the middle and the 1 goes at the end.
35 HIRES 6,1 40 LINE OX*SX,OY*SY,X*SX,Y*SY,1
That's it. Let's run it:
- Do this challenge on other machines from the 8 Bit Era
- How would you do this in BASIC without the cartridge?