0% found this document useful (0 votes)
7 views7 pages

CG EX-3

The document outlines an experiment in a Computer Graphics Lab focused on applying geometric transformations (translation, scaling, and rotation) to a triangle. It includes algorithms and code implementations for each transformation using C++. The learning outcomes emphasize understanding basic graphics programming and 2D transformations.

Uploaded by

akshitvats555
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)
7 views7 pages

CG EX-3

The document outlines an experiment in a Computer Graphics Lab focused on applying geometric transformations (translation, scaling, and rotation) to a triangle. It includes algorithms and code implementations for each transformation using C++. The learning outcomes emphasize understanding basic graphics programming and 2D transformations.

Uploaded by

akshitvats555
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/ 7

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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

• Draw the Original Triangle


• Prompt the user to enter scaling factors sx and sy
• Update the coordinates of each vertex by multiplying them with the respective
scaling factors:
x1′=x1×sx, y1′=y1×sy
x2′=x2×sx, y2′=y2×sy
x3′=x3×sx, y3′=y3×sy
Rotation:
• Initialize Graphics Mode.
• Take Input for Triangle Coordinates
• Draw the Original Triangle.
• Take Input for Rotation Angle.
• Use the rotation transformation formulas
x′=x cos(θ)−y sin(θ)
y′=x sin(θ)+y cos(θ)
4. Implementation/Code:
a) Translation:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main() {
clrscr();
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int x1, y1, x2, y2, x3, y3, tx, ty;


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 translation value among x-axis: ";
cin >> tx;
cout << "Enter translation value among y-axis: ";
cin >> ty;
x1 += tx; x2 += tx; x3 += tx;
y1 += ty; y2 += ty; y3 += ty;
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
}
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

line(x3, y3, x1, y1);


cout << "Enter scaling value among x-axis: ";
cin >> sx;
cout << "Enter scaling value among y-axis: ";
cin >> sy;
x1 *= sx; x2 *= sx; x3 *= sx;
y1 *= sy; y2 *= sy; y3 *= sy;
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
}

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

tempX = x2; tempY = y2;


x2 = tempX * cos(angle) - tempY * sin(angle);
y2 = tempX * sin(angle) + tempY * cos(angle);
tempX = x3; tempY = y3;
x3 = tempX * cos(angle) - tempY * sin(angle);
y3 = tempX * sin(angle) + tempY * cos(angle);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();

}
Fig 3: Rotation

5. Learning Outcome:
• Understanding Basic Graphics Programming.
• Understanding 2D Transformations.
• Understood the concept of coordinate transformation using trigonometric
functions

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy