-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram9.c
More file actions
88 lines (73 loc) · 2.46 KB
/
Copy pathprogram9.c
File metadata and controls
88 lines (73 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// write a c program to implement Sutherland–Hodgeman Polygon clipping Algorithm
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void clip(int p[][2], int *np, int x1, int y1, int x2, int y2) {
int newp[20][2], newnp = 0;
int i, k, xi, yi, xk, yk, iPos, kPos, xIntersect, yIntersect;
for (i = 0; i < *np; i++) {
k = (i + 1) % *np;
xi = p[i][0];
yi = p[i][1];
xk = p[k][0];
yk = p[k][1];
iPos = (xi - x1) * (y2 - y1) - (yi - y1) * (x2 - x1);
kPos = (xk - x1) * (y2 - y1) - (yk - y1) * (x2 - x1);
if (iPos >= 0 && kPos >= 0) {
newp[newnp][0] = xk;
newp[newnp++][1] = yk;
} else if (iPos >= 0 && kPos < 0) {
xIntersect = xi + (xk - xi) * abs(iPos) / (abs(iPos) + abs(kPos));
yIntersect = yi + (yk - yi) * abs(iPos) / (abs(iPos) + abs(kPos));
newp[newnp][0] = xIntersect;
newp[newnp++][1] = yIntersect;
} else if (iPos < 0 && kPos >= 0) {
xIntersect = xi + (xk - xi) * abs(iPos) / (abs(iPos) + abs(kPos));
yIntersect = yi + (yk - yi) * abs(iPos) / (abs(iPos) + abs(kPos));
newp[newnp][0] = xIntersect;
newp[newnp++][1] = yIntersect;
newp[newnp][0] = xk;
newp[newnp++][1] = yk;
}
}
*np = newnp;
for (i = 0; i < *np; i++) {
p[i][0] = newp[i][0];
p[i][1] = newp[i][1];
}
}
void main() {
int gd = DETECT, gm, np, i, x1, y1, x2, y2;
int p[10][2];
clrscr();
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
printf("Enter Number of Polygon Points: ");
scanf("%d", &np);
for (i = 0; i < np; i++) {
printf("Enter Point %d (x y): ", i + 1);
scanf("%d %d", &p[i][0], &p[i][1]);
}
printf("Enter Clipping Window (x1 y1 x2 y2): ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
rectangle(x1, y1, x2, y2);
setcolor(RED);
for (i = 0; i < np; i++) {
if (i != np - 1)
line(p[i][0], p[i][1], p[i + 1][0], p[i + 1][1]);
else
line(p[i][0], p[i][1], p[0][0], p[0][1]);
}
clip(p, &np, x1, y1, x2, y1);
clip(p, &np, x2, y1, x2, y2);
clip(p, &np, x2, y2, x1, y2);
clip(p, &np, x1, y2, x1, y1);
for (i = 0; i < np; i++) {
if (i != np - 1)
line(p[i][0], p[i][1], p[i + 1][0], p[i + 1][1]);
else
line(p[i][0], p[i][1], p[0][0], p[0][1]);
}
getch();
closegraph();
}