Hi,
If you're looking to make a normal rectangular grid, then you can use the following expressions:
If each grid cell has a size of (cWidth, cHeight) and the grids origin is located at coordinates (GridX, GridY), then:
Finding the grid coordinates from 2D coordinates:
GridPosX# = (PosX# - GridX) / cWidth
GridPosY# = (PosY# - GridY) / cHeight
Finding 2D coordinates from grid coordinates:
PosX# = GridPosX# * cWidth + GridX
PosY# = GridPosY# * cHeight + GridY
'Snapping' grid coordinates:
(You basically have to round them up/down, for example)
GridPosX = int(GridPosX# + 0.5)
GridPosY = int(GridPosY# + 0.5)
-> Gives more or less unwanted results when GridPosX/Y# < 0.0 though, you might want to write your own rounding function.
'Snapping' 2D coordinates:
GridPosX# = (PosX# - GridX) / cWidth
GridPosY# = (PosY# - GridY) / cHeight
GridPosX = int(GridPosX# + 0.5)
GridPosY = int(GridPosY# + 0.5)
PosX = GridPosX * cWidth + GridX
PosY = GridPosY * cHeight + GridY
I think this sums it all up. Good luck with your project ^^!
Sven B