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.

No comments: