Sunday, 1 July 2012

program to draw a wheels using c++.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define round(a) ((int)(0.5+a))
#define radian(x) (3.14*x/180.0)

void rotate(double r,int xc,int yc,int *crl)
{
    int temp,i;
    for(i=0;i<16;i++)
    {
        if(i%2==0)
        {
            temp=crl[i];
            crl[i]=round(xc+(crl[i]-xc)*cos(radian(30))-(crl[i+1]-yc)*sin(radian(30)));
        }
        else
            crl[i]=round(yc+(temp-xc)*sin(radian(30))+(crl[i]-yc)*cos(radian(30)));
    }
        return;
}

void main(void)
{
    int gd,gm,i,xc,yc,crl[16];
    double theta,r;
    clrscr();
    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,"c:\\tc\\bgi");
    printf("Enter the Center of the wheel:");
    scanf("%d %d",&xc,&yc);
    printf("Enter the Radius of the wheel:");
    scanf("%lf",&r);
    setbkcolor(WHITE);
    setcolor(RED);
    circle(xc,yc,round(r));
    crl[0]=xc;
    crl[1]=yc+round(r);
    crl[2]=xc;
    crl[3]=yc-round(r);
    for(i=4;i<16;i++)
    {
        if(i%2==0)
            crl[i]=xc+(crl[i-4]-xc)*cos(radian(45))-(crl[i-3]-yc)*sin(radian(45));
        else
            crl[i]=yc+(crl[i-4]-xc)*sin(radian(45))+(crl[i-5]-yc)*cos(radian(45));
    }
    line(crl[0],crl[1],crl[2],crl[3]);
    line(crl[4],crl[5],crl[6],crl[7]);
    line(crl[8],crl[9],crl[10],crl[11]);
    line(crl[12],crl[13],crl[14],crl[15]);
    i=0;

    while(i>=0)
    {
        rotate(r,xc,yc,crl);
        delay(100);
        cleardevice();
        circle(xc,yc,round(r));
        line(crl[0],crl[1],crl[2],crl[3]);
        line(crl[4],crl[5],crl[6],crl[7]);
        line(crl[8],crl[9],crl[10],crl[11]);
        line(crl[12],crl[13],crl[14],crl[15]);
    }
    getch();
    closegraph();
}