Search code examples
c++allegroallegro5

How to convert pixels into indices


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.


Solution

  • 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)