在C语言中,可以使用多种方法来绘制矩形。以下是几种常见的方法:
方法一:使用`lineto`函数
```c
include
int main() {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc");
cleardevice();
moveto(160, 120);
lineto(480, 120);
lineto(160, 360);
lineto(480, 360);
getch();
closegraph();
return 0;
}
```
方法二:使用`rectangle`函数
```c
include
int main() {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc");
cleardevice();
rectangle(160, 120, 480, 360);
getch();
closegraph();
return 0;
}
```
方法三:使用EasyX图形库
```c
include
int main() {
initgraph(640, 480);
rectangle(100, 100, 300, 200);
getch();
closegraph();
return 0;
}
```
方法四:使用Turbo C的`putpixel`和`line`函数
```c
include include int main() { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "c:\\tc"); cleardevice(); setcolor(RED); putpixel(100, 100, RED); putpixel(300, 100, RED); putpixel(300, 200, RED); putpixel(100, 200, RED); getch(); closegraph(); return 0; } ``` 方法五:使用EasyX图形库的`drawShapes`函数 ```c include void drawShapes() { rectangle(100, 100, 300, 200); } int main() { initgraph(640, 480); drawShapes(); getch(); closegraph(); return 0; } ``` 建议 根据你的开发环境和需求选择合适的图形库,如`graphics.h`(适用于Turbo C)或EasyX图形库(适用于Windows系统)。 在使用任何绘图函数之前,确保已经正确初始化了图形系统。 根据需要设置绘图颜色和线型,以便绘制出所需的矩形效果。 在每次绘制新图形之前,使用`cleardevice()`函数清屏,以确保图形显示正确。 通过以上方法,你可以在C语言中轻松绘制矩形。选择哪种方法取决于你的具体需求和开发环境。选择合适的图形库:
初始化图形系统:
设置颜色和线型:
清屏: