在计算机图形学中,使用graphics.h提供直接功能以绘制不同的坐标形状(例如圆形,矩形等)。通过使用这些功能,我们可以绘制不同的对象,例如汽车,小屋,树木等。在此程序中,我们将使用直线和圆来绘制移动的汽车。
程序中使用的功能:
- delay(n):此函数用于将程序输出保留一小段时间,因为处理速度非常快,因此请使用它来查看结果。
- setcolor(n): graphics.h头文件中的函数,用于设置指针(光标)的颜色。计算机图形中有一些预定义的颜色。这里n是色号。
- line(x1,y1,x2,y2):来自graphics.h头文件的函数,该函数绘制一条线,其中(x1,y1)为该线的第一坐标,而(x2,y2)为该线的第二坐标。
- circle(x,y,r):来自graphics.h头文件的函数,该函数绘制一个以中心(x,y)和半径r为中心的圆。
示例1:本示例不使用cleardevice()方法创建移动的汽车。
// C program to draw a moving car. This
// program run in gcc compiler having
// graphics.h library installed
#include
#include
// Function to draw moving car
void draw_moving_car(void) {
int i, j = 0, gd = DETECT, gm;
// Passed three arguments to initgraph
// function to initialize graphics mode
initgraph(&gd, &gm, "");
for (i = 0; i <= 420; i = i + 10) {
// Set color of car as red
setcolor(RED);
// Thease lines for bonnet and
// body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
// For left wheel of car
circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
// For right wheel of car
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);
// Line left of left wheel
line(0 + i, 330, 50 + i, 330);
// Line middle of both wheel
line(80 + i, 330, 130 + i, 330);
// Line right of right wheel
line(210 + i, 330, 160 + i, 330);
delay(100);
// To erase previous drawn car, draw
// the whole car at same possition
// but color using black
setcolor(BLACK);
// Lines for bonnet and body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
// For left wheel of car
circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
// For right wheel of car
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);
// Line left of left wheel
line(0 + i, 330, 50 + i, 330);
// Line middle of both wheel
line(80 + i, 330, 130 + i, 330);
// Line right of right wheel
line(210 + i, 330, 160 + i, 330);
}
getch();
closegraph();
}
// Driver code
int main()
{
draw_moving_car();
return 0;
}
输出:
示例2:本示例使用cleardevice()方法清除屏幕。
// C program to draw a moving car. This
// program run in gcc compiler having
// graphics.h library installed
#include
#include
// Function to draw moving car
void draw_moving_car(void) {
int i, j = 0, gd = DETECT, gm;
// Passed three arguments to initgraph
// function to initialize graphics mode
initgraph(&gd, &gm, "");
for (i = 0; i <= 420; i = i + 10) {
// Set color of car as red
setcolor(RED);
// Thease lines for bonnet and
// body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
// For left wheel of car
circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
// For right wheel of car
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);
// Line left of left wheel
line(0 + i, 330, 50 + i, 330);
// Line middle of both wheel
line(80 + i, 330, 130 + i, 330);
// Line right of right wheel
line(210 + i, 330, 160 + i, 330);
delay(100);
// To erase previous drawn car
// use cleardevice() function
cleardevice();
}
getch();
closegraph();
}
// Driver code
int main()
{
draw_moving_car();
return 0;
}
输出: