2 D Transformation !!

2 D Transformation in Computer Graphics


Scaling
A scaling transformation alters size of an object. In the scaling process, we either compress or expand the dimension of the object.
Scaling operation can be achieved by multiplying each vertex coordinate (x, y) of the polygon by scaling factor s
x and sy to produce the transformed coordinates as (x’, y’).
So,
 x’ = x * sx and y’ = y * sy.

The scaling factor s
x, sy scales the object in X and Y direction respectively. So, the above equation can be represented in matrix form:


Note: If the scaling factor S is less than 1, then we reduce the size of the object. If the scaling factor S is greater than 1, then we increase size of the object.

Translation

A translation moves an object to a different position on the screen. You can translate a point in 2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new coordinate (X’, Y’).


From the above figure, you can write that −
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector.
We can write it as −
P’ = P + T

Rotation

In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will get a new point P’ (X’, Y’).
x = xcosθ ysinθ
y = xsinθ + ycosθ

//2-D Transformation Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
                int driver,mode,ch,ch1;
                int x1,y1,x2,y2,x3,y3,midx,midy,tx,ty;
                double ang,xr,yr,sx,sy,sf;
                clrscr();
                driver = DETECT;
                initgraph(&driver,&mode," ");
                midx=(getmaxx()/2);
                midy=(getmaxy()/2);
                line(0,midy,getmaxx(),midy);
                line(midx,0,midx,getmaxy());
                printf("Enter the coordinates of the triangle:");
                scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
                line(x1+midx,midy-y1,x2+midx,midy-y2);
                line(x1+midx,midy-y1,x3+midx,midy-y3);
                line(x2+midx,midy-y2,x3+midx,midy-y3);
                printf("\n1.Translation 2.Rotation 3.Scaling 4.Reflection 5.Shearing");
                printf("\nEnter the choice:");
                scanf("%d",&ch);
                switch(ch)
                {
                                case 1:
                                                printf("Enter the translation factor");
                                                scanf("%d%d",&tx,&ty);
                                                x1+=tx;y1+=ty;
                                                x2+=tx;y2+=ty;
                                                x3+=tx;y3+=ty;
                                                break;
                                case 2:
                                                printf("\nEnter the rotation angle");
                                                scanf("%lf",&ang);
                                                ang=(ang*3.14)/180;
                                                printf("\nEnter the reference points:");
                                                scanf("%lf%lf",&xr,&yr);
                                                x1=xr+((x1-xr)*cos(ang))-((y1-yr)*sin(ang));
                                                x2=xr+((x2-xr)*cos(ang))-((y2-yr)*sin(ang));
                                                x3=xr+((x3-xr)*cos(ang))-((y3-yr)*sin(ang));
                                                y1=yr+((x1-xr)*sin(ang))+((y1-yr)*cos(ang));
                                                y2=yr+((x2-xr)*sin(ang))+((y2-yr)*cos(ang));
                                                y3=yr+((x3-xr)*sin(ang))+((y3-yr)*cos(ang));
                                                break;
                                case 3:
                                                printf("Enter the scaling factor");
                                                scanf("%lf%lf",&sx,&sy);
                                                printf("\nEnter the reference points:");
                                                scanf("%lf%lf",&xr,&yr);
                                                x1=xr+(x1-xr)*sx;y1=yr+(y1-yr)*sy;
                                                x2=xr+(x2-xr)*sx;y2=yr+(y2-yr)*sy;
                                                x3=xr+(x3-xr)*sx;y3=yr+(y3-yr)*sy;
                                                break;
                                case 4:
                                                printf("\n1.reflection about x axis 2.reflection about y axis 3.reflection about x=y axis");
                                                scanf("%d",&ch1);
                                                if(ch1==1)
                                                {
                                                                y1=-y1;
                                                                y2=-y2;
                                                                y3=-y3;
                                                }
                                                else if(ch1==2)
                                                {
                                                                x1=-x1;
                                                                x2=-x2;
                                                                x3=-x3;
                                                }
                                                else
                                                {
                                                                x1=-x1;y1=-y1;
                                                                x2=-x2;y2=-y2;
                                                                x3=-x3;y3=-y3;
                                                }
                                                break;
                                case 5:
                                                printf("\n1.shear relative to x-axis 2.shear relative to y axis");
                                                scanf("%d",&ch1);
                                                if(ch1==1)
                                                {
                                                                printf("Enter the shear factor");
                                                                scanf("%d",&sf);
                                                                x2=x2+sf*(y2);
                                                                x3=x3+sf*(y3);
                                                }
                                                else
                                                {
                                                                printf("Enter the shear factor");
                                                                scanf("%d",&sf);
                                                                y2=y2+sf*(x2);
                                                                y3=y3+sf*(x3);
                                                }
                                                break;
                                default:
                                                printf("Wrong choice");
                }
                line(x1+midx,midy-y1,x2+midx,midy-y2);
                line(x1+midx,midy-y1,x3+midx,midy-y3);
                line(x2+midx,midy-y2,x3+midx,midy-y3);
                getch();
                closegraph();
}

Output:





Comments