CG Exp 5
CG Exp 5
Experiment 5
Student Name: Zatch UID:
Branch: CSE Section/Group:
Semester: 6 Date of Performance:
Subject Name: Computer Graphics Lab Subject Code: 22CSH-352
1. Aim:
Implement clockwise and anticlockwise rotation of a triangle about a specified point
and evaluate the results.
2. Objective:
To perform and visualize clockwise and anticlockwise rotations of a triangle
about a specified point.
3. Algorithm:
a) To Rotate Clockwise :
1. Initialize Graphics Mode:-
Detect and initialize the graphics driver and mode using initgraph().
4. Compute Centroid:-
Calculate the centroid (xc,yc)(xc, yc)(xc,yc) using:
xc= x1+x2+x2 / 3, yc= y1+y2+y3 / 3
b) To Rotate Anti-clockwise:
1. Initialize Graphics Mode:-
Detect and initialize the graphics driver and mode using initgraph().
4. Compute Centroid:-
Calculate the centroid (xc,yc) using:
xc= x1+x2+x2 / 3, yc= y1+y2+y3 / 3
4. Implementation/Code:
a.To Rotate Clockwise:
#include <iostream>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int tri[] = {x1, y1, x2, y2, x3, y3, x1, y1};
setcolor(WHITE);
drawpoly(4, tri);
float angle;
cout << "Enter the rotation angle: ";
cin >> angle;
getch();
closegraph();
return 0;
}
Output:-
b. To Rotate Anti-Clockwise:
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main() {
clrscr();
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Input triangle coordinates
int x1, y1, x2, y2, x3, y3;
cout << "Enter (x1, y1), (x2, y2), (x3, y3) for the triangle: ";
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Compute centroid
int xc = (x1 + x2 + x3) / 3;
int yc = (y1 + y2 + y3) / 3;
Output:-
5. Learning Outcomes:-
• Learned how rotation transformation works in computer graphics using
trigonometric functions (sin, cos).
• Explored the difference between clockwise and anti-clockwise rotation by
modifying the sine term signs in the rotation formula.
• Understood the importance of centroid (xc, yc) in rotating a shape around its
center rather than the origin.
• Gained hands-on experience in using C++ graphics.h library functions like
initgraph(), drawpoly(), and setcolor() for visual representation.
• Learned the necessity of converting degrees to radians using the formula:
rad= angle x 3.14/180