自定义drawRect()函数中是否有错误或错误?

时间:2011-08-01 11:38:18

标签: c function graphics

我的drawRect()函数表现得好像有错误一样。我试图确定以下是否存在错误,或者,如果不是我正在做错误的错误。

我正在尝试制作一个在顶部屏幕上显示矩形的功能。如果程序员输入“drawRect(3,3)”,则会创建一个3乘3的矩形。然而,如果程序员键入“drawRect(3,4)”,则显示矩形的右上角,然后显示无限长的顶部。有人能帮助我吗?这是我的代码:

#include <stdio.h>
#include <stdlib.h>

#define SIDES 0xB3
#define TOP_RIGHT 0xBF
#define BOTTOM_LEFT 0xC0
#define TOP_BOTTOM 0xC4
#define BOTTOM_RIGHT 0xD9
#define TOP_LEFT 0xDA

int heightloop;
int widthloop;

int displayrect(int height, int width)
{
printf("%c",TOP_LEFT);
for(widthloop=1;widthloop<width-2;width++)
{
    printf("%c",TOP_BOTTOM);
}
printf("%c\n",TOP_RIGHT);
for(heightloop=1;heightloop<height-2;height++)
{
    printf("%c",SIDES);
    for(widthloop=1;widthloop<width-2;width++)
    {
        printf(" ");
    }
    printf("%c\n",SIDES);
}

printf("%c",BOTTOM_LEFT);
for(widthloop=1;widthloop<width-2;width++)
{
    printf("%c",TOP_BOTTOM);
}
printf("%c",BOTTOM_RIGHT);
return(0);
}

2 个答案:

答案 0 :(得分:2)

在你的循环中,你应该增加widthloopheightloop而不是widthheightwidthloopheightloop也应该用0初始化。

答案 1 :(得分:0)

这样的循环:

for(widthloop=1;widthloop<width-2;width++)

for(heightloop=1;heightloop<height-2;height++)

width <= 3height <= 3时无效。

另请注意,widthloopheightloop应该是本地变量,虽然这不是本身的错误,只是“代码味道”。