Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] 2D Grid Functions

Author
Message
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 14th Feb 2009 23:23 Edited at: 15th Feb 2009 04:14
This is a small collection of 2D grid functions I just wrote:



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.

lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 15th Feb 2009 03:52
thats really cool, is this for use in 3d only or can 3d apps work for this also...
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 15th Feb 2009 04:11 Edited at: 15th Feb 2009 04:12
Well the demonstration is in 2D, however you could easily use it in 3D as well if you came up with your own drawGrid( ) function which drew the grid in 3D space instead of 2D.

Login to post a reply

Server time is: 2024-05-02 03:31:43
Your offset time is: 2024-05-02 03:31:43