Tuesday, 26 June 2012

c++ program to display moving car.

#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#define ESC_KEY   0x1b

class wheel
{
int r,x,y,nx,ny,cx,cy;
float rd;
public:
wheel(int x2,int y2,int x1,int y1,int d)
{
cx=x2;
cy=y2;
x=x1;
y=y1;
rd=d*(3.14/180);
 generate();
}



void generate();
int getx()
{ return(nx);
}
int gety()
{ return(ny);
}

};
 void wheel::generate()
{
    x=x-cx;
        y=y-cy;
        nx=(x*cos(rd)-y*sin(rd))+cx;
        ny=(x*sin(rd)+y*cos(rd))+cy;
}

void main()
{
    void get_point(int,int,int,int,int);
    void car(int,int);
    void wheel_motion(int,int,int,int);
    int i=20,j,k;
    int gdriver=DETECT ,gmode=1;
    initgraph(&gdriver,&gmode,"..\\bgi");
    int r;
    float rd;
    int road[8];
    road[0]=0;
    road[1]=220;
    road[2]=getmaxx();
    road[3]=220;
    road[4]=getmaxx();
    road[5]=240;
    road[6]=0;
    road[7]=240;

  a:
    if (r>360) r=0;
    if (i>650+15) i=-150;
        cleardevice();
    car(i+15,200);
    wheel w1(i+15,200,i,200,r);
    wheel_motion(i+15,200,w1.getx(),w1.gety());

    wheel w2(i+115,200,i+100,200,r);
    wheel_motion(i+115,200,w2.getx(),w2.gety());

        i=i+1;
        r=r+1;
        setfillstyle(10, 3);

        fillpoly(4, road);

        while(!kbhit())
        {

            delay(20);
            goto a;
        }
        if (getch()==ESC_KEY)
        {
            exit(1);
        }
        else
        goto a;


}
void get_point(int x,int y,int cx, int cy,int r)
{
    int nx,ny;
    float rd;
        x=x-cx;
        y=y-cy;

        rd=r*(3.14/180);

        nx=x*cos(rd)-y*sin(rd);
        ny=x*sin(rd)+y*cos(rd);

        nx=nx+cx;
        ny=ny+cy;
        x=x+cx;
        y=y+cy;
        line(nx,ny,cx,cy);
}
void car(int x,int y)
{
int nx;
nx=x+100;
circle(x,y,20);
circle(x,y,15);
circle(nx,y,20);
circle(nx,y,15);
arc(x,y,0,180,22);
arc(nx,y,0,180,22);
line(x+22,y,nx-22,y);///0-0
line(x-22,y,x-45,y);///-0
line(nx+22,y,nx+45,y);///0-
line(x-45,y,x-45,y-10);///0<->[
//arc(200-23,200-10-10,105,180,22);
arc(x-13,y-10,97,180,32);///0<->[
//line(200+25-42,200-40,200-13-13,200-10-32);
arc(x+25,y-40,90,180,42);///[<->/

int dx=190;
///rear
line(x-45+dx,y,x-45+dx,y-10);//RIGHT SIDE LINE TO CONECT ARK
arc(x-13+dx-64,y-10,0,90-3,32);//ARK OF RIGHT SIDE

arc(x+25+dx-58-84,y-40,0,90,42);//ARK OF RIGHT SIDE TOP

line(x+25+dx-58-84,y-40-42,x-13+38,y-40-42);/// /-\

}

void wheel_motion(int cx,int cy,int nx,int ny)
{        nx=nx-cx;
        ny=ny-cy;
        line(nx+cx,ny+cy,cx,cy);
        putpixel(nx+cx,ny+cy,2);
        get_point(nx+cx,ny+cy,cx,cy,45);
        putpixel(-ny+cx,nx+cy,2);
        line(-ny+cx,nx+cy,cx,cy);
        get_point(-ny+cx,nx+cy,cx,cy,45);

        putpixel(-nx+cx,ny+cy,2);
        putpixel(-nx+cx,-ny+cy,2);
        line(-nx+cx,-ny+cy,cx,cy);
        get_point(-nx+cx,-ny+cy,cx,cy,45);


        putpixel(ny+cx,-nx+cy,2);
        line(ny+cx,-nx+cy,cx,cy);
        get_point(ny+cx,-nx+cy,cx,cy,45);
}


No comments:

Post a Comment