Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Code 2

This document contains code to implement 2D transformations - translation, scaling, and rotation - on a rectangle in C graphics. The main function initializes graphics mode and displays a menu to select a transformation. Based on the user's choice, it calls the respective function - translate(), scale(), or rotate() - passing the rectangle's coordinates and transformation parameters. These functions apply the transformation and redraw the rectangle. Translation adds offsets, scaling multiplies coordinates by factors, and rotation uses trigonometric functions to calculate new coordinates.

Uploaded by

sohamrajam
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)
10 views

Code 2

This document contains code to implement 2D transformations - translation, scaling, and rotation - on a rectangle in C graphics. The main function initializes graphics mode and displays a menu to select a transformation. Based on the user's choice, it calls the respective function - translate(), scale(), or rotate() - passing the rectangle's coordinates and transformation parameters. These functions apply the transformation and redraw the rectangle. Translation adds offsets, scaling multiplies coordinates by factors, and rotation uses trigonometric functions to calculate new coordinates.

Uploaded by

sohamrajam
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/ 2

Name:-Om Yeole

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:-

You might also like