This is a small collection of 2D grid functions I just wrote:
Rem Project: 2D stufffs
Rem Created: Saturday, February 14, 2009
Rem ***** Main Source File *****
global mode as integer : mode = 1
goSub initDisplay
do
cls
if keyState( 2 )
mode = 1
endIf
if keyState( 3 )
mode = 2
endIf
if mode = 1
circle snapToGridPtX( mouseX( ), 200, 50, 10 ), snapToGridPtY( mouseY( ), 200, 50, 10 ), 5
endIf
if mode = 2
x1 = snapToGridTilePosX( mouseX( ), 200, 50, 10 )
y1 = snapToGridTilePosY( mouseY( ), 200, 50, 10 )
x2 = x1 + 50
y2 = y1 + 50
box x1, y1, x2, y2
endIf
drawGrid( 200, 200, 50, 50, 10, 10, 0xFFFFFF )
print "Press '1' to select 'point' mode. The circle will be drawn on the point nearest to the mouse."
print "Press '2' to select 'tile' mode. The tile which the mouse is over will be selected."
print " "
printc "Current Tile: "
printc snapToGridTileX( mouseX( ), 200, 50, 10 )
printc ", "
printc snapToGridTileY( mouseY( ), 200, 50, 10 )
print " "
printc "Current Snap Position: "
printc snapToGridPtX( mouseX( ), 200, 50, 10 )
printc ", "
printc snapToGridPtY( mouseY( ), 200, 50, 10 )
sync
loop
//*****************************************************************************
//initDisplay routine - sets the resolution to the highest supported
//resolution on the current hardware.
//*****************************************************************************
initDisplay:
load dll "user32.dll", 1
global g_screenWidth as integer : g_screenWidth = call dll( 1, "GetSystemMetrics", 0 )
global g_screenHeight as integer : g_screenHeight = call dll( 1, "GetSystemMetrics", 1 )
delete dll 1
global g_colorDepth as integer : g_colorDepth = 16
global g_syncRate as integer : g_syncRate = 60
set display mode g_screenWidth, g_screenHeight, g_colorDepth
sync on
sync rate g_syncRate
return
//************************************************************************************
//drawGrid function - draws a 2D grid with the specified parameters.
//************************************************************************************
function drawGrid( xPos as float, yPos as float, cellSizeX as float, cellSizeY as float, numCellsX as float, numCellsY as float, lineColor as DWORD )
//ink lineColor, 0
for y = 0 to numCellsY - 1
for x = 0 to numCellsX - 1
line xPos + ( x * cellSizeX ), yPos + ( y * cellSizeY ), xPos + ( x * cellSizeX ), yPos + ( y * cellSizeY ) + cellSizeY
line xPos + ( x * cellSizeX ), yPos + ( y * cellSizeY ), xPos + ( x * cellSizeX ) + cellSizeX, yPos + ( y * cellSizeY )
line xPos + ( x * cellSizeX ) + cellSizeX, yPos + ( y * cellSizeY ), xPos + ( x * cellSizeX ) + cellSizeX, yPos + ( y * cellSizeY ) + cellSizeY
line xPos + ( x * cellSizeX ), yPos + ( y * cellSizeY ) + cellSizeY, xPos + ( x * cellSizeX ) + cellSizeX, yPos + ( y * cellSizeY ) + cellSizeY
next x
next y
endFunction
//************************************************************************************
//snapToGridPtX function - returns X coordinate of the snap-to-grid location.
//************************************************************************************
function snapToGridPtX( x as float, gridPosX as float, cellSize as float, numCells as float )
locX as float
locX = x - gridPosX
if ( locX mod cellSize ) < cellSize / 2
locX = floor( locX / cellSize ) * cellSize
else
locX = ceil( locX / cellSize ) * cellSize
endIf
locX = locX + gridPosX
endFunction locX
//************************************************************************************
//snapToGridPtY function - returns Y coordinate of the snap-to-grid location.
//************************************************************************************
function snapToGridPtY( y as float, gridPosY as float, cellSize as float, numCells as float )
locY as float
locY = y - gridPosY
if ( locY mod cellSize ) < cellSize / 2
locY = floor( locY / cellSize ) * cellSize
else
locY = ceil( locY / cellSize ) * cellSize
endIf
locY = locY + gridPosY
endFunction locY
//************************************************************************************
//snapToGridTileX function - returns X tile which the point is in.
//************************************************************************************
function snapToGridTileX( x as float, gridPosX as float, cellSize as float, numCells as float )
locX as float
locX = x - gridPosX
locX = floor( locX / cellSize )
endFunction locX
//************************************************************************************
//snapToGridTileY function - returns Y tile which the point is in.
//************************************************************************************
function snapToGridTileY( y as float, gridPosY as float, cellSize as float, numCells as float )
locY as float
locY = y - gridPosY
locY = floor( locY / cellSize )
endFunction locY
//************************************************************************************
//snapToGridTilePosX function - returns position of the X tile which the point is in.
//************************************************************************************
function snapToGridTilePosX( x as float, gridPosX as float, cellSize as float, numCells as float )
locX as float
locX = x - gridPosX
locX = floor( locX / cellSize )
locX = ( locX * cellSize ) + gridPosX
endFunction locX
//************************************************************************************
//snapToGridTilePosY function - returns position of the Y tile which the point is in.
//************************************************************************************
function snapToGridTilePosY( y as float, gridPosY as float, cellSize as float, numCells as float )
locY as float
locY = y - gridPosY
locY = floor( locY / cellSize )
locY = ( locY * cellSize ) + gridPosY
endFunction locY
The functions are:
drawGrid( ) - draws a grid with the specified position, cell size, and number of cells.
snapToGridPtX( ) - returns the X position in pixels of the point on the specified grid which is closest to the specified point.
snapToGridPtY( ) - returns the Y position in pixels of the point on the specified grid which is closest to the specified point.
snapToGridTileX( ) - return the X coordinate of the tile which the point is over.
snapToGridTileY( ) - return the Y coordinate of the tile which the point is over.
snapToGridTilePosX( ) - return the X position of the tile which the point is over.
snapToGridTilePosY( ) - return the Y position of the tile which the point is over.
It probably seems a little complex for a simple grid system but if you run the code and check it out a bit you'll see that it's actually very simple. If you have any questions feel free to ask.