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

Assignment no 2

Uploaded by

Samreen Chowlkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assignment no 2

Uploaded by

Samreen Chowlkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Department of Computer Engineering

Academic Year: 2024-25 Name of Student: Varad Khot


Semester: III Student ID: 23102137
Class / Branch: S.E Comp Date of Performance: 06/10/2024
Subject: Computer Graphics Date of Submission: 06/10/2024
Name of Instructor: Prof. Harshada Sonkamble
Assignment No. 02

1. 2D Transformation: Reflection and

Shearing. Program:

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<math.h>

void drawTriangle(int x1, int y1,

int x2, int y2, int x3, int y3, int

color) { setcolor(color);

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

}
void main() {

int x1, x2, x3, y1, y2, y3, nx1, nx2, nx3, ny1, ny2, ny3;

int shx, shy, choice, reflectionChoice;

int gd = DETECT, gm = 0

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

printf("PROGRAM FOR 2D TRANSFORMATION\n");


printf("Enter the coordinates of the triangle (x1 y1 x2 y2 x3 y3): ")
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
drawTriangle(x1, y1, x2, y2, x3, y3, WHITE);
printf("\n1. Shearing\n"); printf("2. Reflection\n"); printf("3. Exit\n"); printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) { case 1:
printf("Enter shearing factors (shx shy): ");

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

nx1 = x1 + y1 * shx; ny1 = y1 + x1 * shy; nx2 = x2

+ y2 * shx; ny2 = y2 + x2 * shy; nx3 = x3 + y3 *

shx;

ny3 = y3 + x3 * shy;

drawTriangle(nx1, ny1, nx2, ny2, nx3, ny3,

YELLOW); break;

case 2:

printf("1. Reflection about X-axis\n"); printf("2.

Reflection about Y-axis\n"); printf("3. Reflection

about Origin\n"); printf("Enter your choice for

reflection: "); scanf("%d", &reflectionChoice);

switch(reflectionChoice) { case 1:

nx1 = x1;

ny1 = -y1 + getmaxy();

nx2 = x2;

ny2 = -y2 + getmaxy();

nx3 = x3;

ny3 = -y3 + getmaxy();

break;

case 2:nx1 = -x1 + getmaxx();


ny1 = y1;

nx2 = -x2 + getmaxx(); ny2 = y2;


nx3 = -x3 + getmaxx();

ny3 = y3;

break;

case 3:

nx1 = -x1 + getmaxx();

ny1 = -y1 + getmaxy();

nx2 = -x2 + getmaxx();

ny2 = -y2 + getmaxy();

nx3 = -x3 + getmaxx();

ny3 = -y3 + getmaxy();

break;

default:

printf("Invalid

reflection choice!\n");

return;

}
drawTriangle(nx1, ny1, nx2, ny2, nx3, ny3, GREEN);

break;

case 3:

printf("Exiting...\n");

break;
default:

printf("Invalid choice!\

n");

getch();

closegraph();

}
Output:
2. Sutherland Hodgeman Polygon clipping algorithm Program:

#include <graphics.h> // For graphics functions

#include <stdio.h> // For input/output functions like printf, scanf

#define round(a) ((int)(a + 0.5)) // Define rounding


int k;

float xmin, ymin, xmax, ymax, arr[20], m;

void clipleft(float x1, float y1, float x2, float y2) { if

(x2 - x1)

m = (y2 - y1) / (x2 - x1);

else

m = 100000;
if (x1 >= xmin && x2 >= xmin)

{ arr[k] = x2;

arr[k + 1] = y2
k += 2;

if (x1 < xmin && x2 >= xmin) { arr[k]

= xmin;

arr[k + 1] = y1 + m * (xmin - x1);

arr[k + 2] = x2;
arr[k + 3] = y2; k += 4;

}
if (x1 >= xmin && x2 < xmin) { arr[k] = xmin;
arr[k + 1] = y1 + m * (xmin - x1);

k += 2;

void cliptop(float x1, float y1, float x2, float y2) { if (y2 - y1)
m = (x2 - x1) / (y2 - y1);

else

m = 100000;

if (y1 <= ymax && y2 <= ymax) { arr[k] = x2;


arr[k + 1] = y2;

k += 2;

}
if (y1 > ymax && y2 <= ymax)

{ arr[k] = x1 + m * (ymax - y1);

arr[k + 1] = ymax;

arr[k + 2] =

x2; arr[k + 3]

= y2; k += 4;

}
if (y1 <= ymax && y2 > ymax)

{ arr[k] = x1 + m * (ymax - y1);

arr[k + 1] = ymax;

k += 2;

}
void clipright(float x1, float y1, float x2, float y2) { if (x2 - x1)
m = (y2 - y1) / (x2 – x1);
else

m = 100000;
if (x1 <= xmax && x2 <= xmax)

{ arr[k] = x2;

arr[k + 1] = y2;

k += 2;

}
if (x1 > xmax && x2 <= xmax) { arr[k]

= xmax;

arr[k + 1] = y1 + m * (xmax - x1); arr[k

+ 2] = x2;

arr[k + 3] = y2;

k += 4;

}
if (x1 < xmax && x2 > xmax) { arr[k]

= xmax;

arr[k + 1] = y1 + m * (xmax - x1);

k += 2;

void clipbottom(float x1, float y1, float x2, float y2) { if (y2 - y1)
m = (x2 - x1) / (y2 - y1);

else

m = 100000;
if (y1 >= ymin && y2 >= ymin)

{ arr[k] = x2;
arr[k + 1] = y2; k += 2;

}
if (y1 < ymin && y2 >= ymin) { arr[k] = x1 + m * (ymin - y1); arr[k + 1] = ymin;
arr[k + 2] =

x2; arr[k + 3]

= y2; k += 4;

}
if (y1 >= ymin && y2 < ymin) { arr[k] = x1 + m * (ymin - y1); arr[k + 1] = ymin;
k += 2;}

}
void main() {

int gd = DETECT, gm, n, i, poly[20];

float xi, yi, xf, yf, polyy[20];

printf("Coordinates of rectangular

clip window:\n"); printf("xmin,

ymin: ");

scanf("%f %f", &xmin, &ymin);

printf("xmax, ymax: ");

scanf("%f %f", &xmax, &ymax);

printf("\n\nPolygon to be

clipped:\nNumber of sides: ");

scanf("%d", &n);

printf("Enter the coordinates:\n");

for (i = 0; i < 2 * n; i++)

scanf("%f", &polyy[i]);
polyy[2 * n] = polyy[0]; polyy[2

* n + 1] = polyy[1];
for (i = 0; i < 2 * n + 2; i++)

poly[i] = round(polyy[i]);

initgraph(&gd, &gm, "C:\\

Turboc3\\BGI"); //

Initialize graphics

setcolor(RED);

rectangle(xmin, ymax, xmax, ymin); // Draw clipping rectangle


setcolor(WHITE);

fillpoly(n, poly); // Draw the unclipped polygon

getch(); // Wait for user input before proceeding

cleardevice();

k = 0;

for (i = 0; i < 2 * n; i += 2)

clipleft(polyy[i], polyy[i + 1], polyy[i + 2], polyy[i + 3]);

n = k / 2;

for (i = 0; i < k; i++)

polyy[i] = arr[i];

polyy[k] = polyy[0];

polyy[k + 1] = polyy[1];
k = 0;

for (i = 0; i < 2 * n; i += 2)

cliptop(polyy[i], polyy[i + 1], polyy[i + 2], polyy[i + 3]);n = k / 2;

for (i = 0; i < k; i++)

polyy[i] = arr[i];

polyy[k] = polyy[0];
polyy[k + 1] = polyy[1];
k = 0;

for (i = 0; i < 2 * n; i += 2)

clipright(polyy[i], polyy[i + 1], polyy[i + 2], polyy[i + 3]);


n = k / 2;

for (i = 0; i < k; i++)

polyy[i] = arr[i]; polyy[k]

= polyy[0];

polyy[k + 1] = polyy[1];
k = 0;

for (i = 0; i < 2 * n; i += 2)

clipbottom(polyy[i], polyy[i + 1], polyy[i + 2], polyy[i + 3]);


for (i = 0; i < k; i++) poly[i] =

round(arr[i]);

if (k)

fillpoly(k / 2, poly); // Draw

clipped polygon

setcolor(RED);

rectangle(xmin, ymax, xmax, ymin); // Draw the clipping rectangle

getch(); // Wait for user input before exiting closegraph(); //

Close the graphics mode

Output:

You might also like