# solving this system of linear equation.

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

## Recommended Posts

I need a bit of help recognizing the pattern to figuring out the following problem:

so, here's the problem:
the numbers are arranged in how the tile's are layed out in memory, however, i want to be able to get a patch of tile's relative to how the white lines are layed out.

for example: column 1 is 6, 9, and 13. 2 is 3, 7, and 10, and 3 is 1, 4, and 8.
so my input would be something like this: the bottom tile's index in the world(in this case that is tile #6), and then i'd specify which tile i was last on, and finally i'd pass the column and row size to get the chunk of.

the goal is to quickly determine which column/row the last tile is on, increment first by column, then by row. so i'd iterate over tiles 6, 9, 13, 3, 7, 10, 1, 4, and 8.

i'm having trouble creating an algorithm which can extract the row/column from the index of the tile(and do the reverse).

edit: after a bit more analysis, i figured out an algorithm to suit my needs.

edit2: ok, i can get the column/row, and thought i could figure out how to get back easily. But i can't seem to figure out how to get back to the index.

This is what i'm doing to get the column/row:
//above example, the width and height can be taken as 3, and 5.
//note all divisions are integers, and round down.
void Get(int Index){
int x = Index%width;
int y = Index/width;
int column = x+(height/2-((height&1)^1))-(y/2);
int row = x+(y+1)/2;
}

Edited by slicer4ever

##### Share on other sites

Rearrange your column equation to solve for x, or y if you prefer. Do the same for the opposite axis in the row = equation. now subsititute on for the variable in the other. with X and Y you can repeat the first step with the other equation to get back to Index. But why not just use the axii of the tile map(up left and up right in your image above) as opposed to the world X and Y axii. The numbers and indices become much easier to work with.

##### Share on other sites

Burnt_Fyr, on 03 Jun 2013 - 08:53, said:
Rearrange your column equation to solve for x, or y if you prefer. Do the same for the opposite axis in the row = equation. now subsititute on for the variable in the other. with X and Y you can repeat the first step with the other equation to get back to Index.

I've tried this, but my math isn't coming up right somewhere, this is what i've done:
int x = Index%width;
int y = Index/width;
int column = x+(height/2-((height&amp;1)^1))-(y/2);
int row = x+(y+1)/2;

width = 3, height = 5 = (5/2 = 2 as int)
Index(x, y) = column, row

Index: 0 (0, 0) = 2, 0
c = x+2-y/2;
2 = x+2-y/2
-2 =  -2
0 = x-y/2
+y/2 =+y/2
y/2 = x
*2   = *2
y = 2x

r = x+(y+1)/2
0 = x+(2x+1)/2
0  = x+x ?//not sure if my math here is correct, as it's integer math, the 1/2 would round out right?
0 = 2x
/2  = /2
0 = x
y = 2x = 2(0) = 0

Index: 3(0, 1) = 2, 1
c = x+2-y/2
2 = x+2-y/2
-2 =  -2
0 = x-y/2
+y/2 = +y/2
y/2 = x
*2  = *2
y = 2x

r = x+(y+1)/2
1 = x+(2x+1)/2
1 = x+x ?
1 = 2x
/2 = /2
0 = x
y = 2x = 2(0) = 0 != 1


Burnt_Fyr, on 03 Jun 2013 - 08:53, said:
But why not just use the axii of the tile map(up left and up right in your image above) as opposed to the world X and Y axii. The numbers and indices become much easier to work with.

in this particular instance, it would make more sense to arrange the tiles that way, but when it comes to rendering, and figuring out what tile's been clicked, or what tile an object is on, it's easier for me to arrange the tiles in world x/y instead of the grid's relative x/y

edit: ok, so here's a couple of wolfam's demonstrating the solution, but i'm not sure how wolfam figures out the integer solution.

this is the example equation I used for calculating the column/row:
http://www.wolframalpha.com/input/?i=%28x%2Bfloor%28h%2F2%29-floor%28y%2F2%29+%3D+c%2C+x%2Bfloor%28%28y%2B1%29%2F2%29+%3D+r%29+where+x%3D+0+and+y+%3D+2+and+h%3D5

and plugging in c and r back into the function, wolfam can figure out the correct solution, but i can't figure out how to solve this problem:
http://www.wolframalpha.com/input/?i=solve+%28x%2Bfloor%28h%2F2%29-floor%28y%2F2%29+%3D+c%2C+x%2Bfloor%28%28y%2B1%29%2F2%29+%3D+r%29+where+c%3D+1+and+r+%3D+1+and+h%3D5 Edited by slicer4ever

##### Share on other sites
i've yet to be able to solve my issue of going back, so i'm bumping this thread looking for a bit of help on achieving the correct result.

##### Share on other sites

It's too early for me to work through your math, escpecially when taken out of context of what your trying to do. So instead I'm going back to my suggestion earlier, and attempt to address the concerns you had above.

Using a grid of [x,y] for the tiles, the indices are easier to work with, as I said before.

If you want that angled look, during rendering, just apply a -45 degree rotation to the camera, and this gives the typical rts top down view that you are after. When it comes to picking a tile, you create the pickray, and then transform it back into the world via the projection and view matricies. This puts the pickray back into the right space, so that the coordinates where it intersects the tile map are correct relative to that map. For determining what tile a unit is on, simply grab the coordinates of the unit, as these are already in the same space of the tile map.

##### Share on other sites
i've already figured out the equations:
column = x-y/2;
row = x+(y+1)/2;

x = row/2+(colum+((row&1)?((column>0)?1:0):((column<0)?-1:0)))/2
y = row-column

Edited by slicer4ever

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013241
×