2D example of snapping a box to a grid. Position of the box is represented by grid coordinates.
gridSize = 50
snap = 10 :`snaps to grid if within 10units of grid
objectx = 3
objecty = 2
objectSize = 100
DO
cls
ink rgb(0,255,0),0
for x = 1 to 10
line x*gridSize,1,x*gridSize,480
next x
for y = 1 to 10
line 1,y*gridSize,640,y*gridSize
next y
if mouseclick() = 1
if flag = 0 and mouseWithin(objectX*gridSize, objectY*gridSize,objectX*gridSize+objectSize,objectY*gridSize+objectSize) = 1 then flag = 1
if flag = 1
gx = (mousex()/gridSize)
gy = (mousey()/gridSize)
objectX = gx
objectY = gy
endif
else
flag = 0
endif
ink rgb(0,0,255),0
box objectX*gridSize, objectY*gridSize,objectX*gridSize+objectSize,objectY*gridSize+objectSize
LOOP
function mouseWithin(x1,y1,x2,y2)
if mousex() > x1 and mousey() < y2 and mousey() > y1 and mousey() < y2 then exitfunction 1
endfunction 0
"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike