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

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?

Shouldn’t it be:
for(int i = x1; i < x2; i++)
and
for(int j = y1; j <= y2; j++)

If I do that the resulting rectangle is one row/column smaller, than the rectangle decribed by the coordinates. It affects just the size. My problem is the content.

What you describe it seems that you go outside a certain range.

I thought so too. But where?
i * kMatrixWidth becomes maximal 15*16 and x2-x1 is between 1 and 15.

Ha! Found it. It has to be (kMatrixWidth-1). My counter goes from 0 to 15 for fullscreen, so the full width has to be 15… Thanks for your assistance @Kasper_Kamperman !