jbcurtin
jbcurtin

Reputation: 1803

How do I find a coordinate using cartesian coordinate system with just an index?

How do I find a coordinate using cartesian coordinate system with just an index?

I'm not married to the order of the grid, but I want to call the location by an index rather then a coronate if at all possible.

I'm trying to create a grid for game tiles.

enter image description here

Upvotes: 0

Views: 486

Answers (4)

taserian
taserian

Reputation: 623

What you're loking for is what's known as a pairing function, where (x, y) maps to a certain integer N and vice-versa (the function is one-to-one and onto). Keith's first answer (for finite ranges) is close, but requires you to know the maximum size of the square, in effect adding another parameter to the pairing function. His screenshot in Excel (for infinite ranges) shows how it's done, but I'd like to add some explanation to it.

Given a value N that you want to map to a coordinate (x, y):

First, we locate which layer it belongs to. A layer is what Keith showed in his Excel column D, and goes something like this:

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

You find out what layer N belongs to by

layer = math.floor(math.sqrt(N - 1)) + 1

Given the layer, find the integer corresponding to the diagonal (shown above with a ', with values 1, 3, 7, 13 for layers 1, 2, 3, 4; Column H in Keith's answer)

diagonal = (layer^2) - layer + 1 

Now that you have the diagonal, we can find the values of x and y (Keith's columns I and J):

if (N < diagonal):   
    x = layer
    y = N - ((layer-1)^2) + 1  
elif (N == diagonal):
    x = layer
    y = layer
else:
    x = (layer^2) - N + 1
    y = layer

My formulas look a little different from Keith's, but they're ultimately derived from the same place. I did my calculations independently, then compared them to Keith's, and found that they're pretty much identical.

Upvotes: 1

joel goldstick
joel goldstick

Reputation: 4493

Maybe I'm reading too little into this, but what is wrong with a dictionary of tuples where the key is the index, and the tuple contains the coordinates.

Upvotes: 0

Keith
Keith

Reputation: 6834

Assuming the coordinates are:

  • Purely integer
  • Finite, say in a square of dimension 'N'.

Then

 (x,y) -> x + N*y
   n -> (x,y) = (n%N,  n/N)

See also: this wikipedia picture for a way of handling non-finite ranges. Here is an Excel image Excel. Excel is quite a nice tool for prototyping a calculation. you can see at a glance each step of the computation.

Obviously, you can map this into code very easily; it handle the infinite quarter grid case.

Upvotes: 3

Alex Reynolds
Alex Reynolds

Reputation: 96937

Making some assumptions here, but you could solve these as linear equations:

9 = 1x + 3y
4 = 1x + 2y
-----------
5 = 0x + 1y

->  5 = y
-> -6 = x, by plugging y back into one of the two linear equations

Now you that you have (x, y), you can translate any coordinate pair (x', y') into an index:

newIndex = -6x' + 5y

If you need to wrap around, use a modulus.

Upvotes: -1

Related Questions