100% found this document useful (1 vote)
205 views

Lab Assignment: Title: Basic 2D Transformations Date of Performance: Date of Submission

The document describes algorithms for performing basic 2D transformations - translation, scaling, and rotation - on triangles. It includes code samples in C programming language to implement the algorithms. The algorithms take triangle coordinate points as input, perform the transformation calculations, and output the transformed triangle coordinates. The document is an assignment submission by a student that implemented and tested the 2D transformation algorithms.

Uploaded by

Anuja Patole
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
205 views

Lab Assignment: Title: Basic 2D Transformations Date of Performance: Date of Submission

The document describes algorithms for performing basic 2D transformations - translation, scaling, and rotation - on triangles. It includes code samples in C programming language to implement the algorithms. The algorithms take triangle coordinate points as input, perform the transformation calculations, and output the transformed triangle coordinates. The document is an assignment submission by a student that implemented and tested the 2D transformation algorithms.

Uploaded by

Anuja Patole
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Priyanka N. Gaikwad MCA 1 Roll no.

15

Page no

Computer Graphics

LAB ASSIGNMENT

Title: Basic 2D Transformations Date of Performance: Date of Submission:

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

Aim : - To implement basic 2D Transformations Theory :Algorithm :- Translation


1.

Read the co-ordinates of the triangle ,i.e., (x1,y1) , (x2,y2) (x3,y3)such that they are not equal.

2. For (i=1;i<=3;i++) { if (i==3) Line(xi,yi,x1,y1) else Line(xi,yi,xi+1,yi+1) end if } 3. Read the translation factor ,i.e., tx ,ty 4. For (i=1;i<=3;i++) { xi= xi+tx yi= yi+ty } 5. Repeat step 2 6. Stop

Algorithm :- Scaling

Priyanka N. Gaikwad MCA 1 Roll no.15


1.

Page no

Read the co-ordinates of the triangle ,i.e., (x1,y1) , (x2,y2) (x3,y3)such that they are not equal.

2. For (i=1;i<=3;i++) { if (i==3) Line(xi,yi,x1,y1) else Line(xi,yi,xi+1,yi+1) end if } 3. Read the scaling factor ,i.e., sx ,sy 4. For (i=1;i<=3;i++) { xi= xi*sx yi= yi*sy } 5. Repeat step 2 6. Stop

Algorithm :- Rotation
1.

Read the co-ordinates of the triangle ,i.e., (x1,y1) , (x2,y2) (x3,y3)such that they are not equal.

2. For (i=1;i<=3;i++)

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

{ if (i==3) Line(xi,yi,x1,y1) else Line(xi,yi,xi+1,yi+1) end if } 3. Read the titha ,i.e., titha 4. For (i=1;i<=3;i++) { xi= xi*cos(titha) yi*sin(titha) yi= xi*sin(titha) yi*cos(titha) } 5. Repeat step 2 6. Stop

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

Program : Translation #include<stdio.h> #include<conio.h> #include<graphics.h> void tri(int x[],int y[]); void main() { int gd,gm,x[3],y[3],tx,ty,i; clrscr(); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); printf("Enter the coordinates of the triangle"); for(i=0;i<3;i++) { printf("\nEnter x%d,y%d :",(i+1),(i+1)); scanf("%d%d",&x[i],&y[i]); } tri(x,y); printf("Enter the tranlating distances tx and ty:"); scanf("%d%d",&tx,&ty); for(i=0;i<3;i++) { x[i]=x[i]+tx; y[i]=y[i]+ty; } tri(x,y); getch(); closegraph(); } void tri(int x[3],int y[3]) { int i;

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

for(i=0;i<3;i++) { line(x[i],y[i],x[(i+1)%3],y[(i+1)%3]); } } Output :

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

Scaling #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x1,y1,x2,y2,x3,y3,mx,my; void draw(); void scale(); void main() { int gd,gm; int c; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); draw(); scale(); } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); }

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

void scale() { int x,y,a1,a2,a3,b1,b2,b3; int mx,my; printf("Enter the scalling coordinates:"); scanf("%d%d",&x,&y); //(mx,my) is the fixed point which is the center of the triangle mx=(x1+x2+x3)/3; my=(y1+y2+y3)/3; a1=mx+(x1-mx)*x; b1=my+(y1-my)*y; a2=mx+(x2-mx)*x; b2=my+(y2-my)*y; a3=mx+(x3-mx)*x; b3=my+(y3-my)*y; cleardevice(); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); draw(); getch(); }

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

Output :

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

Rotation #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void tri(int x[],int y[]); void main() { int gd,gm,x[3],y[3],a[3],b[3],i,mx,my; float angle; clrscr(); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:/tc/bgi"); printf("Enter the coordinates of the triangle"); for(i=0;i<3;i++) { printf("Enter the %d point:",(i+1)); scanf("%d%d",&x[i],&y[i]); } tri(x,y); getch(); printf("Enter the rotating angle:"); scanf("%f",&angle); angle=angle*3.14/180; printf("Enter the fixed point:"); scanf("%d%d",&mx,&my); for(i=0;i<3;i++) { a[i]=mx+(x[i]-mx)*cos(angle)-(y[i]-my)*sin(angle); b[i]=my+(x[i]-mx)*sin(angle)+(y[i]-my)*cos(angle); } tri(a,b); getch(); closegraph();

Priyanka N. Gaikwad MCA 1 Roll no.15

Page no

} void tri(int x[],int y[]) { int i; for(i=0;i<2;i++) line(x[i],y[i],x[i+1],y[i+1]); line(x[2],y[2],x[0],y[0]); } Output :

Conclusion : 2D transformations are widely used to manipulate objects in computer graphics.

You might also like