Finding Coordinates on a grid given X.

Started by
9 comments, last by Juanu 18 years, 7 months ago
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?? :'(
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
tags. :)

[Edited by - ender7771 on September 14, 2005 8:04:39 PM]
this method works, but you should use terminology like x/y columns/rows. it'll save some confusion :)
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
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¿?¿¿
something like this might work...

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



9  10 11 12 13 14 15 161  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?
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]
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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??
I think this will work to find the row which the number is on,

ceiling(number/columns) //assuming your first row is row 1
row = floor(num / rowWidth)col = num - (row * rowWidth)


Or am I understanding this wrong..?

This topic is closed to new replies.

Advertisement