• Create Account

# Need some help with an algorithm

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Kevin Chuca  Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 December 2013 - 07:38 PM

Hi, i have a 2D vector with the tiles of a map

vector<vector <int> > tiles;

2nd dimension is like this

{ coord_x, coord_y, graphic_id }

Now I want to change some of the base tiles according where user clicks on the map.

Problem is tile coords are all 32 multiple, and clicks can happen anywhere ofc.

My idea is to find the nearest 32 multiple and then iterate over the vector and find which tile match.

What do you guys think? Is there a more efficient way to code this?

### #2L. Spiro  Crossbones+   -  Reputation: 8730

Like
4Likes
Like

Posted 11 December 2013 - 07:52 PM

The description of the question is hard to understand but I am going to take a guess and assume you mean:

You have a 2D grid of tiles on a board, each tile being 32×32, and you can click anywhere on the board.  You want to know which tile is being clicked.

std::vector is not helping you.  Use a 2D array.

int tiles[96][64]; // Board is 64 tiles wide and 96 tiles tall.  You can make it any size you want.

Click.Pos = 459×761.

Clicked tile X = 459 / 32 = 14.

Clicked tile Y = 761 / 32 = 23.

Clicked tile = tiles[23][14].

L. Spiro

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #3ultramailman  Prime Members   -  Reputation: 1341

Like
0Likes
Like

Posted 11 December 2013 - 07:54 PM

Problem is tile coords are all 32 multiple, and clicks can happen anywhere ofc.

My idea is to find the nearest 32 multiple and then iterate over the vector and find which tile match.

Sounds like you want some kind of snapping. Why not use integer division?

### #4Kevin Chuca  Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 December 2013 - 01:02 AM

Sorry, forgot to mention...

Map is isometric, I'm rendering it as a diamond.

### #5L. Spiro  Crossbones+   -  Reputation: 8730

Like
2Likes
Like

Posted 12 December 2013 - 02:25 AM

First-off the red numbers are wrong.
[0,0] = 0
[0,1] = 1
[0,2] = 2
etc.

And not only are they wrong, they are meaningless. The black numbers are what you actually store (in tiles[5][5]).

The only difference between this and what I already gave you is that you need to convert the mouse coordinates into rectangular coordinates first, which is simple.

The origin is at the top of square [0,0]. That is X = 0, Y = 0.
The diamond increases 2 pixels along the X (as you go from the origin to the right of the screen, every 2 pixels right you go the diamond goes down the screen 1 pixel).

So start by getting the Y.
Let’s say the origin is at screen coordinate [300,25] and we click at [338,70] (with screen coordinates [0,0] being the upper-left corner).

Get the Y first.
The X coordinate is 38 pixels to the right of the origin, and for every 2 horizontal pixels it moves vertically down by 1, so 38 ÷ 2 = 19.
The adjusted Y coordinate is 29.

Now get the X.
The Y is 45 units below the board origin, and for every pixel down, 2 pixels are going to the right. So 45 × 2 = 90, which is the adjusted X.

int tiles[5][5]; // Board is 5 tiles wide and 5 tiles tall. You can make it any size you want.

Click.Pos = 19×90.

Clicked tile X = 19 / 32 = 0.
Clicked tile Y = 90 / 32 = 2.

Clicked tile = tiles[2][0].

L. Spiro

Edited by L. Spiro, 12 December 2013 - 06:32 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #6Kevin Chuca  Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 December 2013 - 04:24 AM

Thanks, will try it.

The image is one I just found through google,

### #7Kevin Chuca  Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 December 2013 - 11:40 PM

I have the y part but can't get the x.

Let’s say the origin is at screen coordinate [300,25]

...300.25 is the top corner of the diamond, right?

If I understand rightly your approach will not work for me, since I'm rendering the tiles in the order as shown in the image (the red numbers).

Edited by Kevin Chuca, 13 December 2013 - 12:19 AM.

### #8L. Spiro  Crossbones+   -  Reputation: 8730

Like
0Likes
Like

Posted 13 December 2013 - 02:06 AM

If I understand rightly your approach will not work for me, since I'm rendering the tiles in the order as shown in the image (the red numbers).

That sounds like a self-imposed limit. Unless you have a good reason for that (and I doubt one exists), it should be a 2-dimensional array via the black numbers.

300.25 is the top corner of the diamond, right?

It is.

L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS