Doing graphics on the fly |
Try online | Examples |
This example will demonstrate the kind of things you can do with the
ability Aptilis has to generate pictures. We will calculate biorythms.
Biorythms are based on the assumption that humans go through cycles.
(I am not necessarily endorsing that, but it's a nice programming example.)
There's an emotional cycle that lasts 28 days, a physical one that goes on
for 23 days, and an intellectual one that spans 33 days. In short that means
that every 23 days you are at the top of your physical shape. The cycles
start the day you're born.
Here's what the HTML looks like:
<FORM action="/cgi-bin/aptilis.exe" method="GET" target="biograph"> <INPUT type=hidden name=file value="/home/scripts/biorytms.e.txt"> <B>Day of birth (1, 2, ...30, 31)</B> <INPUT type="text" size="8" name="day"><BR> <B>Month of birth (1...12)</B> <INPUT type="text" size="8" name="month"><BR> <B>Year of birth (ex: 1970)</B> <INPUT type="text" size="8" name="year"><BR> <BR> <INPUT type=submit value="Check biorythms"> </FORM>
And here's the Aptilis source:
This programme caused me a few headaches as either my local server, some hidden proxy on the web
or even the server would defeat it by caching the generated pictures. So trying a date would work,
but any subsequent test would still bring the same original bio-rythm curve. I solved the problem by
using the GET method. It puts the data from the form in the URL, and that can be limited in size, but
at least that makes for a different URL for each different date, and no more caching problem. Since
we are not sending a lot of data, GET is fine here.
sub main
_twoPi = 2 * 3.141592654
if year > 1900
year = year - 1900
end if
// The time functions won't go before 1970, so we implemented our own
nDays = getDays(day, month, year) - 7
b = createBitmap(400, 250)
if b = -1
print("Content-type: text/plain\n\n")
print("Sorry, it was not possible to create a bitmap...")
else
print("Content-type: image/gif\n\n")
white = RGB(255, 255, 255)
black = RGB(0, 0, 0)
red = RGB(255, 0, 0)
green = RGB(0, 255, 0)
blue = RGB(0, 0, 255)
cleargray = 11
gray = 10
setColor(b, gray, 160, 160, 160)
setColor(b, cleargray, 192, 192, 192)
clearBitmap(b, white)
box(b, 0, 0, 399, 249, gray, 0)
box(b, 2, 10, 397, 194, cleargray, 1)
line(b, 67, 4, 73, 4, gray)
line(b, 70, 4, 70, 200, gray)
box(b, 48, 200, 92, 220, gray, 0)
printAt(b, 50, 217, "today", blue)
line(b, 70, 220, 70, 246, gray)
line(b, 67, 246, 73, 246, gray)
for i=1 to 39
x = i * 10
line(b, x, 241, x, 246, gray)
end for
for i=1 to 4
printAt(b, 77 + i * 70, 217, "+" + int(i * 7)$, gray)
end for
dl = int(nDays + 7) + " days lived" $
getStringMetrics(b, dlm[], dl$)
birth = format("0", 2, 0, day) + "/" + format("0", 2, 0, month) + "/" + int(year + 1900) $
DoCurve(b, nDays, red, 28)
DoCurve(b, nDays, blue, 23)
DoCurve(b, nDays, green, 33)
printAt(b, 399 - dlm[0] - 2, 26, dl $, black)
printAt(b, 316, 42, birth$, black)
printAt(b, 4, 26, "Cycles:", black)
printAt(b, 12, 48, "Emotional", red)
printAt(b, 12, 68, "Physical", blue)
printAt(b, 12, 88, "Intellectual", green)
outputGIFBitmap(b)
deleteBitmap(b)
end if
end main
sub DoCurve(b, n, colour, p)
// to avoid calculations on large values, which breaks the sin function on some platforms.
n = (n % p) - 1
ox = 2
oy = 102
x = 2
for i=.2 to 39.7 step .1
y = 102 - sin( (i + n) * _twoPi / p) * 92
line(b, ox, oy, x, y, colour)
ox = x
oy = y
x = x + 1
end for
end DoCurve
sub getDays(d1, m1, y1)
// The time functions won't go before 1970, so we'll implement our own.
m[1] = 0
m[2] = 31
m[3] = 59
m[4] = 90
m[5] = 120
m[6] = 151
m[7] = 181
m[8] = 212
m[9] = 243
m[10] = 274
m[11] = 304
m[12] = 334
t = getTime()
FillTimeArray(ta[], t)
q1 = d1 + m[m1] + y1 * 365.25
q2 = ta[3] + m[ta[4] + 1] + (ta[5] - 1900) * 365.25
if (y1 & 3) = 0 and m1 > 2
q1 = q1 + 1
end if
if (ta[4] & 3) = 0 and ta[4] > 2
q2 = q2 + 1
end if
return q2 - q1
end getDays