C Program to move a Car – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 20 Aug 2022 18:18:28 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg C Program to move a Car – Programmerbay https://programmerbay.com 32 32 C Program to Move a Car https://programmerbay.com/program-to-move-a-car-in-c/ https://programmerbay.com/program-to-move-a-car-in-c/#respond Wed, 03 Aug 2022 07:54:18 +0000 https://www.programmerbay.com/?p=1961 In this article, we’ll be implementing moving car program in computer graphics. Using translation transformation, we can draw and move an object to another coordinate. The below code is a moving car program written in C.

C Program to move a car in computer graphics

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gm,gd=DETECT;
int i= 0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(i<=800)
{
line(0,300,800,300); // Path
//Body of the car
line(50+i,220,100+i,220);
line(50+i,220,30+i,250);
line(100+i,220,120+i,250);
rectangle(0+i,250,160+i,270);
// Tyres of the car
circle(30+i,285,12);
circle(130+i,285,12);
if(i>=800)
{
break;
}
i=i+2;
clearviewport(); // clearing image which would make illusion of moving car
}
getch();
closegraph();

}

Explanation: 

In the above code, we have drawn a car and a path on which it would be running. We used, a line method to create a path, 1 rectangle and 3 line methods to build the upper body of a car and two circle method for its tyres.

Using translation transformation, we were able to make an illusion of a moving car. And lastly, clearviewport() method used to clear a previously generated view.

Output:

movingcar

]]>
https://programmerbay.com/program-to-move-a-car-in-c/feed/ 0