C++ Program for 2D Transformations in Graphics

Classified in Computers

Written on in English with a size of 2.87 KB

C++ Program for 2D Transformations

Write your text here! #include<iostream>

#include<graphics.h>
using namespace std;

int main()
{
    int gd = DETECT,gm,ch;
    initgraph(&gd,&gm,NULL);
    cleardevice();
    long int x1 = 30,y1 = 30,x2 = 70,y2 = 70;
    int y = 2,x = 2;

    do
    {
        cout<<"\n2-D TRANSFORMATIONS";
        cout<<"\n1.Translation";
        cout<<"\n2.Rotation";
        cout<<"\n3.Scaling";
        cout<<"\n4.Exit";
        cout<<"\nEnter your choice ";
        cin>>ch;

        switch(ch)
        {
            case 1:
                long int tx,ty;
                cout<<"\nEnter x1 coordinate ";
                cin>>x1;
                cout<<"\nEnter y1 coordinate ";
                cin>>y1;
                cout<<"\nEnter x2 coordinate ";
                cin>>x2;
                cout<<"\nEnter y2 coordinate ";
                cin>>y2;
                cout<<"\nEnter tx to perform transformation on x-axis ";
                cin>>tx;
                cout<<"\nEnter ty to perform transformation on y-axis ";
                cin>>ty;
                cleardevice();
                cout<<"\nRectangle before transformation : ";
                rectangle(x1,y1,x2,y2);
                //getch();
                //cleardevice();
                cout<<"\nRectangle after transformation : ";
                rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
                //getch();
                break;
            case 2:
                //long int x1 = 100,y1 = 100,x2 = 300,y2 = 300;
                double xt,yt,d1;
                cleardevice();
                cout<<"\nEnter angle of rotation ";
                cin>>d1;
                d1 = (d1*3.14)/180.0;
                xt = x1 + ((x2-x1)*cos(d1) - (y2-y1)*sin(d1));
                yt = y1 + ((x2-x1)*sin(d1) + (y2-y1)*cos(d1));
                line(x1,y1,x2,y2);
                //getch();
                //cleardevice();
                cout<<"\nLine after rotation : \n";
                line(x1,y1,(int)xt,(int)yt);
                //getch();
                break;
            case 3:
                cleardevice();
                cout<<"\nRectangle before scaling ";
                rectangle(x1,y1,x2,y2);
                //getch();
                //cleardevice();
                cout<<"\nRectangle after scaling ";
                x1 = x1*x;
                y1 = y1*y;
                x2 = x2*x;
                y2 = y2*y;
                rectangle(x1,y1,x2,y2);
                getch();
                break;
            case 4:
                break;
            default :
                cout<<"\nWrong choice";

        }//switch

    }while(ch != 4);

    return 0;
}

Related entries: