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

Worksheet - 4

The document is a student worksheet for a computer graphics lab. It details an experiment performing translation, scaling, and rotation transformations on a triangle. The student inputs triangle coordinate values and selects a transformation type. Their code then applies the corresponding mathematical operations to the coordinates and redraws the transformed triangle. Rotations can occur around any vertex or the triangle. The student learned how to program different geometric transformations and distinguish clockwise and counterclockwise rotations in C++ graphics.

Uploaded by

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

Worksheet - 4

The document is a student worksheet for a computer graphics lab. It details an experiment performing translation, scaling, and rotation transformations on a triangle. The student inputs triangle coordinate values and selects a transformation type. Their code then applies the corresponding mathematical operations to the coordinates and redraws the transformed triangle. Rotations can occur around any vertex or the triangle. The student learned how to program different geometric transformations and distinguish clockwise and counterclockwise rotations in C++ graphics.

Uploaded by

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

Worksheet – 4

Student Name: Pushp Raj Choudhary UID: 19BCS1372


Branch: CSE-3 Section/Group: A
Semester: 5th Date of Performance: 15st Sept
Subject Name: CG LAB Subject Code: CSP-305

1.Aim/Overview of the practical:


To perform translation, scaling and rotation transformations on a given triangle.
To rotate a given triangle clockwise and anticlockwise about a given point.

2.Code:
a)
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void main()
{
int x1, x2, x3, y1, y2, y3, d;
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

cout<<"\nPUSHP RAJ";
cout<<"\n19BCS1372";

settextstyle(3,2,3);
setcolor(5);
setbkcolor(9);
outtextxy(450,150,"PUSHP RAJ");

cout<<"Enter Coordinates:\n";

cout<<"x1 & y1: "; cin>>x1>>y1;


cout<<"x2 & y2: "; cin>>x2>>y2;
cout<<"x3 & y3: "; cin>>x3>>y3;
int trgl_arr[] = {x1, y1, x2, y2, x3, y3, x1, y1};
drawpoly(4, trgl_arr);
cout<<"\n1. Traslation\n2. Scalling\n3. Rotation\nEnter choice: ";
cin>>d;

switch(d)
{
case 1: int x, y;
cout<<"Enter translation coordinates: "; cin>>x>>y;
int translation_arr[] = {x1+x, y1+y, x2+x, y2+y, x3+x, y3+y,
x1+x, y1+y};

drawpoly(4, translation_arr);
break;

case 2: int sx, sy;


cout<<"Enter scaling for x & y: "; cin>>sx>>sy;
int scaling_arr[] = {x1*sx, y1*sy, x2*sx, y2*sy, x3*sx, y3*sy,
x1*sx, y1*sy};
drawpoly(4, scalling_arr);
break;

case 3: float t, rx1, ry1, rx2, ry2, rx3, ry3;


cout<<"Enter rotation angle: ";
cin>>t;
t = t*3.14/180;
rx1 = x1*cos(t) - y1*sin(t);
ry1 = y1*cos(t) + x1*sin(t);
rx2 = x2*cos(t) - y2*sin(t);
ry2 = x2*cos(t) + x2*sin(t);
rx3 = x3*cos(t) - y3*sin(t);
ry3 = y3*cos(t) + x3*sin(t);
int rotation_arr[] = {rx1, ry1, rx2, ry2, rx3, ry3, rx1, ry1};
drawpoly(4, rotation_arr);
break;
default: cout<<"invalid input..";
}

getch();
closegraph();
}

Output:
a)transition:
b)scalling:
c)Rotation:
a)anti-clockwise:

b)clockwise:
b) CODE:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void main()
{
int x1, x2, x3, y1, y2, y3, p,x, y;
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

cout<<"\nPUSHP RAJ";
cout<<"\n19BCS1372";

settextstyle(3,2,3);
setcolor(5);
setbkcolor(9);
outtextxy(450,250,"PUSHP RAJ");

cout<<"Enter Coordinates:\n";

cout<<"x1 & y1: "; cin>>x1>>y1;


cout<<"x2 & y2: "; cin>>x2>>y2;
cout<<"x3 & y3: "; cin>>x3>>y3;
int trgl_arr[] = {x1, y1, x2, y2, x3, y3, x1, y1};
drawpoly(4, trgl_arr);
cout<<"19BCS1372";

cout<<"Enter rotation point (1/2/3): ";


cin>>p;
float theta, rx1, ry1, rx2, ry2, rx3, ry3;
cout<<"Enter rotation angle: "; cin>>theta;
theta = theta*3.14/180;

switch(p)
{
case 1:
rx2 = x2*cos(theta) - y2*sin(theta);
ry2 = x2*cos(theta) + x2*sin(theta);
rx3 = x3*cos(theta) - y3*sin(theta);
ry3 = y3*cos(theta) + x3*sin(theta);
int rotation_arr1[] = {x1, y1, rx2, ry2, rx3, ry3};
drawpoly(4, rotation_arr1);
break;

case 2:
rx1 = x1*cos(theta) - y1*sin(theta);
ry1 = y1*cos(theta) + x1*sin(theta);
rx3 = x3*cos(theta) - y3*sin(theta);
ry3 = y3*cos(theta) + x3*sin(theta);

int rotation_arr2[] = {rx1, ry1, x2, y2, rx3, ry3};


drawpoly(4, rotation_arr2);
break;

case 3:
rx1 = x1*cos(theta) - y1*sin(theta);
ry1 = y1*cos(theta) + x1*sin(theta);
rx2 = x2*cos(theta) - y2*sin(theta);
ry2 = x2*cos(theta) + x2*sin(theta);

int rotation_arr3[] = {rx1, ry1, rx2, ry2, x3, y3};


drawpoly(4, rotation_arr3);
break;
default :
cout<<"invalid point...";
}

getch();
closegraph();
}

Output:
a)anti-clockwise rotation:
b)clockwise rotation:

3. Learning outcome:

• Learnt different transformation


• Learnt more about graphics in c++
• Learnt to find clockwise and anti clockwise rotation.

You might also like