我正在拍摄黑色像素的图像并使用此功能绘制白色矩形。该函数的尺寸为正负值,负向上和向左,而正向下和向右。它取正值很好,我尽力在我的代码中考虑负值,但是当矩形的高度为负时,它被绘制得很错。我们还可以假设输入永远不会超出图像的范围。为什么负宽度不正确?我有其他程序在屏幕上绘制这个。 输入示例: draw_rectangle(img,20,20,10,10,8,-8,255); img是一个像素数组(uint8_t)
void draw_rectangle( uint8_t array[], unsigned int cols, unsigned int rows, int x,
int y, int rect_width, int rect_height, uint8_t color ){
unsigned int start = x + (y * cols);
unsigned int startDown;
unsigned int startOther;
if(rect_height > 0){
startDown = (x + ((y-1) * cols)) + (cols * rect_height);
} else if(rect_height < 0){
startDown = (x + ((y-1) * cols)) - (cols * rect_height * -1);
}
if(rect_width > 0){
startOther = ((x + (y * cols)) + rect_width -1);
} else if(rect_width < 0){
startOther = ((x + (y * cols)) - rect_width -1) ;
}
if(rect_width > 0 ){ //if the width is posivite
unsigned int drawIndex = 0;
while(drawIndex < rect_width){
//if((x + rect_width) < cols){
array[start + drawIndex] = color;
array[startDown + drawIndex] = color;
//}
drawIndex++;
}
} else if(rect_width < 0){ //if the width is negative
unsigned int posValue = rect_width * -1;
while(posValue > 0){
//if((x - rect_width) >= 0){
array[start - posValue + 1] = color;
array[startDown - posValue + 1] = color;
//}
posValue--;
}
}
if(rect_height > 0){
unsigned int drawIndex = 0;
while(drawIndex < rect_height){
//if((x + rect_width) < cols){
array[start + (drawIndex * cols)] = color;
array[startOther + (drawIndex * cols)] = color;
//}
drawIndex++;
}
} else if(rect_height < 0){ //if the height is negative
unsigned int posValue = rect_height * -1;
while(posValue > 0){
//if((x - rect_width) >= 0){
array[start - (posValue * cols)] = color;
array[start - (posValue * cols)] = color;
//}
posValue--;
}
}
}
答案 0 :(得分:0)
我想也许你忘了改变&#34;否则如果&#34;这里的代码块:
不应该有&#34; startOther&#34;在那里,就像宽度的块一样?
if(rect_height > 0){
unsigned int drawIndex = 0;
while(drawIndex < rect_height){
//if((x + rect_width) < cols){
array[start + (drawIndex * cols)] = color;
array[startOther + (drawIndex * cols)] = color;
//}
drawIndex++;
}
} else if(rect_height < 0){ //if the height is negative
unsigned int posValue = rect_height * -1;
while(posValue > 0){
//if((x - rect_width) >= 0){
array[start - (posValue * cols)] = color;
array[start - (posValue * cols)] = color;
//}
posValue--;
}
}
即。 尝试更改此内容:
array[start - (posValue * cols)] = color;
array[start - (posValue * cols)] = color;
对此:
array[start - (posValue * cols)] = color;
array[startOther - (posValue * cols)] = color;