0% found this document useful (0 votes)
11 views6 pages

Triangle

Uploaded by

mycodebeg4e1205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Triangle

Uploaded by

mycodebeg4e1205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Tribhuvan University

Institute of Engineering
Thapathali Campus, Thapathali

Subject: Computer Graphics


#LAB 5

Submitted by:
Name: Saroj Nagarkoti
Roll No.: THA078BEI039

Submitted to:
Department of Electronics and Computer Engineering
Date: 2080-10-03
OBJECTIVE:
The objective of this C++ code is to create a graphical representation of a
triangle, perform reflections along the x-axis and y-axis, and rotate the triangle
based on user input using the graphics.h library.

THEORY:

In computer graphics, 2D transformations are operations that modify the appearance of


2D objects on a plane. These transformations include translation (shifting), rotation,
scaling, and reflection. Transformations are typically applied using matrices, where each
transformation is represented by a matrix that, when multiplied with the coordinates of a
point, produces the transformed coordinates.

Reflection is a transformation that mirrors an object across a specified axis.For


reflection across the x-axis, the y-coordinates of the triangle vertices are negated, and
for reflection across the y-axis, the x-coordinates are negated. This results in the
creation of a mirror image of the original object.

Rotation is a transformation that turns an object about a fixed point, usually the
origin.The rotation is achieved using trigonometric functions (cosine and sine) to
compute the new coordinates after rotating the original points around the origin. The
resulting coordinates replace the original ones, effecting the rotation.

ALGORITHM:

Reflection along X-axis :


Step 1: Enter coordinates (X1 ,Y1) ,(X2 ,Y2) ,(X3 ,Y3) .
Step 2: Draw the original triangle.
Step 3: Reflect the triangle by negating y coordinates.
Step 4: Draw the translated triangle using updated coordinates.

Reflection along Y-axis :


Step 1: Enter coordinates (X1 ,Y1) ,(X2 ,Y2) ,(X3 ,Y3) .
Step 2: Draw the original triangle.
Step 3: Reflect the triangle by negating x coordinates.
Step 4: Draw the translated triangle using updated coordinates.

Rotation:
Step 1: Enter coordinates (X1 ,Y1) ,(X2 ,Y2) ,(X3 ,Y3) .
Step 2: Draw the original triangle.
Step 3: Convert the angle to radians (angle * (M_PI / 180.0)).
Step 4: For each vertex (x, y) compute:
x' = x * cos(radian) - y * sin(radian)
y' = x * sin(radian) + y * cos(radian)
Step 5: Update the code and draw the rotated triangle using updated
coordinates.

SOURCE CODE:

#include <iostream>
#include <graphics.h>
#include <cmath>
#define TriangleColor WHITE
#define Triang RED
#define Tri GREEN
using namespace std;

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 drawAxis(int centerX, int centerY, int length)


{

line(centerX - length, centerY, centerX + length, centerY);

line(centerX, centerY - length, centerX, centerY + length);


}

void reflectTriangle(int &x1, int &y1, int &x2, int &y2, int &x3, int &y3, char axis)
{
if (axis == 'x')
{
y1 = -y1;
y2 = -y2;
y3 = -y3;
}
else if (axis == 'y')
{
x1 = -x1;
x2 = -x2;
x3 = -x3;
}
}

void rotateTriangle(int &x1, int &y1, int &x2, int &y2, int &x3, int &y3, double angle)
{
double radian = angle * (M_PI / 180.0);
int x1New = x1 * cos(radian) - y1 * sin(radian);
int y1New = x1 * sin(radian) + y1 * cos(radian);
int x2New = x2 * cos(radian) - y2 * sin(radian);
int y2New = x2 * sin(radian) + y2 * cos(radian);
int x3New = x3 * cos(radian) - y3 * sin(radian);
int y3New = x3 * sin(radian) + y3 * cos(radian);

x1 = x1New;
y1 = y1New;
x2 = x2New;
y2 = y2New;
x3 = x3New;
y3 = y3New;
}
int main()
{
int gd = DETECT, gm;
char data[] = "C:/MinGW/lib/libbgi.a";
initgraph(&gd, &gm, data);
int centerX = getmaxx() / 2;
int centerY = getmaxy() / 2;

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


double angle;

cout << "Enter the coordinates : ";


cin >> x1 >> y1;
cout << "Enter the coordinates : ";
cin >> x2 >> y2;
cout << "Enter the coordinates : ";
cin >> x3 >> y3;
cout << "Enter the Angle of rotation: ";
cin >> angle;

setcolor(WHITE);
drawAxis(centerX, centerY, 400);

drawTriangle(centerX + x1, centerY - y1, centerX + x2, centerY - y2, centerX + x3,
centerY - y3, TriangleColor);

reflectTriangle(x1, y1, x2, y2, x3, y3, 'x');


drawTriangle(centerX + x1, centerY - y1, centerX + x2, centerY - y2, centerX + x3,
centerY - y3, Triang);

reflectTriangle(x1, y1, x2, y2, x3, y3, 'y');


drawTriangle(centerX + x1, centerY - y1, centerX + x2, centerY - y2, centerX + x3,
centerY - y3, TriangleColor);

rotateTriangle(x1, y1, x2, y2, x3, y3, angle);


drawTriangle(centerX + x1, centerY - y1, centerX + x2, centerY - y2, centerX + x3,
centerY - y3, Tri);

getch();
closegraph();
}
DISCUSSION AND CONCLUSION:
Rotation is a fundamental transformation in graphics used to reorient objects, such as
characters or shapes, in a scene. Reflection is often used to create symmetrical
patterns in graphics, making designs more aesthetically pleasing.Both rotation and
reflection are not only essential for functional purposes like object positioning but also
contribute significantly to the artistic aspects of graphics, enabling creative expression in
design and animation.

In conclusion, understanding and implementing rotation and reflection in 2D graphics


are crucial skills for anyone involved in computer graphics, game development, or
related fields. These transformations provide the foundation for building complex and
visually appealing scenes and animations.

You might also like