Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Graphics Algos/Car Animation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The car.c file is a program which shows an animation of a moving car.
It has been drawn using the line function in the graphics library.

line(x1,y1,x2,y2)
where, x1,y1 ---- starting points of line
and x2,y2 ------- ending points of the line.

The animation takes place by the concept of translation.
We've put the car in a for loop incrementing the values of x co-ordinates of all the line functions by i

The final out put will look this way!

![screenshot from 2017-12-16 15-52-31](https://user-images.githubusercontent.com/26206171/34069730-d0f448d6-e27c-11e7-9a34-17603028a4ec.png)
![screenshot from 2017-12-16 15-52-35](https://user-images.githubusercontent.com/26206171/34069731-d5130948-e27c-11e7-882e-2d9ca7c00f0b.png)
![screenshot from 2017-12-16 15-52-37](https://user-images.githubusercontent.com/26206171/34069732-d5d02c76-e27c-11e7-99ba-7138d97e4877.png)
![screenshot from 2017-12-16 15-52-40](https://user-images.githubusercontent.com/26206171/34069733-d80b1ae6-e27c-11e7-96b5-715bf9601b95.png)
73 changes: 73 additions & 0 deletions Graphics Algos/Car Animation/car.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <graphics.h>

int main()
{
int gd = DETECT, gm;
int i, maxx, midy;

/* initialize graphic mode */
initgraph(&gd, &gm, "NULL");
/* maximum pixel in horizontal axis */
maxx = getmaxx();
/* mid pixel in vertical axis */
midy = getmaxy()/2;

for (i=0; i < maxx-150; i=i+5) /* loop is run for showing the animation */
{
/* clears screen */
cleardevice();

/* draw a white road */
setcolor(WHITE); /* color of the road */
line(0, midy + 37, maxx, midy + 37);

/* Draw Car */
setcolor(YELLOW); /* color of the car body */

/* car body */
line(i, midy + 23, i, midy); /* draws the left most line parallel to y-axis */
line(i, midy, 40 + i, midy - 20);
line(40 + i, midy - 20, 80 + i, midy - 20); /* draws the roof of the car*/
line(80 + i, midy - 20, 100 + i, midy); /* draws the front mirror part of the car */
line(100 + i, midy, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
line(0 + i, midy + 23, 18 + i, midy + 23);
arc(30 + i, midy + 23, 0, 180, 12); /* draws the arc to accomodate the wheels */
line(42 + i, midy + 23, 78 + i, midy + 23);
arc(90 + i, midy + 23, 0, 180, 12); /* draws the arc to accomodate the wheels */
line(102 + i, midy + 23, 120 + i, midy + 23);
/* car body ends */

/* Draw Windows */
/* left window */
line(28 + i, midy, 43 + i, midy - 15); /* left part of left window */
line(43 + i, midy - 15, 57 + i, midy - 15); /* roof of the left window */
line(57 + i, midy - 15, 57 + i, midy); /* right part of left window */
line(57 + i, midy, 28 + i, midy); /* bottom part of the left window */
/* left window ends */

/* right window */
line(62 + i, midy - 15, 77 + i, midy - 15); /* left part of right window */
line(77 + i, midy - 15, 92 + i, midy); /* roof of the rightt window */
line(92 + i, midy, 62 + i, midy); /* right part of right window */
line(62 + i, midy, 62 + i, midy - 15); /* bottom part of the right window */
/* right window ends */

floodfill(5 + i, midy + 22, YELLOW); /* fills the whole car body with yellow color */

setcolor(BLUE); /* color of the wheels */
/* Draw Wheels */
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE); /* fills the left wheel with blue color */
floodfill(90 + i, midy + 25, BLUE); /* fills the right wheel with blue color */

/* Add delay of 0.1 milli seconds */
delay(100); /* to observe the animation */
}

getch();
closegraph();
return 0;
}