0% found this document useful (0 votes)
15 views4 pages

Ex 5

The document describes a menu-driven C program that performs 2D transformations such as translation, scaling, rotation, reflection, and shearing using the graphics.h library. It includes functions for each transformation and a main loop that allows user interaction to select and apply transformations to a triangle. The program utilizes graphical output to display the transformations in real-time.

Uploaded by

totalclips780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Ex 5

The document describes a menu-driven C program that performs 2D transformations such as translation, scaling, rotation, reflection, and shearing using the graphics.h library. It includes functions for each transformation and a main loop that allows user interaction to select and apply transformations to a triangle. The program utilizes graphical output to display the transformations in real-time.

Uploaded by

totalclips780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX-5 A menu-driven program for 2D transformation in C

language with graphical output using graphics.h can perform


transformations like:
 Translation

 Scaling

 Rotation

 Reflection

 Shearing

#include <graphics.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

void drawTriangle(int x[], int y[]) {

line(x[0], y[0], x[1], y[1]);

line(x[1], y[1], x[2], y[2]);

line(x[2], y[2], x[0], y[0]);

void translate(int x[], int y[], int tx, int ty) {

for (int i = 0; i < 3; i++) {

x[i] += tx;

y[i] += ty;

void scale(int x[], int y[], float sx, float sy) {

for (int i = 0; i < 3; i++) {


x[i] *= sx;

y[i] *= sy;

void rotate(int x[], int y[], float angle) {

float rad = angle * (3.14159 / 180);

int x_new[3], y_new[3];

for (int i = 0; i < 3; i++) {

x_new[i] = round(x[i] * cos(rad) - y[i] * sin(rad));

y_new[i] = round(x[i] * sin(rad) + y[i] * cos(rad));

for (int i = 0; i < 3; i++) {

x[i] = x_new[i];

y[i] = y_new[i];

void reflect(int x[], int y[], char axis) {

for (int i = 0; i < 3; i++) {

if (axis == 'x')

y[i] = -y[i];

else if (axis == 'y')

x[i] = -x[i];

void shear(int x[], int y[], float shx, float shy) {

for (int i = 0; i < 3; i++) {

x[i] = x[i] + shx * y[i];

y[i] = y[i] + shy * x[i];


}

int main() {

int gd = DETECT, gm;

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

int x[3] = {200, 250, 300};

int y[3] = {200, 150, 200};

int choice, tx, ty;

float sx, sy, angle, shx, shy;

char axis;

while (1) {

cleardevice();

drawTriangle(x, y);

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

printf("1. Translation\n2. Scaling\n3. Rotation\n4. Reflection\n5. Shearing\n6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter tx and ty: ");

scanf("%d %d", &tx, &ty);

translate(x, y, tx, ty);

break;

case 2:
printf("Enter sx and sy: ");

scanf("%f %f", &sx, &sy);

scale(x, y, sx, sy);

break;

case 3:

printf("Enter angle of rotation: ");

scanf("%f", &angle);

rotate(x, y, angle);

break;

case 4:

printf("Enter axis (x or y): ");

scanf(" %c", &axis);

reflect(x, y, axis);

break;

case 5:

printf("Enter shear factors shx and shy: ");

scanf("%f %f", &shx, &shy);

shear(x, y, shx, shy);

break;

case 6:

closegraph();

return 0;

default:

printf("Invalid choice! Try again.\n");

} }

getch();

closegraph();

return 0;

You might also like