uint8_t values issues with vectors

  2d-vector, c++

I am trying to create a 10 by 10 grid with uint8_t values. using some coordinates I am trying to place 255 values at those indexes. When I substitute 255 at those coordinates it works fine but as soon as I try to print out the values at those indexes it doesn’t seem to be the values I wanted.

float grid_width = 10;
float grid_height = 10;
float grid_resolution = 0.1;
float rows = grid_width/grid_resolution;
float cols = grid_height/grid_resolution;

std::vector<std::vector<float>> landmarks = {{2,2},{8,8},{2,8},{8,2},{3,2},{3.5,2.1},{4.0,3.5},{4.5,2.0},{5.0,3.0}};
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));

for(auto i: landmarks)
{
    for(auto m = i[0]-(grid_resolution*2) ; m <= i[0] +(grid_resolution*2) ; m=m+grid_resolution )
    {
        for(auto n = i[1]-(grid_resolution*2); n <= i[1]+(grid_resolution*2); n=n+grid_resolution)
        {
            int m_co = m/grid_resolution;
            int n_co = n/grid_resolution;
            grid[m_co][n_co] = 255;
            cout<<"m_co: "<< m_co <<" n_co:" << n_co <<endl;
            cout<<"grid[m_co][n_co]: "<< unsigned(grid[m_co][n_co])  <<endl; //check the printout over here for index values of 18,22 you will see 255 but when you try it outside the for loops those values are rest to 0
            
        }
        
    }
}

// for(auto m: grid)
// {
//     for(auto n: m)
//     {
//         cout<<unsigned(n)<<" ";
//     }
//     cout<<endl;
// }
cout<<unsigned(grid[18][18])<<endl; //expected 255 got 255
cout<<unsigned(grid[18][22])<<endl; //expected 255 got 0
cout<<unsigned(grid[22][22])<<endl; //expected 255 got 0
cout<<unsigned(grid[22][18])<<endl; //expected 255 got 0
cout<<unsigned(grid[17][17])<<endl; //expected 255 got 0
cout<<unsigned(grid[20][18])<<endl; //expected 255 got 255

So I am taking indexes using landmarks vector and trying to replace 0 from grid vector to 255 on the indexes of landmarks, but I also want to replace 0 on the indexes around that index.
like if 2,2 is my coordinate I want to replace 1,1 and 1,2 and 1,3 and 2,1 and 2,2, and 2,3 and 3,1 and 3,2 and 3,3 to 255 but I do not get 1,3 and 2,3 and 3,3 255 i get 0
this is just an example I am trying to achive

Source: Windows Questions C++

LEAVE A COMMENT