-1

The problem it's I need to change a matrix and I just have acess to these positions in pixels x and y, how can I convert x and y into an indice ?

E.g :

char *map[20][17] = { "WWWWWWWWWWWWWWWW", "WFFFFFFFFFFFFFFW", "WFWWWWWFFWWWWWFW", "WFFFFFFFFFFFFFFW", "WFWWWWWFFWWWWWFW", "WFFFFFFFFFFFFFFW", "WFWWWWWWWWWWWWFW", "WFFFFFFWWFFFFFFW", "WFFFWFFWWFFWFFFW", "WFWWWFFFFFFWWWFW", "WFWFFFWWWWFFFWFW", "WFFFWFFFFFFWFFFW", "WFWWWFWFFWFWWWFW", "WFFFWFWWWWFWFFFW", "WFFFFFFFFFFFFFFW", "WFFFWWWWWWWWFFFW", "WFWFFFFFFFFFFWFW", "WFWWWWWFFWWWWWFW", "WFFFFFFFFFFFFFFW", "WWWWWWWWWWWWWWWW"};

int x_pac = 240, y_pac = 360;

Using x_pac and y_pac to access some position on map, in this case each one of characters have height = 30 and width = 30.

2 Answers2

1

you can get the indices simply dividing x-y positions by width and height. row = y_pac / height. col = x_pac / width

Younghyo Kim
  • 364
  • 2
  • 13
0

I think I can visualize your input.

So you have a map containing a collection of bitmap "image" characters. Where the size of each bitmap "image" characters can be varied (eg. 30x30).

Say for this example: you have 4x4 char map and each character has a dimension of 6x6.

enter image description here

If you want to access a specific pixel in a specific character, use this formula:

// matrix dimension
matrix_row = <row>
matrix_col = <col>

// bitmap dimension
char_row = <char_row>
char_col = <char_col>
num_pixels = char_row * char_col

// get specific "character" in grid
character_index = ((matrix_row - 1)  * matrix_col) * num_pixels

// get specific "pixel" in character
pixel = character_index * ((char_row - 1) * char_col + pixel_offset)
Joseph D.
  • 11,804
  • 3
  • 34
  • 67