# Simple tile selection

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

## Recommended Posts

Could some kind soul point me in the right direction for a simple 2D non-isometric tile selection algorithm. I want to be able to select a rectangular area of tiles of arbitrary size from my tile map. I have a function to get a tile index from my 1d tile array based on screen coords. I've come up with this but it only works when you start dragging from up/left and go down/right.
void multiSelect(int r1, int c1, int r2, int c2)
{
//r1 c1 is beginning row/column
//r2 c2 is end row/column
//getIndex() gets an index from a row/column

for(int i = getIndex(r1, c1); i <= getIndex(r2, c1); i+=MAP_W)
{
for(int j = 0; j <= c2-c1; j++)
{
}
}
}

##### Share on other sites
It only works from top-left to bottom-right because 5 - 2 == 3, and 2 - 5 == -3.

#include <algorithm>
#include <vector>

std::vector<Tile> selection;
unsigned selectionWidth = 0, selectionHeight = 0, selectionArea = 0;

void multiSelect( int r1, int c1, int r2, int c2 )
{
if ( r2 < r1 )
std::swap( r1, r2 );
if ( c2 < c1 )
std::swap( c2, c1 );

selectionWidth = c2 - c1;
selectionHeight = r2 - r1;

selectionArea = selectionWidth * selectionHeight;
selection.resize( selectionArea );

for ( int cy = 0, row = r1; row < r2; ++row, ++cy )
for ( int cx = 0, col = c1; col < c2; ++col, ++cx )
selection[ selectionWidth * cy + cx ] = getTileAtLocation( row, col );
}

##### Share on other sites
Well DUH! Man why didn't I think of that. Once again _fastcall you are DA MAN.
Thanks for the direction.
rating++;

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632580
• Total Posts
3007194

×