-
Official FK nice guy and MOD
X & Y coordinates from grid
Hello peeps, my first time here in Math and Physics.
I am not a math junky by any means, and am having a hard time getting my head around a formula.
Heres the deal:
I have a a grid with 10,000 sqaures on an 800x800 canvas
The grid in not of Mcs but an array.
Getting the _x,_y coords from a mouse event is not a problem, but I need to get the _x,_y coords based upon a returned number from the database.
PHP Code:
var gridHeight = 800;
var gridWidth = 800;
var markerHeight = 8;
var markerWidth = 8;
var gridRows = gridHeight / markerHeight;
var gridCols = gridWidth / markerWidth;
// 3046 of 10000 db items
// known values for mcId 3046 are X:552 Y:432
// using this value to test with, the math formula should return 432
// just as mine below returns 552
var mcId = 3046;
// this is my formula to determine the _x value of a grid square based upon the
// the mcId value between 1 and 10000
mcX = Math.round(Math.floor((gridCols-(mcId / gridWidth)*markerWidth))*markerWidth);
That forumla works 100% in all cases so far for grabbing the x coordinate based upon the id number between 1 and 10,000.
Now I need help with getting the _y value. I cant for the life of me figure this out.
Any ideas you math wizards?
Any help would be appreciated,
3P
Last edited by 3PRIMATES; 11-20-2007 at 02:34 AM.
-
Official FK nice guy and MOD
Ok, I got it. My brain is melting but I did it..LOL
Here is the modified and working code. If you would, please look it over and see if I have done anything real idiotic. Think iy could be trimmed down a bit but I am not sure.
thanks.
3P
PHP Code:
gridHeight = 800;
gridWidth = 800;
markerHeight = 8;
markerWidth = 8;
gridRows = gridHeight / markerHeight;
gridCols = gridWidth / markerWidth;
gridSquared = gridRows * gridCols;
// mcId for testing:
//3046 of 10000 db items
mcId = 3046;
// formula to determine _x value of grid square based on value between 1 and 10000
mcX = Math.round(Math.floor(gridCols - (mcId / gridCols)) * markerWidth);
// formula to determine _y value of grid square based on value between 1 and 10000
mcY = Math.round(Math.floor((10000 - mcId) * markerHeight)- mcX * gridRows);
-
M.D.
var mcX:int = (mcId % gridCols) * markerWidth;
var mcY:int = (mcId / gridCols) * markerHeight;
that should do it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|