I've been doing some contract programming over August so things have been a bit quiet from me on the FIGnition front!
Nevertheless, I thought I'd do a little rotating square graphics demo; without the comments it's another little one block wonder! Here's the code for it:
( Rot square draw
the points
[x,y]
[-y,x]
[-x,-y]
[y,-x]
x' = x*cos[t] - y*sin[t]
y' = x*sin[t] + y*cos[t]
)
: sqDraw ( x y)
4 0 do
2dup - >r 2dup + neg ( x y -x-y : x-y)
>r over 80 + over 80 ( x y x+80 y+80 : -x-y x-y)
+ dr> line
swap neg ( x' -y')
loop
;
: rotFxy ( c s fx fy)
d>r d>r 2over 2over ( c s c s:x y)
dr> 2swap 2over f* d>r ( c s c x : s*x y)
2over f* 2swap dr> ( c s x*c c s*x :y)
2swap dr> 2dup d>r f* ( c s x*c s*x c*y :y)
f+ dr> 2swap d>r d>r ( c s x*c :y s*x+c*y)
2over dr> f* f- dr> ( c s x*c-s*y s*x+c*y)
;
: rotSq ( cosA sinA --)
1 vmode cls 3 pen
54. 54. 0 0 d>r ( c s fx fy : ox oy)
begin
2dup fint drop >r ( c s fx fy : ox oy)
2over fint drop >r ( c s fx fy x y : ox oy)
sqDraw ( c s fx fy x y : ox oy)
dr> sqDraw 2drop d>r ( c s fx fy : x y)
rotFxy ( c s fx' fy' : x y)
inkey until
2drop 2drop 2drop
2drop dr> 2drop
0 vmode
;
You can run it with, for example:
0.9848078 0.1736482 rotSq
This rotates 10º on each frame and runs at 10fps in PAL video.
OK, so what's interesting about it? Well, primarily, the nice thing about drawing and rotating squares is that you only really have to compute and rotate one point, because all the other points in a square involve a 90º rotate each time and that's easy, it's just swap neg. Secondly, it's worth noting that these kinds of things are now much easier with the floating point arithmetic on version 1.0.x.
The program is pretty simple. sqDraw just draws the actual square itself, relative to an origin of (80,80). rotFxy applies a rotation calculation of the angle arcsin(s) to (fx,fy). It does this by treating it as a rotation matrix, with c and s supplying the cosine and sine terms.
rotSq puts it all together. It sets the system to run in hires mode with an xor pen and on each loop it first draws the new square; then erases the old square; and finally rotates the square's coordinates. I picked (54., 54.) as the starting coordinates, because it gives a diagonal of about 76, which is pretty much the maximum that will fit on the screen.
In the next post I'll show a little video of it running.
-cheers from Julz
--
The DIY 8-bit computer from nichemachines™