gridSnap Function

TJ Frame tjframe at gmail.com
Mon Sep 26 03:01:01 EDT 2005


Hey everyone,
  Thought I'd contribute a simple function Im using in my current project.
Someone may find it useful.
  Basically it "snaps" the mouseloc passed to it based on a given grid size
similar to countless programs where you have the ability to snap objects to
a grid. You can have non-square grids by using diffrent gridX and gridY
values
 The way I use the function is that when I move objects by user interaction
I just pass the desired location through this function first. for example -
"Set the loc of myObject to checksnap(the mouseloc,10,10)"
 I also refer to a previously declared global variable gAlignGrid, that
stores a boolean value of whether or not the grid is turned on. You can just
eliminate the first line of the function if you dont need it.
 function checkSnap mloc, gridX, gridY
 if not gAlignGrid then return mloc
 put trunc(item 1 of mloc/gridX) into xfactor
 put trunc(item 2 of mloc/gridY) into yfactor
 if item 1 of mloc >= xfactor * gridX + gridX/2 then add 1 to xfactor
 if item 2 of mloc >= yfactor * gridY + gridY/2 then add 1 to yfactor
 return round(xfactor * gridX ) & "," & round(yfactor * gridY)
end checkSnap
 Feel free to post any enhancements or more efficient methods of doing this
:)
 - TJ



More information about the use-livecode mailing list