Skip to content

Commit 15f786f

Browse files
authored
Merge pull request #238 from rahulkumaran/rahulkumaran-graphics_algo
C program for Car Animation
2 parents 06976f3 + 2043f49 commit 15f786f

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The car.c file is a program which shows an animation of a moving car.
2+
It has been drawn using the line function in the graphics library.
3+
4+
line(x1,y1,x2,y2)
5+
where, x1,y1 ---- starting points of line
6+
and x2,y2 ------- ending points of the line.
7+
8+
The animation takes place by the concept of translation.
9+
We've put the car in a for loop incrementing the values of x co-ordinates of all the line functions by i
10+
11+
The final out put will look this way!
12+
13+
![screenshot from 2017-12-16 15-52-31](https://user-images.githubusercontent.com/26206171/34069730-d0f448d6-e27c-11e7-9a34-17603028a4ec.png)
14+
![screenshot from 2017-12-16 15-52-35](https://user-images.githubusercontent.com/26206171/34069731-d5130948-e27c-11e7-882e-2d9ca7c00f0b.png)
15+
![screenshot from 2017-12-16 15-52-37](https://user-images.githubusercontent.com/26206171/34069732-d5d02c76-e27c-11e7-99ba-7138d97e4877.png)
16+
![screenshot from 2017-12-16 15-52-40](https://user-images.githubusercontent.com/26206171/34069733-d80b1ae6-e27c-11e7-96b5-715bf9601b95.png)

Graphics Algos/Car Animation/car.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <stdio.h>
2+
#include <graphics.h>
3+
4+
int main()
5+
{
6+
int gd = DETECT, gm;
7+
int i, maxx, midy;
8+
9+
/* initialize graphic mode */
10+
initgraph(&gd, &gm, "NULL");
11+
/* maximum pixel in horizontal axis */
12+
maxx = getmaxx();
13+
/* mid pixel in vertical axis */
14+
midy = getmaxy()/2;
15+
16+
for (i=0; i < maxx-150; i=i+5)/* loop is run for showing the animation */
17+
{
18+
/* clears screen */
19+
cleardevice();
20+
21+
/* draw a white road */
22+
setcolor(WHITE);/* color of the road */
23+
line(0, midy + 37, maxx, midy + 37);
24+
25+
/* Draw Car */
26+
setcolor(YELLOW);/* color of the car body */
27+
28+
/* car body */
29+
line(i, midy + 23, i, midy);/* draws the left most line parallel to y-axis */
30+
line(i, midy, 40 + i, midy - 20);
31+
line(40 + i, midy - 20, 80 + i, midy - 20);/* draws the roof of the car*/
32+
line(80 + i, midy - 20, 100 + i, midy);/* draws the front mirror part of the car */
33+
line(100 + i, midy, 120 + i, midy);
34+
line(120 + i, midy, 120 + i, midy + 23);
35+
line(0 + i, midy + 23, 18 + i, midy + 23);
36+
arc(30 + i, midy + 23, 0, 180, 12);/* draws the arc to accomodate the wheels */
37+
line(42 + i, midy + 23, 78 + i, midy + 23);
38+
arc(90 + i, midy + 23, 0, 180, 12);/* draws the arc to accomodate the wheels */
39+
line(102 + i, midy + 23, 120 + i, midy + 23);
40+
/* car body ends */
41+
42+
/* Draw Windows */
43+
/* left window */
44+
line(28 + i, midy, 43 + i, midy - 15);/* left part of left window */
45+
line(43 + i, midy - 15, 57 + i, midy - 15);/* roof of the left window */
46+
line(57 + i, midy - 15, 57 + i, midy);/* right part of left window */
47+
line(57 + i, midy, 28 + i, midy);/* bottom part of the left window */
48+
/* left window ends */
49+
50+
/* right window */
51+
line(62 + i, midy - 15, 77 + i, midy - 15);/* left part of right window */
52+
line(77 + i, midy - 15, 92 + i, midy);/* roof of the rightt window */
53+
line(92 + i, midy, 62 + i, midy);/* right part of right window */
54+
line(62 + i, midy, 62 + i, midy - 15);/* bottom part of the right window */
55+
/* right window ends */
56+
57+
floodfill(5 + i, midy + 22, YELLOW);/* fills the whole car body with yellow color */
58+
59+
setcolor(BLUE);/* color of the wheels */
60+
/* Draw Wheels */
61+
circle(30 + i, midy + 25, 9);
62+
circle(90 + i, midy + 25, 9);
63+
floodfill(30 + i, midy + 25, BLUE);/* fills the left wheel with blue color */
64+
floodfill(90 + i, midy + 25, BLUE);/* fills the right wheel with blue color */
65+
66+
/* Add delay of 0.1 milli seconds */
67+
delay(100);/* to observe the animation */
68+
}
69+
70+
getch();
71+
closegraph();
72+
return 0;
73+
}

0 commit comments

Comments
 (0)