As an example for a series of tutorials, I created this little snippet. I think it won't hurt to post it in here.
sync on
sync rate 0
`Create vector that we'll be working with
r = make vector4(1)
`Create matrix4 that will transform our vector back and forth
#constant WorldToScreen 2
r = make matrix4(WorldToScreen)
set matrix4 WorldToScreen, 30.0, -15.0, 0.0, 0.0, -30.0, -15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, screen width() / 2, screen height() / 4 * 3, 0.0, 1.0
#constant ScreenToWorld 3
r = make matrix4(ScreenToWorld)
det = inverse matrix4(ScreenToWorld, WorldToScreen)
do
`Clear screen
cls
`Draw grid
DrawGrid(0, 0, 20, 20)
`Show world coordinate from mouse
set vector4 1, mousex(), mousey(), 0, 1
transform vector4 1, 1, ScreenToWorld
text 0, 0, "(" + str$(x vector4(1), 2) + ", " + str$(y vector4(1), 2) + ")"
sync
loop
function DrawGrid(StartX, StartY, EndX, EndY)
`Draw X direction
for x = StartX to EndX
`Find start coordinates
set vector4 1, x, StartY, 0, 1
transform vector4 1, 1, WorldToScreen
sx# = x vector4(1)
sy# = y vector4(1)
`Find end coordinates
set vector4 1, x, EndY, 0, 1
transform vector4 1, 1, WorldToScreen
line sx#, sy#, x vector4(1), y vector4(1)
next x
`Draw Y direction
for y = StartY to EndY
`Find start coordinates
set vector4 1, StartX, y, 0, 1
transform vector4 1, 1, WorldToScreen
sx# = x vector4(1)
sy# = y vector4(1)
`Find end coordinates
set vector4 1, EndX, y, 0, 1
transform vector4 1, 1, WorldToScreen
line sx#, sy#, x vector4(1), y vector4(1)
next x
endfunction
It can easily be adapted to work with any coordinate system you want anywhere on your screen or even in 3D space.
Cheers!
Sven B