Mandelbrot Generator with Wee Basic

Well, Simon’s at it again! This time he’s created a mandelbrot generator with Wee Basic. Who knew that this would be possible on a humble Nintendo DS? Here’s a screenshot:

Simon's Mandlebrot Generator

Let’s not forget the code:

rem Mandelbrot Set *


cls 0

cls 1

print 0 “This program plots a graphical”

print 0 “representation of the famous”

print 0 “Mandelbrot set. It takes a while”;

print 0 “to finish so have patience and”

print 0 “dont have too high expectations;”;

print 0 “as the graphics resolution is”

print 0 “not very high on our screen.”

print 0 “”

print 0 “At any time press A to abort.”

for y=0 to 191

line 1 0,y,255,y,2


rem maximum iterations to try *

let iter=100

rem loop through screen pixels *

for x=0  to 255

for y=0 to 191

print 0 at 5,12 “x=”+x+”, y=”+y+”   ”

let realZ=x/128-1.5

let imagZ=y/96-1

let real2=realZ

let imag2=imagZ

let bail=0

print 0 at 5,13 ”          ”

print 0 at 5,13 “r=”+realZ

print 0 at 5,14 ”          ”

print 0 at 5,14 “i=”+imagZ

let k=key()

if k=202

goto fin:


rem Inner iteration loop *

lab inner:

let rx=real2*real2

let ry=imag2*imag2

if rx+ry > 4 or bail>=iter

goto comp:


let imag2=2*real2*imag2+imagZ

let real2=rx-ry+realZ

let bail=bail+1

goto inner:

lab comp:

rem Compute plot color *

if bail < 10

let c=1


let bot=bail-10

let top=iter-10

let c=16-((16*bot)/top)


plot 1 x, y, c



let k=0

while k=0

let k=key()


lab fin:


Thanks again Simon. I’m sure we can all learn a lot by studying the code.


16 thoughts on “Mandelbrot Generator with Wee Basic

  1. Think there’s room for improvement.

    Be careful pasting the code into the editor, as the double quotes might get messed up, and watch out for rogue spaces around = signs, as wee basic hates those.

    If the ability to use a variable as the step option of a for loop was available (it threw an error) I would have been able to set a variable to 32, and in a while loop used the variable as the step to the two for loops and halved it each time until it reached 1, then the code would have progressively drawn the image so the user could get more of a feel of what the final image would be like.

    Once it was a bit faster, we could have switched the drawing to the other screen, and used the stylus to zoom in to the image and have the routine redraw with new coordinates.

    Not sure if other speed improvements could be made, similar to the old dos ‘fractint’ program which ran quite quickly on old 486 pc’s, etc.

    At least running it in the emulator allows you to use the full speed of the PC to run much faster. Choosing the highest speed option in the emulator, I was able to run at 900% speed on my laptop, thus the image generated much quicker.

    Glad you liked it as it is, though.

    • I should also add that due to the low precision of real numbers in Wee Basic, it would probably not accept much of a zoom factor (and the speed would be really depressing). The code as it stands keeps throwing errors in the plot colour calculations and the double quotes are all wrong. Replace these as soon as you paste the code into the Wee Editor or your favourite text editor.

      I have tried to modify the code to make it easier to change the coordinates for zooming in, but so far have only had a blank screen generated. I feel the coordinates being negative may require some investigation, as I set two variables, one to -2 and the other to 1. When subtracting -2 from 1 (e.g. 1 – -2) in Wee Basic, I would have thought the result would have been 3, but it doesn’t appear so. This then messes up the calculations which attempt to generate the real and imaginary values for each pixel.

      Will continue investigating, so stay tuned for an update soon.

      • Simon, try this version:
        It has one of your new fonts and allows step to contain a variable. I couldn’t reproduce your errors with spaces between ‘=’ and 1- -2 (using variables. There is an error if you try 1- -2 as a numerical expression on its own).
        Feel free to post code that fails and I can try it/fix it.

  2. Hi

    Happy New Year to you and any other readers.

    New font looks great, step with a variable works a treat, can’t get step to work with a real value (only accepts an integer). For example, drawing a circle (although I haven’t tried with a variable containing 0.1 yet, so that might work):

    rem draw a circle *
    for a=0 to 2*pi [0.1]
    plot 1 x+r*sin(a),y+r*cos(a),c

    Instead I have to use a variable and a while loop – not that much of an issue but would be good to know it is possible.

    I have not been able to recreate the problem with spaces around the equals sign either, but my mandelbrot code seems to have problems in the else block of the code which chooses the plot colour. If I split it up into sections, it worked for a while, then stopped working until I put the let c=16-… into one line together. It’s quite random.

    I have had a problem with the backspace issue. Start Wee Basic then enter the following text:

    rem this text does not fit on one line * [return]

    Go to the start of the wrapped text (the second ‘line’) then hit backspace once. The cursor goes to the end of the first line (past the end of where text should be). Hit backspace a second time and the system goes kaput!

    Tried it three times in a row and same thing each time. First, the icons disappear, then the keyboard, then both screens go blank.


    • If you do this in no$gba emulator the same thing happens then occasionally a message appears. I got the message once then have not been able to get it again and didn’t write it down, so from memory it said something like:

      Illegal Opcode and no debug vector available.

  3. Have been thinking and some new features that would be good are:

    Ability to save screenshot(s) to sd card. E.g. SAVE s “filename” where s=screen (0, 1 or 2 for both)
    Keypresses to auto repeat, so action games would be possible.
    In editor, direction keys to repeat (save multiple presses to get to end of line, for example), and direction left at position 0 moves to right of previous line, and vice versa
    Auto strip/convert cr/lf to format editor prefers. In Wee Editor pressing arrow keys doesn’t always move cursor (as presumably cr and lf in the file)
    Simple sound command to generate tones
    ABS(n) function to return positive value
    Two dimensional arrays (depends on available NDS memory)
    Additional colour number 17 which when used with plot or line causes inverse of colours already plotted/drawn. E.g. if I want to (for the fractal program) allow the user to pick where to zoom in, I have to draw some sort of rectangle, and allow the user to move and resize it to what they require. If I simply draw in a colour it would obliterate what’s already there, so an ‘inverse’ colour would solve that!

    Have had other ideas but can’t remember them now. Should write them down at my age!

    Oh, just one more thing – can we please find out the lower/upper limits for variables as that would be quite useful.



    P.S. Have been playing with the fractal program, version 2 will allow user input of coordinates.

  4. Hi again.

    One of the things to check is when including a number in a print command, the following text has a space before it – e.g.:

    print 0 at 0,0 “x=”+x+”, y=”+y

    produces something like:

    x=1.500000 , y=0.350000

    whereas I’d prefer not to have the space before the comma!


    P.S. Should I carry on posting these here, or have you a more specific place to put them. If you’d like to email me and I could then email you back, my address is: simon at needanother dot co dot uk. Please feel free to take this email address for your records then remove it from the post.

  5. Wee Editor enhancements:

    Allow change of font (or change to fixed font)
    Word wrap at 32 characters, as in Wee Basic
    Change window name to Wee Editor
    Allow change of font/background colour (so I could develop in white on black background)
    Separate Save and Save As options
    Check for errors in basic program at ‘File|Run Program’ command so doesn’t run emulator if errors found
    Remember position of window and when starting editor use previous position
    Allow Ctrl-A to select all text
    Syntax highlighting – e.g. rem statements in green, text in blue, etc.

    That’ll do for now.


    • Simon, all of your suggestions are great. I won’t have time to address any of them quickly, but I have made a (one might say ‘very rough’) hack to solve the backspace locking issue. Here’s a link to the new file:
      You’ll see that it deletes the spaces between the words that are wrapped rather than ‘unwrapping’ them. Hopefully this alleviates a lot of frustration.
      Feel free to keep posting here. I certainly don’t mind. I’m also more than happy to email you if you like.

  6. Hi

    One other suggestion concerns the reporting of errors when pressing run (a) button. Currently, the error is shown with a figure representing the position of the error in the file. This figure is based on the number of characters from the start of the file, but there’s no easy way to find out when trying to debug code where this is. Could the cursor position be displayed on the bottom screen so it is easy to find out where you are in the file. E.g. if the error is at char position 145, as you move the cursor you can see that you are currently at position 103 (as it is mentioned on the lower screen) and need to move down the file to get to 145.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s