Jump to content
  • Advertisement
Sign in to follow this  
Juanu

Finding Coordinates on a grid given X.

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok... I have a BIIIIG BIIIIG Trouble.... I have A grid. WIth X columns and X lines. An Exmaple: 8 Columns, 2 columns. And evevry field is consecutively numbered. For example: This should be grid 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 So. If i Only know the amount of Columns and Line, and someone comes and tells me, Please, Find the coordinates for number 12 on the grid. How can i Find them? Ok. You se the grid and sya "Its simple, Column 4, Line 2. But you cant's see the grid. You only know that has 8 Columns and 2 lines. and you need to find number 12 I need to find a formula that works with any grid, no matter the amount of columns and lines. HELP?? :'(

Share this post


Link to post
Share on other sites
Advertisement
Quote:
I have A grid. WIth X columns and X lines.


What do you mean by this???

But I think you need to divide the number (12) by the number of columns to get which column it is on. Then take the mod to get which one it is.

But try to rephrase your question.

Edit: Fixed the [ quote ] tags. :)

[Edited by - ender7771 on September 14, 2005 8:04:39 PM]

Share this post


Link to post
Share on other sites
this method works, but you should use terminology like x/y columns/rows. it'll save some confusion :)

Share this post


Link to post
Share on other sites
Ok
I know it's complicated
But it's as complicated lso to formulate the quesdtion..
butr you got it right..
I just got how to get the column position...
but..

How do I find the LIne?

so

12 is on column 4 because 12 mod 8 = 4


But the line?

:S

P:.S: SORRY. I MEANT X, Y

Share this post


Link to post
Share on other sites
Ok

This is why i mneed these

I have a class. Wich receives as parameters, the number of columns and lines of the SPRITE SHEET.

So. It knows that it has (columns * lines ) sprites. The first one is on 1,1 and the last one is on columns, lines. But theres a class thaat tells this class, wich frame it needs by passing it only the number.

So. The first class must find this frame number by only haiving those 3 parameters. Columns, LInes and number of frame it need.

I already know how to find the column. Number mod Columns.

But now i just need the Line in wich is it..

Any Help¿?¿¿

Share this post


Link to post
Share on other sites
something like this might work...


int offset(const int& row, const int& col)
{
return row * (nCellsPerRow + col);
}




9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8

12 = row: 2, col: 4...

plug row-2 and col-4 into the function you get 12

is that what you mean?

Share this post


Link to post
Share on other sites
row = ct div width
column = ct mod width

where div is C/C++ integer division (floor of the real value)

Since indices are almost certainly zero-based, this will work. A truer representation of how things will work is as follows:

[00] [01] [02] [03] [04] [05] [06] [07]
[08] [09] [10] [11] [12] [13] [14] [15]


Thus, 12 div 8 is index 1, indicating row 2, and 12 mod 8 is index 4, indicating column 5.

To put it in C++ terms:

int field = 12;
int row = field / grid_width;
int col = field % grid_height;


Hope that helps,
Twilight Dragon

[Edited by - TDragon on September 14, 2005 7:16:30 AM]

Share this post


Link to post
Share on other sites
nOPE. I never ereceive the Column and the Row.

I only receive the numbner

and with that i have to find it.

As i Said.
I got the part to find the column

Number MOD columns (if it's 0, is the last column)

But what about the rows?

How can I find the Row number by now having, Columns, Lines, and column position of teh number??

Share this post


Link to post
Share on other sites
I think this will work to find the row which the number is on,

ceiling(number/columns) //assuming your first row is row 1

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!