Code 2
Code 2
ID:-VU1F2223029
Div:-A Batch:- A
PRACTICAL NO 6
IMPLEMENTATION OF 2D TRANSFORMATION : TRANSLATION, SCALING, ROTATION
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
#include<stdio.h>
void translate(int, int, int, int);
void scale(int, int, float, float);
void rotate(int, int, float);
void main() {
int gd = DETECT, gm;
int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
int ch;
int tx, ty;
float sx, sy;
float angle;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
rectangle(x1, y1, x2, y2);
printf("Choose a transformation:\n");
printf("1. Translation\n");
printf("2. Scaling\n");
printf("3. Rotation\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the translation factors (tx ty): ");
scanf("%d %d", &tx, &ty);
translate(x1, y1, tx, ty);
break;
case 2:
printf("Enter the scaling factors (sx sy): ");
scanf("%f %f", &sx, &sy);
scale(x1, y1, sx, sy);
break;
case 3:
printf("Enter the rotation angle (in degrees): ");
scanf("%f", &angle);
rotate(x1, y1, angle);
break;
default:
printf("Invalid choice!");
}
getch();
closegraph();
}
void translate(int x1, int y1, int tx, int ty) {
x1 = x1 + tx;
y1 = y1 + ty;
rectangle(x1, y1, x1 + 100, y1 + 100);
}
void scale(int x1, int y1, float sx, float sy) {
x1 = x1 * sx;
y1 = y1 * sy;
rectangle(x1, y1, x1 + 100, y1 + 100);
}
void rotate(int x1, int y1, float angle) {
float radian = angle * (3.1416 / 180);
int x_new = x1 * cos(radian) - y1 * sin(radian);
int y_new = x1 * sin(radian) + y1 * cos(radian);
rectangle(x_new, y_new, x_new + 100, y_new + 100);
}
Output:-