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

Assingment 4

The document contains a C program that implements 2D transformation functions for polygons using OpenGL. It allows for transformations such as translation, scaling, shearing, and rotation based on user input. The program also includes functions for drawing lines and filling polygons based on the transformed coordinates.

Uploaded by

Arpit
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)
6 views

Assingment 4

The document contains a C program that implements 2D transformation functions for polygons using OpenGL. It allows for transformations such as translation, scaling, shearing, and rotation based on user input. The program also includes functions for drawing lines and filling polygons based on the transformed coordinates.

Uploaded by

Arpit
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/ 6

Name :-Arpit Dalal

Roll Number:-24
Division:-CS - A
PRN:-12310414

Write a program to implement 2D transformation functions.

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

int edges;
int i, j;
int arr[999][2];
int miny, maxy;
int transformedArr[999][2];

void drawLine(int x1, int y1, int x2, int y2) {


glBegin(GL_POINTS);
int dx = x2 - x1;
int dy = y2 - y1;
int steps;
if (abs(dx) > abs(dy)) {
steps = abs(dx);
} else {
steps = abs(dy);
}
float xinc = dx / (float)steps;
float yinc = dy / (float)steps;
float x = x1, y = y1;
for (int k = 0; k <= steps; k++) {
glVertex2i((int)x, (int)y);
x += xinc;
y += yinc;
}
glEnd();
}

void fillPolygon() {
int y;
int x[999];
int count;
for (y = miny; y <= maxy; y++) {
count = 0;
for (i = 0; i < edges; i++) {
int x1 = transformedArr[i][0];
int y1 = transformedArr[i][1];
int x2 = transformedArr[(i + 1) % edges][0];
int y2 = transformedArr[(i + 1) % edges][1];
if ((y1 <= y && y < y2) || (y2 <= y && y < y1)) {
float xtemp = x1 + (y - y1) * (x2 - x1) /(y2 - y1);
x[count++] = (int)xtemp;
} else if (y1 == y && y2 == y) {
if (x1 < x2) {
x[count++] = x1;
x[count++] = x2;
} else {
x[count++] = x2;
x[count++] = x1;
}
}
}
for (i = 0; i < count; i++) {
for (j = i + 1; j < count; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for (i = 0; i < count; i += 2) {
if (i + 1 < count) {
glBegin(GL_POINTS);
for (int k = x[i]; k <= x[i + 1]; k++) {
glVertex2i(k, y);
}
glEnd();
}
}
}
}

void applyTransformation(int choice) {


switch (choice) {
case 1: {
int tx, ty;
printf("Enter shift in x direction: ");
scanf("%d", &tx);
printf("Enter shift in y direction: ");
scanf("%d", &ty);
for (i = 0; i < edges; i++) {
transformedArr[i][0] = arr[i][0] + tx;
transformedArr[i][1] = arr[i][1] + ty;
}
break;
}
case 2: { // Scaling
float sx, sy;
printf("Enter scaling factor for x: ");
scanf("%f", &sx);
printf("Enter scaling factor for y: ");
scanf("%f", &sy);
for (i = 0; i < edges; i++) {
transformedArr[i][0] = (int)(arr[i][0] * sx);
transformedArr[i][1] = (int)(arr[i][1] * sy);
}
break;
}
case 3: {
float shx, shy;
printf("Enter shearing factor for x (shx): ");
scanf("%f", &shx);
printf("Enter shearing factor for y (shy): ");
scanf("%f", &shy);
for (i = 0; i < edges; i++) {
transformedArr[i][0] = (int)(arr[i][0] + shx * arr[i][1]);
transformedArr[i][1] = (int)(arr[i][1] + shy * arr[i][0]);
}
break;
}
case 4: {
float angle;
printf("Enter angle of rotation (in degrees): ");
scanf("%f", &angle);
float rad = angle * M_PI / 180.0;
for (i = 0; i < edges; i++) {
transformedArr[i][0] = (int)(arr[i][0] * cos(rad) - arr[i][1] * sin(rad));
transformedArr[i][1] = (int)(arr[i][0] * sin(rad) + arr[i][1] * cos(rad));
}
break;
}
default:
printf("Invalid choice. No transformation applied.\n");
for (i = 0; i < edges; i++) {
transformedArr[i][0] = arr[i][0];
transformedArr[i][1] = arr[i][1];
}
}
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
int choice;
printf("Choose a transformation:\n1. Translation\n2. Scaling\n3. Shearing\n4. Rotation\nEnter
your choice: ");
scanf("%d", &choice);
applyTransformation(choice);

miny = 480;
maxy = 0;
for (i = 0; i < edges; i++) {
if (transformedArr[i][1] < miny) {
miny = transformedArr[i][1];
}
if (transformedArr[i][1] > maxy) {
maxy = transformedArr[i][1];
}
}
fillPolygon();
glBegin(GL_LINES);
for (i = 0; i < edges - 1; i++) {
glVertex2i(transformedArr[i][0], transformedArr[i][1]);
glVertex2i(transformedArr[i + 1][0], transformedArr[i + 1][1]);
}
glVertex2i(transformedArr[0][0], transformedArr[0][1]);
glVertex2i(transformedArr[edges - 1][0], transformedArr[edges - 1][1]);
glEnd();
glFlush();
}

void myInit(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-480.0, 640.0, -640.0, 480.0);
}

int main(int argc, char **argv) {


printf("number of edges : ");
scanf("%d", &edges);
for (int j = 0; j < edges; j++) {
printf("enter value of coordinate %d :", j + 1);
scanf("%d", &arr[j][0]);
scanf("%d", &arr[j][1]);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Polygon Filling");
myInit();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like