0% found this document useful (0 votes)
5 views5 pages

Practical 5

The document contains a C program that implements 2D transformations such as translation, scaling, and rotation for a shape defined by its vertices. Users can input the number of vertices and their coordinates, and then choose a transformation to apply. The program utilizes graphics functions to visualize the transformations on the shape.
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)
5 views5 pages

Practical 5

The document contains a C program that implements 2D transformations such as translation, scaling, and rotation for a shape defined by its vertices. Users can input the number of vertices and their coordinates, and then choose a transformation to apply. The program utilizes graphics functions to visualize the transformations on the shape.
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/ 5

Prac cal 5

Code :-
#include <stdio.h>

#include <graphics.h>

#include <math.h>

#define PI 3.14159265

void translate(int points[][2], int n, int tx, int ty) {

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

points[i][0] += tx;

points[i][1] += ty;

void scale(int points[][2], int n, float sx, float sy) {

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

points[i][0] = (int)(points[i][0] * sx);

points[i][1] = (int)(points[i][1] * sy);

void rotate(int points[][2], int n, float angle) {

float rad = angle * PI / 180.0;

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

int x = points[i][0];

int y = points[i][1];

points[i][0] = (int)(x * cos(rad) - y * sin(rad));

points[i][1] = (int)(x * sin(rad) + y * cos(rad));

void convertToPolyPoints(int points[][2], int n, int polyPoints[]) {


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

polyPoints[2 * i] = points[i][0];

polyPoints[2 * i + 1] = points[i][1];

polyPoints[2 * n] = points[0][0];

polyPoints[2 * n + 1] = points[0][1];

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, NULL);

int n;

prin ("Enter the number of ver ces of the shape: ");

scanf("%d", &n);

int points[n][2];

prin ("Enter the coordinates of the ver ces (x y):\n");

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

scanf("%d %d", &points[i][0], &points[i][1]);

int polyPoints[2 * n + 2];

int choice;

do {

cleardevice();

convertToPolyPoints(points, n, polyPoints);

drawpoly(n + 1, polyPoints);
prin ("\n2D Transforma on Menu:\n");

prin ("1. Transla on\n");

prin ("2. Scaling\n");

prin ("3. Rota on\n");

prin ("4. Exit\n");

prin ("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: {

int tx, ty;

prin ("Enter transla on factors (tx ty): ");

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

translate(points, n, tx, ty);

break;

case 2: {

float sx, sy;

prin ("Enter scaling factors (sx sy): ");

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

scale(points, n, sx, sy);

break;

case 3: {

float angle;

prin ("Enter rota on angle (in degrees): ");

scanf("%f", &angle);

rotate(points, n, angle);

break;

case 4:
prin ("Exi ng program.\n");

break;

default:

prin ("Invalid choice. Please try again.\n");

delay(2000);

} while (choice != 4);

closegraph();

return 0;

Output :-

Translated :-
Scaled :-

Rotated :-

You might also like