0

Koch Snowflake in Applesoft Basic

 1 year ago
source link: https://smist08.wordpress.com/2022/09/17/koch-snowflake-in-applesoft-basic/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction

After learning to program in high school in BASIC on a PDP-11, I went to University where first year computer science was taught using WATFIV (Waterloo Fortran IV), where we had to write our programs by hand, type them into an IBM card punch machine and then submit our stack of punch cards to the data center to run. Later we would receive back our punch cards along with any printout from the program.

ibmpunch.jpg?w=656

Definitely a step backwards from the interactive programming on the PDP-11. I joined the co-op program and after my first work term, used much of the money I made to buy an Apple II+ computer. This returned me to BASIC programming with Applesoft BASIC, which was created by Microsoft for Apple to replace their original Integer Basic.

Last time I produced a Koch Snowflake in QB64, a version of BASIC more advanced than the early BASICs like on the PDP-11 or Apple II. QB64 had functions, subroutines, structured programming and line numbers were optional. I wondered about producing the same program on a more original version of BASIC.  Applesoft is a good candidate as it uses line numbers, has no structured programming, but it does have a handful of builtin graphics commands that allow us to write our Koch Snowflake program. The main challenge is what to do about the recursive function calls.

220px-apple_ii_plus.jpg?w=220

Apple II Emulators

Although I sold my Apple II+ many years ago, to buy an IBM AT clone, it turns out many people have put a lot of effort into writing Apple II emulators and that you can find an Apple II emulator on most systems, In this case I used the AppleWin emulator, which was easy to install and up and running in no time. Below is the Koch Snowflake on the Apple screen. This is in the Apple II’s high resolution graphics mode of 280×192 in 6 colors. Rather low res by today’s standards.

master_000000000.png?w=560

Applesoft BASIC programs aren’t stored on the disk as regular text files, they are stored in a tokenized format which helps the interpreter run them faster. This is a problem as you can’t easily transfer the files back and forth to a PC. Since the program is small, I just typed it into the Apple, but it means to show the listing you get the following screen shots rather than text in the article.

The big innovation of Applesoft BASIC is that variables are floating point by default rather than integer, so this works well for the couple of calculations we need to do. The other thing is that variable names can be longer, but only the first two characters are used for uniqueness, hence AB = ABC = ABCD. The program roughly follows the same logic as the program in the last article, except no functions and recursion is handled by a simple stack stored in a couple of arrays.

The Program

The first part of the program initializes the variables, defines the arrays that will be used to stack level and line length and sets the graphics mode and color (white = 3).

master_000000005.png?w=560

The next part corresponds to the KochSnowflake subroutine in the last article that draws the main three sides of the snowflake.

master_000000006.png?w=559

Lines 200 to 330 correspond to the SnowflakeSide subroutine, note the lack of structured programming forces us to have four IF statements.

master_000000007.png?w=560

Lines 1000 to 1060 are the turtle graphics routine. We don’t have a turn routine, as the angle is a global variable. Move is similar to the Move Subroutine in the last article.

master_000000008.png?w=560

All in all, still quite a simple program. Editing programs in Applesoft is quite old school, but the memory of how to do things returns fairly quickly. Still people who’ve only used graphics editors might find it baffling.

Summary

It’s great that people have put so much effort into creating such high quality emulators of historical computer systems. This was a nice journey back down memory lane of how I did most of my programming in University from second year onwards. The Apple emulators support Apple’s Pascal system and even the add-on Z80 CPM cards. Perhaps I wouldn’t want to produce modern commercial software this way, but I think there are lessons to be learned from learning some of the old ways.

Loading...

Related

Back to BasicsSeptember 4, 2022In "programming"

Wio Terminal First ImpressionsAugust 30, 2021In "Learning"

Drawing in Code by ProcessingApril 11, 2020In "Learning"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK