Lab Report 8
Title: To show line clipping in C using the Cohen-Sutherland Algorithm.
Theory:
Generally, any procedure that identifies those portions of a picture that are either inside or
outside of a specified region of space is referred to as a clipping algorithm, or simply clipping.
The region against which an object is to clip is called a clip window. In other words, the clipping
defines how much we show or hide the object on the window. Here, our perception must be like
a machine to detect the visible surface. An application of clipping includes: \
● Extracting parts of a defined scene for viewing
● Identifying visible surfaces in three-dimensional views
● Drawing, painting operations that allow parts of the picture to be selected for
copying, moving, erasing, or duplicating, etc.
The clipping primitives are:
● Point Clipping
● Line Clipping (straight-line segments)
● Area Clipping (polygons)
● Curve Clipping
● Text Clipping
The code:
The code for the asked program is given below:
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;
int xmin, xmax, ymin, ymax;
struct lines {
int x1, y1, x2, y2;
};
int sign(int x) {
if (x > 0) return 1;
else return 0;
}
void clip(struct lines mylines) {
int bits[4], bite[4], i, var;
setcolor(RED);
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);
string initial = "", end = "", temp = "";
for (i = 0; i < 4; i++) initial += bits[i] ? '1' : '0';
for (i = 0; i < 4; i++) end += bite[i] ? '1' : '0';
float m = (mylines.x2 - mylines.x1 != 0) ?
(mylines.y2 - mylines.y1) / (float)(mylines.x2 - mylines.x1) : 0;
float c = mylines.y1 - m * mylines.x1;
if (initial == end && end == "0000") {
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
}
else {
for (i = 0; i < 4; i++) {
int val = (bits[i] & bite[i]);
temp += val ? '1' : '0';
}
if (temp != "0000") return;
for (i = 0; i < 4; i++) {
if (bits[i] == bite[i]) continue;
if (i == 0 && bits[i] == 1) {
var = round(m * xmin + c);
mylines.y1 = var;
mylines.x1 = xmin;
}
if (i == 0 && bite[i] == 1) {
var = round(m * xmin + c);
mylines.y2 = var;
mylines.x2 = xmin;
}
if (i == 1 && bits[i] == 1) {
var = round(m * xmax + c);
mylines.y1 = var;
mylines.x1 = xmax;
}
if (i == 1 && bite[i] == 1) {
var = round(m * xmax + c);
mylines.y2 = var;
mylines.x2 = xmax;
}
if (i == 2 && bits[i] == 1) {
var = round((float)(ymin - c) / m);
mylines.y1 = ymin;
mylines.x1 = var;
}
if (i == 2 && bite[i] == 1) {
var = round((float)(ymin - c) / m);
mylines.y2 = ymin;
mylines.x2 = var;
}
if (i == 3 && bits[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y1 = ymax;
mylines.x1 = var;
}
if (i == 3 && bite[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y2 = ymax;
mylines.x2 = var;
}
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);
}
initial = "", end = "";
for (i = 0; i < 4; i++) initial += bits[i] ? '1' : '0';
for (i = 0; i < 4; i++) end += bite[i] ? '1' : '0';
if (initial == end && end == "0000") {
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
}
else return;
}
}
int main()
{
int gd = DETECT, gm;
xmin = 40; xmax = 100;
ymin = 40; ymax = 80;
initgraph(&gd, &gm, NULL);
line(xmin, ymin, xmax, ymin);
line(xmax, ymin, xmax, ymax);
line(xmax, ymax, xmin, ymax);
line(xmin, ymax, xmin, ymin);
struct lines mylines[4];
mylines[0] = {30, 65, 55, 30};
mylines[1] = {60, 20, 100, 90};
mylines[2] = {60, 100, 80, 70};
mylines[3] = {85, 50, 120, 75};
for (int i = 0; i < 4; i++) {
line(mylines[i].x1, mylines[i].y1, mylines[i].x2, mylines[i].y2);
delay(1000);
}
for (int i = 0; i < 4; i++) {
clip(mylines[i]);
delay(1000);
}
delay(4000);
getch();
closegraph();
return 0;
}
The output:
The output of the asked program is given below:
Conclusion:
Thus, as shown in the program above, we can draw and reflect a triangle about various axes
using the various functions available in the graphics.h header file.