Practical 12CGR
Practical 12CGR
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
typedef struct {
int x, y;
} Point;
int computeRegionCode(Point p) {
int code = 0;
int accept = 0;
while (1) {
if ((code1 == 0) && (code2 == 0)) {
accept = 1;
break;
} else if ((code1 & code2) != 0) {
break;
} else {
int codeOut;
float x, y;
if (code1 != 0) {
codeOut = code1;
} else {
codeOut = code2;
}
if (codeOut == code1) {
p1.x = x;
p1.y = y;
code1 = computeRegionCode(p1);
} else {
p2.x = x;
p2.y = y;
code2 = computeRegionCode(p2);
}
}
}
if (accept) {
setcolor(WHITE);
line(p1.x, p1.y, p2.x, p2.y);
printf("Line accepted from (%d, %d) to (%d, %d)\n", p1.x, p1.y, p2.x, p2.y);
} else {
printf("Line rejected\n");
}
}
void main() {
setcolor(RED);
rectangle(xmin, ymin, xmax, ymax);
setcolor(GREEN);
line(p1.x, p1.y, p2.x, p2.y);
cohenSutherlandClip(p1, p2);
getch();
closegraph();
}
Output:-