0% found this document useful (0 votes)
10 views9 pages

CG Practical No.2 (Transformation)

Uploaded by

ayushikhar16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

CG Practical No.2 (Transformation)

Uploaded by

ayushikhar16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical No.

Name:- Ambatkar Atharv Uday

Roll No. :- 23CO007

Batch :- A

1) Scaling

#include<iostream>

#include<graphics.h>

using namespace std;

int main()

initwindow(500,500,"Scaling");

int x1,y1,x2,y2,x3,y3,x4,y4,xn1,yn1,xn2,yn2,xn3,yn3,xn4,yn4,sx,sy;

cout<<"Enter coordinate x1 and y1 ";

cin>>x1>>y1;

cout<<"Enter coordinate x2 and y2 ";

cin>>x2>>y2;

cout<<"Enter coordinate x3 and y3 ";

cin>>x3>>y3;

cout<<"Enter coordinate x4 and y4 ";

cin>>x4>>y4;

int mat1[4][3]={{x1,y1,1},{x2,y2,1},{x3,y3,1},{x4,y4,1}};
int mat3[4][3]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};

cout<<"Enter scaling factor for x and y direction ";

cin>>sx>>sy;

int mat2[3][3]={{sx,0,0},{0,sy,0},{0,0,1}};

for(int i=0;i<4;i++)

for(int j=0;j<3;j++)

for(int k=0;k<3;k++)

mat3[i][j]+=(mat1[i][k]*mat2[k][j]);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x4,y4);

line(x4,y4,x1,y1);

xn1=mat3[0][0];

yn1=mat3[0][1];

xn2=mat3[1][0];

yn2=mat3[1][1];

xn3=mat3[2][0];

yn3=mat3[2][1];

xn4=mat3[3][0];

yn4=mat3[3][1];
line(xn1+150,yn1,xn2+150,yn2);

line(xn2+150,yn2,xn3+150,yn3);

line(xn3+150,yn3,xn4+150,yn4);

line(xn4+150,yn4,xn1+150,yn1);

getch();

return 0;

}
2) Translation

#include<iostream>

#include<graphics.h>

using namespace std;

int main()

initwindow(500,500,"Translation");

int x1,y1,x2,y2,x3,y3,x4,y4,xn1,yn1,xn2,yn2,xn3,yn3,xn4,yn4,tx,ty;

cout<<"Enter coordinate x1 and y1 ";

cin>>x1>>y1;

cout<<"Enter coordinate x2 and y2 ";

cin>>x2>>y2;

cout<<"Enter coordinate x3 and y3 ";

cin>>x3>>y3;

cout<<"Enter coordinate x4 and y4 ";

cin>>x4>>y4;

int mat1[4][3]={{x1,y1,1},{x2,y2,1},{x3,y3,1},{x4,y4,1}};

int mat3[4][3]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};

cout<<"Enter translation factor for x and y direction ";

cin>>tx>>ty;
int mat2[3][3]={{1,0,0},{0,1,0},{tx,ty,1}};

for(int i=0;i<4;i++)

for(int j=0;j<3;j++)

for(int k=0;k<3;k++)

mat3[i][j]+=(mat1[i][k]*mat2[k][j]);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x4,y4);

line(x4,y4,x1,y1);

xn1=mat3[0][0];

yn1=mat3[0][1];

xn2=mat3[1][0];

yn2=mat3[1][1];

xn3=mat3[2][0];

yn3=mat3[2][1];

xn4=mat3[3][0];

yn4=mat3[3][1];

line(xn1+150,yn1,xn2+150,yn2);

line(xn2+150,yn2,xn3+150,yn3);

line(xn3+150,yn3,xn4+150,yn4);
line(xn4+150,yn4,xn1+150,yn1);

getch();

return 0;

}
3) Rotation

#include<iostream>

#include<math.h>

#include<graphics.h>

using namespace std;

int main()

initwindow(500,500,"Rotation");

float x1,y1,x2,y2,x3,y3,x4,y4,xn1,yn1,xn2,yn2,xn3,yn3,xn4,yn4;

float theta;

cout<<"Enter coordinate x1 and y1 ";

cin>>x1>>y1;

cout<<"Enter coordinate x2 and y2 ";

cin>>x2>>y2;

cout<<"Enter coordinate x3 and y3 ";

cin>>x3>>y3;

cout<<"Enter coordinate x4 and y4 ";

cin>>x4>>y4;

float cordinate[4][3]={{x1,y1,1},{x2,y2,1},{x3,y3,1},{x4,y4,1}};

float output[4][3]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
cout<<"Enter angle theta ";

cin>>theta;

const float pi=3.1415926536;

float a;

a=theta*(pi/180);

float rotateclock[3][3]={{cos(a),-sin(a),0},{sin(a),cos(a),0},{0,0,1}};

for(int i=0;i<4;i++)

for(int j=0;j<3;j++)

for(int k=0;k<3;k++)

output[i][j]+=(cordinate[i][k]*rotateclock[k][j]);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x4,y4);

line(x4,y4,x1,y1);

xn1=output[0][0];

yn1=output[0][1];

xn2=output[1][0];

yn2=output[1][1];
xn3=output[2][0];

yn3=output[2][1];

xn4=output[3][0];

yn4=output[3][1];

line(xn1+150,yn1+150,xn2+150,yn2+150);

line(xn2+150,yn2+150,xn3+150,yn3+150);

line(xn3+150,yn3+150,xn4+150,yn4+150);

line(xn4+150,yn4+150,xn1+150,yn1+150);

getch();

return 0;

You might also like