Friday 29 June 2012

c++ program that performed various operation like rotation,scaling and transfrmation on polygon.

   #include<graphics.h>
    #include<conio.h>
    #include<iostream.h>
    #include<math.h>
    class polygon
    {
    public:
    float x1,y1,x2,y2,x3,y3,x11,y11,x22,y22,x33,y33,a1;
    float a[3][3];
 


  float b[3][3];
    void scalling();
    void translation();
    void rotation();
    };

    void main()
    {
        int gdrive=DETECT,gmode;
        initgraph(&gdrive,&gmode,"");
        polygon p;
        cleardevice();
        setcolor(YELLOW);
        line(320,0,320,480);
        line(0,240,640,240);
        cout<<"**MAIN MENU**";
        cout<<"\n[1] SCALLING.";
        cout<<"\n[2] TRASLATION.";
        cout<<"\n[3] ROTATION.";
        int r;
        cout<<"\n ENTER CHOICE:";
        cin>>r;
        if(r==1)
        {
         p.scalling();
        }
        else if(r==2)
        {
         p.translation();
        }
        else if(r==3)
        {
          p.rotation();
        }
    getch();
    }

      void polygon::scalling()
    {
    x1=60;y1=40;x2=160;y2=40;x3=110;y3=140;
    setcolor(9);
    line(320+x1,240-y1,320+x2,240-y2);
    line(320+x2,240-y2,320+x3,240-y3);
    line(320+x3,240-y3,320+x1,240-y1);
    float sx,sy,xc,yc;
    cout<<"ENTER sx & sy :";
    cin>>sx>>sy;
    xc=(x1+x2+x3)/3;
    yc=(y1+y2+y3)/3;
    a[1][1]=sx;
    a[2][2]=sy;
    a[1][2]=0;
    a[1][3]=(-(xc*sx)+xc);
    a[2][1]=0;
    a[2][3]=(-(yc*sy)+yc);
    a[3][1]=a[3][2]=0;
    a[3][3]=1;
    x11=sx*x1+a[1][3]*1;
    y11=sy*y1+a[2][3]*1;
    x22=sx*x2+a[1][3]*1;
    y22=sy*y2+a[2][3]*1;
    x33=sx*x3+a[1][3]*1;
    y33=sy*y3+a[2][3]*1;
    setcolor(BLUE);
    line(320+x11,240-y11,320+x22,240-y22);
    line(320+x22,240-y22,320+x33,240-y33);
    line(320+x33,240-y33,320+x11,240-y11);
    getch();
    }

    void polygon::translation()
     {
    int tx,ty;
    x1=50;y1=50;x2=150;y2=50;x3=100;y3=150;
    setcolor(9);
    line(320+x1,240-y1,320+x2,240-y2);
    line(320+x2,240-y2,320+x3,240-y3);
    line(320+x3,240-y3,320+x1,240-y1);
    cout<<"ENTER tx & ty :";
    cin>>tx>>ty;
    a[1][1]=a[2][2]=a[3][3]=1;
    a[1][2]=a[2][1]=0;
    a[3][1]=a[3][2]=0;
  a[1][3]=tx;
  a[2][3]=ty;
  x11=x1+tx;
  y11=y1+ty;
  x22=x2+tx;
  y22=y2+ty;
  x33=x3+tx;
  y33=y3+ty;
  setcolor(BLUE);
  line(320+x11,240-y11,320+x22,240-y22);
  line(320+x22,240-y22,320+x33,240-y33);
  line(320+x33,240-y33,320+x11,240-y11);
 }
 void polygon::rotation()
  {
    int xr,yr,g;
    x1=50;y1=50;x2=150;y2=50;x3=100;y3=150;
    setcolor(9);
    line(320+x1,240-y1,320+x2,240-y2);
    line(320+x2,240-y2,320+x3,240-y3);
    line(320+x3,240-y3,320+x1,240-y1);
    cout<<"ENTER ANGLE :";
    cin>>g;
    cout<<"ENTER xr & yr :";
    cin>>xr>>yr;
    a1=g*(3.14/180);
    b[1][1]=cos(a1);
    b[1][2]=-sin(a1);
    b[1][3]=-(xr*cos(a1))+(yr*sin(a1))+xr;
    b[2][1]=sin(a1);
    b[2][2]=cos(a1);
    b[2][3]=-(xr*sin(a1))-(yr*cos(a1))+yr;
    b[3][1]=b[3][2]=0;
    b[3][3]=1;
    x11=(b[1][1]*x1)+(b[1][2]*y1)+(b[1][3]*1);
    y11=(b[2][1]*x1)+(b[2][2]*y1)+(b[2][3]*1);
    x22=(b[1][1]*x2)+(b[1][2]*y2)+(b[1][3]*1);
    y22=(b[2][1]*x2)+(b[2][2]*y2)+(b[2][3]*1);
    x33=(b[1][1]*x3)+(b[1][2]*y3)+(b[1][3]*1);
    y33=(b[2][1]*x3)+(b[2][2]*y3)+(b[2][3]*1);
    setcolor(BLUE);
    line(320+x11,240-y11,320+x22,240-y22);
    line(320+x22,240-y22,320+x33,240-y33);
    line(320+x33,240-y33,320+x11,240-y11);
  }

No comments:

Post a Comment