Showing posts with label graphics. Show all posts
Showing posts with label graphics. Show all posts

Thursday, September 19, 2019

Fractals in one page of code - Redux

Some time back I blogged about how someone wrote a one page program in Just BASIC that draws a colorful fractal image.

And I quote (myself):

"What's great about BASIC and languages like it is that you don't have a write a lot of code that has nothing to do with what you're trying to create (like Java for example). It should be really easy to throw together a little code and play with graphics. Programming should be fun, not a burden.

Here is a thread that shows how to draw fractals in less than a page of BASIC."

But the link is now broken because the forum site shut down and we lost a lot of good stuff.

Here's where we make it right.  A few months ago I found a fractal example for the Apple II written in Applesoft BASIC.  I thought it was pretty cool, so I adapted it for Liberty BASIC.

startMS = time$("ms")
WindowHeight = 230
open "mandelbrot set" for graphics as #gr
#gr down()
#gr fill("black")
color$ = "red orange yellow green blue red orange yellow green black"
for x = 0 to 279
for y = 0 to 95
  x1 = x / 280 * 3 - 2 : y1 = y / 191 * 2 - 1
  i = 0:s = x1:t = y1
    while i < 20 AND s * s + t * t < 4
      s1 = s * s - t * t + x1
      t = 2 * s * t + y1:s = s1: i = i + 1
    wend
  c = i/2
  #gr color(word$(color$, c + 1))
  if c <> 0 then #gr set(x, y) : #gr set(x, 191-y)
next y
next x
print "done in "; time$("ms")-startMS; " milliseconds"

Note: This is written to run on Liberty BASIC v5 to be one of the example programs.  LB5 is as of the time of this writing is in alpha test.  Click to visit the Liberty BASIC v5 board in our forum.

Saturday, August 25, 2018

Starfield Simulation in Liberty BASIC

Here is the starfield simulation that for the VIC-20 that I posted a few days ago, but ported as simply as possible to Windows using the Liberty BASIC language (http://www.libertybasic.com).  If you compare them side by side you can see that a lot of the original code is preserved across the translation.

Enjoy!

 dim st(9,1)  
 gosub [setup]  
 timer 20, [cycle]  
 [cycle]  
   for c=0 to n  
     xold=st(c,0)  
     yold=st(c,1)  
     st(c,0)=xold*1.23  
     st(c,1)=yold*1.23  
     x=int(st(c,0))  
     y=int(st(c,1))  
     if abs(x)<15 and abs(y)<15 then  
       gosub [clearStar]  
      else  
       if abs(x)>200 or abs(y)>200 then  
         st(c,0)=(rnd(1)-0.5)*3  
         st(c,1)=(rnd(1)-0.5)*3  
        else  
         gosub [drawStar]  
       end if  
     end if  
   next c  
   wait  
   goto [cycle]  
 [clearStar]  
   #starfield "color black ; place "; xCenter + xold; " "; yCenter + yold  
   #starfield "\."  
   return  
 [drawStar]  
   gosub [clearStar]  
   #starfield "color white ; place "; xCenter + x; " "; yCenter + y  
   #starfield "\."  
   return  
 [setup]  
   input"# stars (1-10)";n  
   n=n-1  
   for x=0 to n  
     st(x,0)=(rnd(1)-0.5)*200  
     st(x,1)=(rnd(1)-0.5)*200  
   next x  
   open "starfield" for graphics as #starfield  
   #starfield "home ; posxy xCenter yCenter"  
   #starfield "down ; fill black ; backcolor black"  
   #starfield "trapclose [quit]"  
   return  
 [quit]  
   close #starfield  
   end  

Sunday, February 21, 2010

CNC Machine Simulator

The two Osborne 1 computers we were using in engineering were getting a bit worn, so Bob sprung for some Kaypro computers. I don't think they appeared all at once but soon enough we had two Kaypro II machines and one Kaypro 10. The Kaypro II's were similar to the Osbornes but with bigger screens. Now we had 80 columns of bright green text. The Kaypro 10 also had a built in 10MB hard drive.

One really important difference with these new machines is that they had block graphics characters, and you could plot lines and points. This wasn't high resolution but it was good enough to inspire me to write a simple interpreter for the CNC programming language. Remember how I tried to write a programmable calculator program for my VIC-20 but never finished it? This interpreter was the realization of ideas that I'd been kicking around for a while. I wrote this as an extension of the machining text editor in MBASIC.

Now we could run a rough simulation of how a routing or drilling program would work and see the result on the screen. There was no fine detail, but it was easy to see if we accidentally drilled outside the board, or if a panel would be ruined by an accidentally placed cut. This certainly saved time and money because we were able now to make fewer first pieces so the single head Excellon machine and it's operator were more efficient.

Sunday, February 7, 2010

My own Commodore 64

One of my close friends Jeff Benitz (whom I had originally met years before at Radio Shack while playing with their TRS-80s) bought a Commodore 64. I went with him to Westwood, Massachusetts to buy the used computer. It included a monitor, a cassette deck, and a dot matrix printer. We enjoyed staying up late into the night playing a game called Telengard, which was a popular Dungeons and Dragons style game. I seem to remember we went to Toys R Us to buy the game back when you could buy tons of software for this machine in the stores. Telengard was written in BASIC, and we figured out how to break into it to modify it. Terrific fun (if you ask me).

One other cool thing I remember doing with this computer was writing a custom routine to reprogram the custom graphics characters on the fly to draw detailed images. This was in BASIC. It was slow but it worked.

Eventually Jeff sold this computer to me. I didn't use it for much and after moving a couple of times I tossed it because I couldn't find the power supply. Oops. What was valuable became obsolete and disposable. Nowadays I wish I could find the time to rediscover the simple joy of programming old home computers.

Thursday, June 7, 2007

Applesoft BASIC

So settling down to programming on the Apple II+, we had several books to help me with the process. Of course there was the standard green covered Applesoft book, and Mr. Alessi also has another book but I can't remember the title. I tried to find a shot of the book cover on Google but no luck.

Before we started writing inventory management software I tried my hand at creating some graphics. The Apple II had hi-res graphics (for that day), and Applesoft had high level commands for drawing so you didn't need to POKE and PEEK as much as in other BASICs. We had only a green phosphor monitor without color, but this was appropriate for business software development.

When editing a program in Applesoft BASIC, you could type some escape sequences to move the cursor around. If you moved the cursor up to the start of a line on the screen you could then reenter the line by moving the cursor to the right, and you could substitute some characters to change the line of code. This was crude compared to the way you did things on a VIC-20 where you just moved the cursor to the line you wanted to edit, changed just what was needed and hit Return.

Also, I had never written software before that used a floppy disk. This was another thing that I needed to become comfortable with. Our Apple II had 3 floppy drives, each was 143K. We also had a 16K card and a Z80 Softcard so the machine could run CP/M, but this was something we rarely did.

Thursday, May 24, 2007

Summer Break - My Own Company

When I was about 14 I decided to create my own company to create video games. I didn't even own a computer, but I spent a lot of time at NEECO, and they let me use theirs. The machine I targeted was the Commodore VIC-20, and I thought I was being clever when I came up with the name VinkSoft.

The game I created was a simple simulation of flying through an asteroid field. You needed to shoot the asteroids to destroy them or else they would hit your ship. It probably took a couple of dozen hours to create this game. The graphics on the VIC-20 were all done by creating custom graphics characters. Sound was pretty easy with the computer's built-in 3 voice tone generator and white noise generator. The actual BASIC code for the program wasn't very big because it all had to fit in 3.5K of RAM. The game was fast enough in BASIC that it didn't need any machine code routines.

My plan was to sell the software at NEECO, and the sales guys there agreed to do it. The software would be in a ziplock bag with some artwork and a cassette tape. A lot of software was sold this way back then. My father helped me with the artwork. He's pretty good at drawing sharp graphics, which makes sense because he spent thousands of hours drawing things at a drafting table.

So the sales guys at NEECO put the software on the wall (2 copies) with all the rest of the stuff there. Within a couple of days they had sold one! Unfortunately the owner of the company had not cleared this clandestine operation and when he found out he had my remaining copy pulled. I don't think they even gave it back to me.

The things we kids did during summer break. ;-)

Saturday, May 5, 2007

The VIC-20

In 1980 Commodore introduced a new 6502 based $299 home computer called the VIC-20. http://oldcomputers.net/vic20.html

It was essentially a version of the Commodore PET that plugged into a TV set. For the price there was nothing like it. It had Commodore BASIC built-in, color and sound, a joystick port, and a cartridge expansion slot. It didn't have graphical sprites, but it was still capable of video games and it cost about half the price of an Atari 400.

I wrote a lot of software for this machine at NEECO where I hung out. It didn't have full screen graphics, but you could program the graphics characters on the fly. It did have a very low screen resolution (22x23 text mode, 176x184 graphics mode) and it only came with 3.5K of available RAM out of the box, but we were very used to limited memory back then.

Compute! magazine had some really great software listings for this machine, like a graphics character editor for example and a machine code monitor. Just type it in and go. :-)

Saturday, April 21, 2007

Enter the TRS-80

I remember when Radio Shack started to advertise the TRS-80 computer on television. I don't think I had ever seen an ad for a computer on TV before this. It was $599 for the computer which seemed pretty amazing.

When these machines arrived at the local Radio Shack store my brother and I went to have a look. The only interesting thing you could do with the machine was program it. They may have had a couple of applications on tape in the store but we ignored them. The programming manual was very friendly, with a cartoon character that looked like a TRS-80 spouting explanations.

As I understand it the Level I BASIC that came with the machine was a custom version of Tiny BASIC. It had single letter variable names, only one string variable (I think), and it had only three error messages for the programmer:

  • What? - Syntax error
  • How? - Divide by zero, etc.
  • Sorry. - Out of memory

Level I BASIC also had SET and RESET commands so you could draw graphics on a 128x48 grid.