Triangle
Triangle
Institute of Engineering
Thapathali Campus, Thapathali
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:
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:
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 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;
setcolor(WHITE);
drawAxis(centerX, centerY, 400);
drawTriangle(centerX + x1, centerY - y1, centerX + x2, centerY - y2, centerX + x3,
centerY - y3, TriangleColor);
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.