0% found this document useful (0 votes)
4 views

2d transformations

This C program demonstrates 2D transformations (translation, scaling, and rotation) on a rectangle using graphics.h library. The user can select a transformation type and input parameters, after which the program displays the original and transformed rectangles. Each transformation is implemented in a separate function and visualized using the graphics library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2d transformations

This C program demonstrates 2D transformations (translation, scaling, and rotation) on a rectangle using graphics.h library. The user can select a transformation type and input parameters, after which the program displays the original and transformed rectangles. Each transformation is implemented in a separate function and visualized using the graphics library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void translate(int, int, int, int, int, int);


void scale(int, int, int, int, float, float);
void rotate(int, int, int, int, float);

void main()
{
int gd=DETECT, gm;
int x1=100, y1=100, x2=200, y2=150;
int choice, tx, ty;
float sx, sy, angle;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");


cleardevice();

outtextxy(10, 10, "Original Rectangle");


rectangle(x1, y1, x2, y2);
getch();
cleardevice();
closegraph();

printf("\n2D Transformations Menu:\n");


printf("1. Translation\n");
printf("2. Scaling\n");
printf("3. Rotation\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("\nEnter translation factors (tx ty): ");
scanf("%d %d", &tx, &ty);
translate(x1, y1, x2, y2, tx, ty);
break;

case 2:
printf("\nEnter scaling factors (sx sy): ");
scanf("%f %f", &sx, &sy);
scale(x1, y1, x2, y2, sx, sy);
break;

case 3:
printf("\nEnter rotation angle (in degrees): ");
scanf("%f", &angle);
rotate(x1, y1, x2, y2, angle);
break;

default:
printf("\nInvalid choice!");
break;
}
getch();
closegraph();
}
// ----------- Translation -----------
void translate(int x1, int y1, int x2, int y2, int tx, int ty)
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

outtextxy(10,10,"Translation");
rectangle(x1, y1, x2, y2);

setcolor(RED);
rectangle(x1 + tx, y1 + ty, x2 + tx, y2 + ty);

getch();
closegraph();
}

// ----------- Scaling -----------


void scale(int x1, int y1, int x2, int y2, float sx, float sy)
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

outtextxy(10,10,"Scaling");
rectangle(x1, y1, x2, y2);

int mx = (x1 + x2) / 2;


int my = (y1 + y2) / 2;

x1 = mx + (x1 - mx) * sx;


x2 = mx + (x2 - mx) * sx;
y1 = my + (y1 - my) * sy;
y2 = my + (y2 - my) * sy;

setcolor(RED);
rectangle(x1, y1, x2, y2);

getch();
closegraph();
}

// ----------- Rotation -----------


void rotate(int x1, int y1, int x2, int y2, float angle)
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

outtextxy(10,10,"Rotation");

int mx = (x1 + x2) / 2;


int my = (y1 + y2) / 2;

int xr1 = x1 - mx;


int yr1 = y1 - my;
int xr2 = x2 - mx;
int yr2 = y2 - my;

float rad = angle * (3.14159 / 180);


float cosA = cos(rad);
float sinA = sin(rad);

int new_x1 = mx + (xr1 * cosA - yr1 * sinA);


int new_y1 = my + (xr1 * sinA + yr1 * cosA);
int new_x2 = mx + (xr2 * cosA - yr2 * sinA);
int new_y2 = my + (xr2 * sinA + yr2 * cosA);

rectangle(x1, y1, x2, y2);

setcolor(RED);
line(new_x1, new_y1, new_x2, new_y1);
line(new_x2, new_y1, new_x2, new_y2);
line(new_x2, new_y2, new_x1, new_y2);
line(new_x1, new_y2, new_x1, new_y1);

getch();
closegraph();
}

You might also like