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

CG-3 (1)

The document outlines an experiment in a Computer Graphics lab where students apply translation, scaling, and rotation transformations to a triangle. It includes the aim, objectives, algorithm, code implementation in C++, and expected learning outcomes related to geometric transformations. Students learn to utilize graphics libraries and understand the mathematical principles behind these transformations.

Uploaded by

sp179418
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)
6 views7 pages

CG-3 (1)

The document outlines an experiment in a Computer Graphics lab where students apply translation, scaling, and rotation transformations to a triangle. It includes the aim, objectives, algorithm, code implementation in C++, and expected learning outcomes related to geometric transformations. Students learn to utilize graphics libraries and understand the mathematical principles behind these transformations.

Uploaded by

sp179418
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: Archin chauhan UID: 22BCS14876


Branch: CSE Section: 22BCS_IOT-626/B
Semester: 6th Date: 21 th Jan, 2025
Subject: Computer Graphics with 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) Start

b) Initialize Graphics Mode


 Detect and initialize the graphics mode.

c) Define Original Triangle


 Set initial coordinates of the triangle (x1, y1), (x2, y2), (x3, y3).
 Draw the original triangle in white color.

d) Translation
 Ask the user to input translation values (tx, ty).
 Compute new coordinates:
 xt1 = x1 + tx, yt1 = y1 + ty
 xt2 = x2 + tx, yt2 = y2 + ty
 xt3 = x3 + tx, yt3 = y3 + ty
 Draw the translated triangle in red color and label it.

e) Scaling
 Ask the user to input scaling values (sx, sy).
 Compute new coordinates:
 xs1 = xt1 * sx, ys1 = yt1 * sy
 xs2 = xt2 * sx, ys2 = yt2 * sy
 xs3 = xt3 * sx, ys3 = yt3 * sy
 Draw the scaled triangle in green color and label it.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

f) Rotation
 Ask the user to input the rotation angle (in degrees).
 Convert the angle to radians: rad = angle * π / 180.
 Compute new coordinates using rotation formulas:
 xr1 = xs1 * cos(rad) - ys1 * sin(rad)
 yr1 = xs1 * sin(rad) + ys1 * cos(rad)
 xr2 = xs2 * cos(rad) - ys2 * sin(rad)
 yr2 = xs2 * sin(rad) + ys2 * cos(rad)
 xr3 = xs3 * cos(rad) - ys3 * sin(rad)
 yr3 = xs3 * sin(rad) + ys3 * cos(rad)
 Draw the rotated triangle in blue color and label it.

g) Wait for User Input


 Use getch() to wait until a key is pressed.

h) Close Graphics Mode


 Call closegraph() to exit graphics mode.

i) End

4. Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int color) {

setcolor(color);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}
void main() {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

clrscr();
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

int x1 = 100, y1 = 100, x2 = 80, y2 = 150, x3 = 150, y3 = 120;


int tx, ty, sx, sy;
float angle, rad;

drawTriangle(x1, y1, x2, y2, x3, y3, WHITE);

cout << "Enter the translation values (tx, ty): ";


cin >> tx >> ty;
int xt1 = x1 + tx, yt1 = y1 + ty;
int xt2 = x2 + tx, yt2 = y2 + ty;
int xt3 = x3 + tx, yt3 = y3 + ty;
drawTriangle(xt1, yt1, xt2, yt2, xt3, yt3, RED);

cout << "Enter the scaling values (sx, sy): ";


cin >> sx >> sy;
int xs1 = xt1 * sx, ys1 = yt1 * sy;
int xs2 = xt2 * sx, ys2 = yt2 * sy;
int xs3 = xt3 * sx, ys3 = yt3 * sy;
drawTriangle(xs1, ys1, xs2, ys2, xs3, ys3, GREEN);

cout << "Enter the rotation angle: ";


cin >> angle;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

rad = angle * 3.1416 / 180;


int xr1 = xs1 * cos(rad) - ys1 * sin(rad);
int yr1 = xs1 * sin(rad) + ys1 * cos(rad);
int xr2 = xs2 * cos(rad) - ys2 * sin(rad);
int yr2 = xs2 * sin(rad) + ys2 * cos(rad);
int xr3 = xs3 * cos(rad) - ys3 * sin(rad);
int yr3 = xs3 * sin(rad) + ys3 * cos(rad);
drawTriangle(xr1, yr1, xr2, yr2, xr3, yr3, BLUE);

getch();
closegraph();
}

5. Output:

Original Triangle:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Translated Triangle:

Scaled Triangle:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Rotated Triangle:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Learning Outcomes:
1. Understanding Basic Transformations

 Learned how to apply Translation, Scaling, and Rotation to geometric


shapes.

2. Implementation of Graphics in C++

 Gained hands-on experience with graphics.h for drawing and manipulating


shapes.

3. Mathematical Computation of Transformations

 Understood the mathematical formulas behind transformations:

 Translation: Shifting coordinates by (tx, ty).

 Scaling: Enlarging or shrinking a shape using (sx, sy).

 Rotation: Rotating a shape using trigonometric functions (cos, sin)

You might also like