Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

WORKSHEET-1.

Name: Samridh Garg UID: 19BCS1815


BRANCH: CSE SECTION: 5(C)
SUBJECT: Computer Graphics Lab CODE: CSP-305

Aim:-WAP to perform different transformations on triangle.


1. Translation
2. Scaling
3. Rotation

TRANSLATION :

Code :
#include<iostream>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
using namespace std;
main(){
initwindow(500,500);
int i,j,k,x1,x2,x3,y1,y2,y3,x,y;
int c[3][3],a[3][3];
cout<<"ENTER COORDINATES OF TRIANGLE";
cout<<"\n 1st coordinate";
cout<<"\n x - coordinate ";
cin>>a[0][0];
cout<<"\n y - coordinate";
cin>>a[1][0];
a[2][0]=1;
cout<<"\n 2nd coordinate";
cout<<"\n x - coordinate";
cin>>a[0][1];
cout<<"\n y - coordinate";
cin>>a[1][1];
a[2][1]=1;
cout<<"\n 3rd coordinate";
cout<<"\n x - coordinate";
cin>>a[0][2];
cout<<"\n y - coordinate";
cin>>a[1][2];
a[2][2]=1;
cout<<"\n\n\n enter the translation coordinate";
cout<<" enter x - coordinate";
cin>>x;
cout<<"enter y - coordinate";
cin>>y;
int t[3][3]={{1,0,x},{0,1,y},{0,0,1}};
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{ c[i][j]=0;
for(k=0;k<3;k++)
{c[i][j]+=t[i][k]*a[k][j]; }}}
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);
line(c[0][0],c[1][0],c[0][2],c[1][2]);
getch();
closegraph();
}

Output:
SCALING :

Code :
#include<stdio.h>
#include<graphics.h>
#include<math.h>

void scaling(int x1,int y1,int x2,int y2,int x3,int y3)


{
int sx,sy,xn1,yn1,xn2,xn3,yn3,yn2,gd,gm;
printf("enter the scaling vector\n");
scanf("%d %d",&sx,&sy);

xn1=x1*sx;
yn1=y1*sy;
xn2=x2*sx;
yn2=y2*sy;
xn3=x3*sx;
yn3=y3*sy;

line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
delay(100);

line(xn1,yn1,xn2,yn2);
line(xn1,yn1,xn3,yn3);
line(xn2,yn2,xn3,yn3);
delay(100000);
cleardevice();

}
int main()
{
initwindow(500,500);
int ch,x1,y1,x2,y2,x3,y3;
printf("enter the vertex co-ordinates of triangle\n");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
scaling(x1,y1,x2,y2,x3,y3);
return 0;
}
Output:

ROTATION:

Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
main()
{
initwindow(500,500);
int x1,y1,x2,y2,x3,y3;
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);
triangle(x1,y1,x2,y2,x3,y3);
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
triangle(x1,y1,x2,y2,x3,y3);
getch();
}
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 Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
triangle(a1,b1,a2,b2,a3,b3); }

Output:

You might also like