-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab 6 Cohen Suthern Line Clipping Algorithm.cpp
142 lines (127 loc) · 2.66 KB
/
Lab 6 Cohen Suthern Line Clipping Algorithm.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
// Region codes
const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000
// Clipping Window Boundaries
int xmin, ymin, xmax, ymax;
// Function to find the region code of a point (x, y)
int computeCode(int x, int y)
{
int code = INSIDE;
if (x < xmin)
code |= LEFT;
else if (x > xmax)
code |= RIGHT;
if (y < ymin)
code |= BOTTOM;
else if (y > ymax)
code |= TOP;
return code;
}
// Cohen-Sutherland clipping algorithm
void cohenSutherClip(int x0, int y0, int x1, int y1)
{
int code0 = computeCode(x0, y0);
int code1 = computeCode(x1, y1);
int accept = 0;
while (1)
{
if ((code0 == 0) && (code1 == 0))
{
accept = 1;
break;
}
else if (code0 & code1)
{
break;
}
else
{
int codeOut;
int x, y;
if (code0 != 0)
codeOut = code0;
else
codeOut = code1;
if (codeOut & TOP)
{
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
}
else if (codeOut & BOTTOM)
{
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
}
else if (codeOut & RIGHT)
{
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
}
else if (codeOut & LEFT)
{
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
if (codeOut == code0)
{
x0 = x;
y0 = y;
code0 = computeCode(x0, y0);
}
else
{
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
}
}
if (accept)
{
line(x0, y0, x1, y1);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter clipping window coordinates as xmin, ymin, xmax, ymax: ");
scanf("%d %d %d %d", &xmin, &ymin, &xmax, &ymax);
int number;
printf("Enter the number of lines you want: ");
scanf("%d", &number);
int(*lines)[4] = (int(*)[4])malloc(number * 4 * sizeof(int));
for (int i = 0; i < number; i++)
{
printf("Enter coordinates of line %d (x0, y0, x1, y1): ", i + 1);
scanf("%d %d %d %d", &lines[i][0], &lines[i][1], &lines[i][2], &lines[i][3]);
}
// Draw the clipping window
rectangle(xmin, ymin, xmax, ymax);
// Draw lines before clipping
for (i = 0; i < number; i++)
{
line(lines[i][0], lines[i][1], lines[i][2], lines[i][3]);
}
printf("Before Line Clipping: ");
getch();
cleardevice();
printf("After Line Clipping:");
// Draw the clipping window again
rectangle(xmin, ymin, xmax, ymax);
// Clip lines using Cohen-Sutherland algorithm and draw clipped lines
for (i = 0; i < number; i++)
{
cohenSutherClip(lines[i][0], lines[i][1], lines[i][2], lines[i][3]);
}
getch();
closegraph();
return 0;
}