Plotting a 3D function with Simons’ BASIC and BASIC 2.0 (C64)

Some time ago I noticed on Youtube a nice Sinclair Spectrum BASIC program that plots a 3D function. Since I like very much the way the function looks, I decided to do a C64 porting.

The Sinclair BASIC code is as follows:

 

The program is quite slow and requires warp mode on emulators to run at a decent speed. Otherwise, on a real machine or without warp mode it takes some minutes. This is more than expected, as it performs several multiplications and evaluates an exponentiation function too.

The program uses an array (M) to handle hidden lines. Line 70 plots a point only if it doesn’t belong to a hidden line. That provides depth to the plot.

The easiest way to port such a program on the Commodore 64 is to use Simons’ BASIC. The code looks quite similar to the Sinclair BASIC’s one, but with some little modifications.

 

 

Download: 3D function (Simons’ BASIC)

 

On the Spectrum, you don’t need to set up a bitmap mode, as the machine is always in bitmap mode (it lacks a hardware text mode). On the contrary, the Commodore 64 provides a hardware text mode (which is available on start-up), so in order to plot individual points we first need to set up a bitmap screen. This is simply done in line 5 of the Simons’ BASIC program. Color 13 (light green) is used for plotted points, color 0 (black) is used for the background (points set to 0). The HIRES command takes care of clearing the bitmap screen as well.

On line 70, the PLOT command provides a fast means of plotting points on the hi-res screen.

The bitmap screen origin is different between the two machines. On the Spectrum, it is the bottom left-hand corner. On the Commodore 64, it is the top left-hand corner. This difference is only dealing with the BASIC plot routines. In machine language, it only depends on how plotting routines are coded. By the way, points are mapped on memory on a different way on these machines, but  that’s another story.

So, due to this difference on BASIC plot routines, on the Commodore 64 version we need to mirror the Y coordinate before plotting. This is accomplished by the expression 199-Y1 in line 70 (as any Y coordinate on a Commodore 64 bitmap screen can range from 0 to 199). Otherwise, the function would appear upside-down.

Another difference between the two BASIC versions is that on Commodore BASIC LET statements are not mandatory. So, we can omit the LET instruction on assignments. This is not the case on Sinclair BASIC, as it needs the LET instruction on each assignment.

Furthermore, on Commodore BASIC we do not need separate NEXT statements for each counter, so we may just code NEXT X,Y on line 80.

 

A 3D function on the Commodore 64 (Simons’ BASIC).

 

If you don’t have Simons’ BASIC or you don’t want to LOAD it each time to plot this function, I have coded a BASIC 2.0 version as well.

My usual trick for fast clearing of the bitmap screen has been used. I also set-up a little multiply table to speed up plotting. The result is the following program.

 

Download: 3D function, BASIC  2.0

 

A 3D function on the Commodore 64 (BASIC 2.0).

 

The program is a bit slower than the Simons’ BASIC one but there’s no great difference, especially in warp-mode. All programs can be compacted for better performances, but I don’t think it would make much difference on this case.

Previous Entries Very basic BASIC: formatting numbers and updating information on screen Next Entries Game review: Hunchback (Commodore 64)

Leave a Reply

*