2D – Transformations

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void trans(int,int,int,int);
void rot(int,int,int,int);
void scale(int,int,int,int);

void main()
{
    int x1,x2,y1,y2,gd=DETECT,gm,x;
    clrscr();
    initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
    printf("Enter initial coordinates of rectangle ");
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    do
    {
        rectangle(x1,y1,x2,y2);
        printf("Enter\n1.Translation\n2.Rotation\n3.Scaling\n4.Exit\n");
        scanf("%d",&x);
        switch(x)
        {
            case 1:
                trans(x1,y1,x2,y2);
                break;
            case 2:
                rot(x1,y1,x2,y2);
                break;
            case 3:
                scale(x1,y1,x2,y2);
                break;
            case 4:
                printf("Exit");
                break;
        }
    }while(x!=4);
    closegraph();
    getch();
}

void trans(int x1,int y1,int x2,int y2)
{
    int x,y;
    printf("Enter x and y to translate ");
    scanf("%d%d",&x,&y);
    rectangle(x1+x,y1+y,x2+x,y2+y);
}

void rot(int x1,int y1,int x2,int y2)
{
    float deg,rad;
    int x3,x4,y3,y4;
    printf("Enter degrees to rotate ");
    scanf("%f",&deg);
    rad=3.14*deg/180;
    x3=x1*cos(rad)-y1*sin(rad);
    y3=x1*sin(rad)+y1*cos(rad);
    x4=x2*cos(rad)-y2*sin(rad);
    y4=x2*sin(rad)+y2*cos(rad);
    rectangle(x3,y3,x4,y4);
}

void scale(int x1,int y1,int x2,int y2)
{
    float x,y;
    printf("Enter x and y to scale ");
    scanf("%f%f",&x,&y);
    rectangle(x1*x,y1*y,x2*x,y2*y);
}

Output
Transformation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s