Saturday, 7 May 2011

A program, egad!

Okay, I know: it doesn't say "Hello World!" This is a disgraceful breach of convention, and I'm utterly ashamed of myself. Now... as I hinted a little while ago, it is indeed SdlBasic I've chosen, since I actually understand most of it, which alone puts it way ahead of C++ and the like. It's a very simple program to plot a Lorenz Attractor, something I've always rather liked watching.

I must be honest and say that the screenshot is actually from the PC. The version of SdlBasic in the Ubuntu repository doesn't seem entirely finished in terms of presentation (it tends to crash) but the actual language works, and I can use the code with no changes on the GP2X. The timings on the GP2X are a lot slower: something like 3.5 seconds overall and 2 seconds for the drawing.

'Very basic Lorenz Attractor plotter

'Set up the display
setdisplay(320, 240, 16, 1)

'Initialise variables
s = 10 : p = 28 : b = 8/3
x = 10 : y = 0 : z = 10
dt = 0.002
drawstarttime = timer

'Actual plottery
for n = 1 to 20000
dx = s * (y - x)
dy = x * (p - z) - y
dz = x * y - b * z
x = x + dx * dt
y = y + dy * dt
z = z + dz * dt
plot(160 + y * 5, z * 5, rgb(255, 255, 0))

'Print elapsed time (in s) since program / drawing start
ink(rgb(255, 255, 255))
timetaken = timer : drawtime = timer - drawstarttime
timedisplay$ = str$ (timetaken / 1000) + " / " + str$ (drawtime / 1000)
text(0, 0, 16, timedisplay$)

'Wait for button press before ending
j = bjoy(0)
while j = 0
j = bjoy(0)
end while




  1. Well frankly I think a Hello World program would have been far better a test of the machines capabilties. ;)

    I do like the look of this though. I was half expecting to see a mass of trigonometry commands among that listing, because of the curving shapes. But it doesn't *seem* to be the case. I wonder if this could be converted to Blitz...

  2. It should be fairly easily convertible to any Basic that has high-res graphics capabilities, since all you really need is a PLOT command (or the equivalent) -- all the rest is maths. =:)

  3. In that case I'll definitely give it a try, when I have a bit of time spare, and I'll let you know if it worked out. :)