2d transformation(exp-5) in computer graphics PDF

Title 2d transformation(exp-5) in computer graphics
Author CEB429 NIRANJAN KHEDKAR
Course Computer Graphics
Institution University of Mumbai
Pages 8
File Size 275.2 KB
File Type PDF
Total Downloads 108
Total Views 253

Summary

2d transformation(exp-5)in computer graphics mumbai university sem -3...


Description

EXPERIMENT-5 NAME: NIRANJAN MANGESH KHEDKAR DIV-B ROLL NO-26 GRADE: SIGN:

AIM: To apply the basic 2D transformations such as translation, Scaling, Rotation, shearing and reflection for a given 2D object. THEORY: We have to perform 2D transformations on 2D objects. Here we perform transformations on a line segment. The 2D transformations are: 1. Translation 2. Scaling 3. Rotation 4. Reflection 5. Shear 1. Translation: Translation is defined as moving the object from one position to another position along straight line path.

We can move the objects based on translation distances along x and y axis. tx denotes translation distance along x-axis and ty denotes translation distance along y axis.

2. Scaling: scaling refers to changing the size of the object either by increasing or decreasing. We will increase or decrease the size of the object based on scaling factors along x and yaxis.

If (x, y) are old coordinates of object, then new coordinates of object after applying scaling transformation are obtained as: x’=x*sx y’=y*sy 3. Rotation: A rotation repositions all points in an object along a circular path in the plane centered at the pivot point. We rotate an object by an angle theta. New coordinates after rotation depend on both x and y • x’ = xcosθ -y sinθ • y’ = xsinθ+ ycosθ

4. Reflection: Reflection is nothing but producing mirror image of an object. Reflection can be done just by rotating the object about given axis of reflection with an angle of 180 degrees.

5.Shear: 1. Shear is the translation along an axis by an amount that increases linearly with another axis (Y). It produces shape distortions as if objects were composed of layers that are caused to slide over each other. 2. Shear transformations are very useful in creating italic letters and slanted letters from regular letters. 3. Shear transformation changes the shape of the object to a slant position.

4.Shear transformation is of 2 types: a.X-shear: changing x-coordinate value and keeping y constant x’=x+shx*y y’=y b.Y-shear: changing y coordinates value and keeping x constant x’=x y’=y+shy*x shx and shy are shear factors along x and y-axis.

PROGRAM: #include #include #include #include #include int roundno(float num){ return num < 0 ? num - 0.5 : num + 0.5; } void scaling(int x1, int y1, int x2, int y2, int x3, int y3) {

int x, y; int newx1, newy1, newx2, newy2, newx3, newy3; printf("Enter scaling co-ordinates (x, y): "); scanf("%d%d",&x,&y); // (- x1) (-y1) to shift back to starting co-ordinate newx1 = (x1*x) - x1; newy1 = (y1*y) - y1; newx2 = (x2*x) - x1; newy2 = (y2*y) - y1;

newx3 = (x3*x) - x1; newy3 = (y3*y) - y1; cleardevice(); printf("Line after scaling"); line(newx1, newy1,newx2, newy2); line(newx2, newy2,newx3, newy3); line(newx3, newy3, newx1, newy1); } void rotation(int x1, int y1, int x2, int y2, int x3,int y3){ int newx1, newy1, newx2, newy2, newx3, newy3, r; float t, sine, cose; printf("\nEnter the angle for rotation: "); scanf("%d",&r); t=3.14*r/180; // shifting to a reference point x1 -= 100; y1 -= 100; x2 -= 100; y2 -= 100; x3 -= 100; y3 -= 100; sine = sin(t); cose = cos(t); newx1 = roundno((x1*cose) - (y1*sine)); newy1 = roundno((x1*sine) + (y1*cose)); newx2 = roundno((x2*cose) - (y2*sine)); newy2 = roundno((x2*sine) + (y2*cose)); newx3 = roundno((x3*cose) - (y3*sine)); newy3 = roundno((x3*sine) + (y3*cose));

// cause anticlockwise spin // newx1 -= 100; // newy1 -= 100; // newx2 -= 100; // newy2 -= 100; // newx3 -= 100; // newy3 -= 100; line(newx1,newy1,newx2,newy2); line(newx2,newy2,newx3,newy3); line(newx3,newy3,newx1,newy1); delay(50); }

void translation(int x1, int y1, int x2, int y2, int x3, int y3){ int x, y, newx1, newy1, newx2, newy2, newx3, newy3; printf("Enter translation co-ordinates (x, y): "); scanf("%d%d",&x,&y); newx1=x1+x; newy1=y1+y; newx2=x2+x; newy2=y2+y; newx3=x3+x; newy3=y3+y; printf("After translation:\n"); line(newx1, newy1, newx2, newy2); line(newx2, newy2, newx3, newy3); line(newx3, newy3, newx1, newy1);

}

void main() { int gm,gd=DETECT; int n; int x1,x2,x3,y1,y2,y3,choice; initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); do{ printf("\n---MENU----\n"); printf("\n1)translation\n2)scaling\n3)rotation\n4)exit\n"); printf("Enter ur choice"); scanf("%d",&n); printf("\nEnter the coordinates of triangle(x1, y1), (x2, y2), (x3, y3):"); scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); switch(n) { case 1: translation(x1, y1, x2, y2, x3, y3); break; case 2: scaling(x1, y1, x2, y2, x3, y3); break; case 3: rotation(x1, y1, x2, y2, x3, y3); break; case 4:exit(0); default:exit(0); } getch(); closegraph(); }while(n!=5);

}

OUTPUT:...


Similar Free PDFs