Friday, 17 August 2018

Perform the following 2D Transformation operation Translation, Rotation and Scaling in C.


In many applications, changes in orientations, size, and shape are accomplished with geometric transformations that alter the coordinate descriptions of objects.
Basic geometric transformations are:
Translation
Rotation
Scaling
ALGORITHM:

1.      Start
2.      Initialize the graphics mode.
3.      Construct a 2D object  (use Drawpoly()) e.g. (x,y)
4.      A) Translation
     a.       Get the translation value tx, ty
     b.      Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
     c.       Plot (x’,y’)
5.      B)  Scaling
     a.       Get the scaling value Sx,Sy
     b.       Resize the object with Sx,Sy  (x’=x*Sx,y’=y*Sy)
     c.       Plot (x’,y’)
6.      C) Rotation
     a.       Get the Rotation angle
     b.      Rotate the object by the angle Ñ„
x’=x cos Ñ„ -  y sin Ñ„
                                    y’=x sin Ñ„  - y cosÑ„  
     c.       Plot (x’,y’)
Below is a C program of 2D Transformation operation Translation , Rotation 
and Scaling with output:-
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void translation();
void rotation();
void scalling();
void triangle();
void quadrant();
void main()
{
            int gm;
            int gd=DETECT;
            int x1,x2,x3,y1,y2,y3,c;
            initgraph(&gd,&gm,"");
            printf("\nEnter the point of triangle (x1,y1):");
            scanf("%d %d",&x1,&y1);
            printf("\nEnter the point of triangle (x2,y2):");
            scanf("%d %d",&x2,&y2);
            printf("\nEnter the point of triangle (x3,y3):");
            scanf("%d %d",&x3,&y3);
    clrscr();
    cleardevice();
    setbkcolor(0);
    triangle(x1,y1,x2,y2,x3,y3);
    while(1)
    {
    printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit \n");
    printf("\nEnter your choice:");
    scanf("%d",&c);
    clrscr();
    cleardevice();
    quadrant();
    switch(c)
    {
            case 1:
                                    triangle(x1,y1,x2,y2,x3,y3);
                                    translation(x1,y1,x2,y2,x3,y3);
                                    getch();
                                    break;
            case 2:
                                    triangle(x1,y1,x2,y2,x3,y3);
                                    rotation(x1,y1,x2,y2,x3,y3);
                                    getch();
                                    break;
            case 3:
                                    triangle(x1,y1,x2,y2,x3,y3);
                                    scalling(x1,y1,x2,y2,x3,y3);
                                    getch();
                                    break;
            case 4:
                                    exit(1);
                        break;
            default:
                        printf("!Enter the correct choice!");
    }
    clrscr();
    cleardevice();
    getch();
   }
}
void quadrant()
{
    line(320,0,320,479);
    line(0,240,639,240);
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
            line(x1,y1,x2,y2);
            line(x2,y2,x3,y3);
            line(x3,y3,x1,y1);
}
void translation(int x1,int y1,int x2,int y2,int x3,int y3)
{
            int nx1,nx2,nx3,ny1,ny2,ny3;
            int xt,yt;
            printf("\n Enter the translation factor xt & yt:");
                        scanf("%d %d",&xt,&yt);
                        cleardevice();
                        quadrant();
                        nx1=x1+xt;
                        ny1=y1+yt;
                        nx2=x2+xt;
                        ny2=y2+yt;
                        nx3=x3+xt;
                        ny3=y3+yt;
                        line(nx1,ny1,nx2,ny2);
                        line(nx2,ny2,nx3,ny3);
                        line(nx3,ny3,nx1,ny1);
}
void rotation(int x1,int y1,int x2,int y2,int x3,int y3)
{
            int nx1,nx2,nx3,ny1,ny2,ny3;
            float t,r;
            printf("\n Enter the angle of rotation :");
                        scanf("%f",&r);
                        cleardevice();
                        quadrant();
                        t=(r*(3.14))/180;
                        nx1=x2+((x1-x2)*cos(t)-(y1-y2)*sin(t));
                        ny1=y2+((x1-x2)*sin(t)+(y1-y2)*cos(t));

                        nx2=x2+((x2-x2)*cos(t)-(y2-y2)*sin(t));
                        ny2=y2+((x2-x2)*sin(t)+(y2-y2)*cos(t));
                        nx3=x2+((x3-x2)*cos(t)-(y3-y2)*sin(t));
                        ny3=y2+((x3-x2)*sin(t)+(y3-y2)*cos(t));
                        line(nx1,ny1,nx2,ny2);
                        line(nx2,ny2,nx3,ny3);
                        line(nx3,ny3,nx1,ny1);
}
void scalling(int x1,int y1,int x2,int y2,int x3,int y3)
{
            int nx1,nx2,nx3,ny1,ny2,ny3;
            float sx,sy;
            printf("\n Enter the scalling factor sx & sy:");
                        scanf("%f %f",&sx,&sy);
                        cleardevice();
                        quadrant();
                        nx1=x1*sx;
                        ny1=y1*sy;
                        nx2=x2*sx;
                        ny2=y2*sy;
                        nx3=x3*sx;
                        ny3=y3*sy;
                        line(nx1,ny1,nx2,ny2);
                        line(nx2,ny2,nx3,ny3);
                        line(nx3,ny3,nx1,ny1);
}
Output:
 

 

 
                                            (choice 1) 

 
                                  (after translation)

 
                                        (choice 2) 

 
                                  (after rotation)

 
                                         (choice 3) 

 
                                       (after scaling)
  
THANK YOU!!!!

No comments:

Post a Comment