Please do not laugh, but I have a math/code/datatype problem.

A trivial job I thought - the goal is, to map colors based on a 16x16 noise into a smaller rectangle defined by x1 y1 x2 y2.

I tryed it with the following function:

void MapNoiseToRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {

static uint8_t ihue=0;

for(int i = x1; i <= x2; i++) {

for(int j = y1; j <= y2; j++) {

uint8_t index = noise[i * kMatrixWidth / (x2-x1)][j * kMatrixHeight / (y2-y1)];

uint8_t bri = 255;

if( colorLoop) {index += ihue;}

CRGB color = ColorFromPalette( currentPalette, index, bri);

leds[XY(i,j)] = color;

}

}

ihue+=1;

}

It works for ALMOST the complete rectangle, but there appear unexpected flickering lines close to x2/y2. Sometimes just a rectangle with a plain color.

With some combinations of x1 y1 x2 y2 it does not work at all. The second coordinate values were always bigger than the first ones.

What is the mistake I made?