CG-3 (1)
CG-3 (1)
Experiment 3
3. Algorithm:
a) Start
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.
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");
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