CG EX-3
CG EX-3
Experiment 3
Student Name: Zatch UID:
Branch: CSE Section/Group:
Semester: 6 Date of Performance:
Subject Name: Computer Graphics Lab Subject Code: 22CSH-352
1. Aim:
Apply translation, scaling, and rotation transformations on a given triangle and
observe the changes.
2. Objective:
To apply geometric transformations such as translation, scaling, and rotation on
a given triangle.
3. Algorithm:
a) Translation:
• Initialize Graphics Mode.
• Take Input for Triangle Coordinates
• Draw the Original Triangle.
• Use the line() function to draw three lines connecting the three given points.
• Prompt the user to enter translation values tx and ty.
• Update the coordinates:
x1′=x1+tx, y1′=y1+ty
x2′=x2+tx, y2′=y2+ty
x3′=x3+tx, y3′=y3+ty
b) Scaling:
• Initialize Graphics Mode.
• Take Input for Triangle Coordinates
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Fig 1: Translation
b) Scaling:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main() {
clrscr();
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
int x1, y1, x2, y2, x3, y3, sx, sy;
cout << "Enter x1, y1: ";
cin >> x1 >> y1;
cout << "Enter x2, y2: ";
cin >> x2 >> y2;
cout << "Enter x3, y3: ";
cin >> x3 >> y3;
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Fig 2: Scaling
c) Rotation
#include<iostream.h>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main() {
clrscr();
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
int x1, y1, x2, y2, x3, y3;
float angle;
cout << "Enter x1, y1: ";
cin >> x1 >> y1;
cout << "Enter x2, y2: ";
cin >> x2 >> y2;
cout << "Enter x3, y3: ";
cin >> x3 >> y3;
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
cout << "Enter the rotation angle: ";
cin >> angle;
angle = angle * 3.1428 / 180;
int tempX, tempY;
tempX = x1; tempY = y1;
x1 = tempX * cos(angle) - tempY * sin(angle);
y1 = tempX * sin(angle) + tempY * cos(angle);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
Fig 3: Rotation
5. Learning Outcome:
• Understanding Basic Graphics Programming.
• Understanding 2D Transformations.
• Understood the concept of coordinate transformation using trigonometric
functions