Harcg 5
Harcg 5
COMPUTERSCIENCE&ENGINEERING
Experiment 5
Student Name: Harshit Singh Chandel UID: 22BCS10013
Branch: BE CSE Section/Group:IOT-608-A
Semester: 6th Date of Performance: 17-02-2025
Subject Name: Computer Graphics Subject Code: 22CSH-352
3. Implementation/Code:
a) To rotate clockwise:
#include <iostream.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1, y1, x2, y2, x3, y3;
cout << "Enter the coordinates (x1, y1), (x2, y2), (x3, y3) for the triangle: ";
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
int points[] = {x1, y1, x2, y2, x3, y3, x1, y1};
drawpoly(4, points);
outtextxy(50,70,"22BCS10013_Harshit");
int xf = (x1 + x2 + x3) / 3;
int yf = (y1 + y2 + y3) / 3;
float ang;
cout << "Enter the clockwise rotation angle: ";
cin >> ang;
float rad = ang * 3.1428 / 180;
int X1 = (x1 - xf) * cos(rad) - (y1 - yf) * sin(rad) + xf;
int Y1 = (x1 - xf) * sin(rad) + (y1 - yf) * cos(rad) + yf;
22BCS10013_HARSHIT
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
int rotatedPoints[] = {X1, Y1, X2, Y2, X3, Y3, X1, Y1};
drawpoly(4, rotatedPoints);
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();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
int x1,y1,x2,y2,x3,y3;
cout<<"Enter (x1,y1),(x2,y2),(x3,y3) for triangle : ";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int a[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,a);
outtextxy(50,70,"22BCS10013_Harshit");
int xf=(x1+x2+x3)/3;
int yf=(y1+y2+y3)/3;
float ang;
cout<<"Enter the anticlockwise rotation angle :";
cin>>ang;
float rad=ang*3.1428/180;
int X1=(x1-xf)*cos(rad)+(y1-yf)*sin(rad)+xf;
22BCS10013_HARSHIT
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
int Y1=-(x1-xf)*sin(rad)+(y1-yf)*cos(rad)+yf;
int X2=(x2-xf)*cos(rad)+(y2-yf)*sin(rad)+xf;
int Y2=-(x2-xf)*sin(rad)+(y2-yf)*cos(rad)+yf;
int X3=(x3-xf)*cos(rad)+(y3-yf)*sin(rad)+xf;
int Y3=-(x3-xf)*sin(rad)+(y3-yf)*cos(rad)+yf;
int b[]={X1,Y1,X2,Y2,X3,Y3,X1,Y1};
drawpoly(4,b);
getch();
}
Output
4. Learning Outcomes
1. Learn how to implement and apply the rotation in clockwise and anti-clockwise
for efficiently rotating a triangle on a 2D graphics window.
2. Learn how to work with coordinate transformations for graphical objects,
3. Understand the process of initializing and closing the graphics mode in C++ using
initgraph() and closegraph() functions.
4. Apply graphics programming to create visual demonstrations, which can be further
extended to develop simple games, animations, or user interfaces.
5. Recognize the role of rotation in triangle drawing.
22BCS10013_HARSHIT