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

Polygon Clipping in C Graphics

The document contains C code for clipping a polygon within a rectangular window by applying clipping algorithms to the left, top, right and bottom edges of the window. It takes in polygon coordinates, defines clipping functions, and calls the functions to iteratively clip the polygon edges against each window boundary.
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)
31 views4 pages

Polygon Clipping in C Graphics

The document contains C code for clipping a polygon within a rectangular window by applying clipping algorithms to the left, top, right and bottom edges of the window. It takes in polygon coordinates, defines clipping functions, and calls the functions to iteratively clip the polygon edges against each window boundary.
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

#include <graphics.

h>
#include <conio.h>
#include <stdio.h>

#define round(a) ((int)(a + 0.5))

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;
}
}

int main() {
int gd = DETECT, gm, n, poly[20];
float xi, yi, xf, yf, polyy[20];
int i; // Declare i here

clrscr();
printf("coordinates of rectangular clip window :\nxmin,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:");


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

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

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


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

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

setcolor(RED);
rectangle(xmin, ymax, xmax, ymin);

printf("\nunclipped polygon");

setcolor(WHITE);
fillpoly(n, poly);

getch();
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[i] = polyy[0];
polyy[i + 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[i] = polyy[0];
polyy[i + 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[i] = polyy[0];
polyy[i + 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);

setcolor(RED);
rectangle(xmin, ymax, xmax, ymin);

printf("\nclipped polygon");

getch();
closegraph();
return 0;
}

You might also like