Exp 8 CG
Exp 8 CG
Experiment 8
Student Name: Krrish Mittal UID: 22BCS13506
Branch: Be-CSE Section/Group: 22BCS_FL_IOT-601
Semester: 6th Date of Performance:12/3/25
Subject Name: Computer Graphics Subject Code: 22CSH-352
with Lab
1. Aim:
a) Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting at one
point with a given window.
b) Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting at two
or more points with a given window
2. Objective: To clip a line intersecting at a single point and two or more points with a
window using the Cohen-Sutherland Line Clipping algorithm.
3. Implementation/Code:
##include <graphics.h>
#include <iostream>
using namespace std;
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
int xmin, ymin, xmax, ymax;
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;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
} else if (code1 & code2) {
break;
} else {
int codeOut;
int x, y;
if (code1 != 0) codeOut = code1;
else codeOut = code2;
if (codeOut & TOP) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (codeOut & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (codeOut & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else if (codeOut & LEFT) {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (codeOut == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
if (accept) {
line(x1, y1, x2, y2);
cout << "\nLine is partially visible and clipped.";
} else {
cout << "\nLine is completely outside and rejected.";
}
getch();
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char *)"");
cout << "Enter bottom-left coordinate of viewport (xmin ymin): ";
cin >> xmin >> ymin;
cout << "Enter top-right coordinate of viewport (xmax ymax): ";
cin >> xmax >> ymax;
int x1, y1, x2, y2;
cout << "Enter coordinates of the first point of the line (x1 y1): ";
cin >> x1 >> y1;
cout << "Enter coordinates of the second point of the line (x2 y2): ";
cin >> x2 >> y2;
rectangle(xmin, ymin, xmax, ymax);
setcolor(WHITE);
line(x1, y1, x2, y2);
getch();
cohenSutherlandClip(x1, y1, x2, y2);
closegraph();
return 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
4. Output:
Figure No. 1
Figure No. 4
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Learning Outcome:
• Gained insights into the Cohen-Sutherland Line Clipping algorithm and its step-by-
step execution for clipping lines based on a given viewport.
• Successfully implemented the algorithm in C++ using the graphics library to clip
lines at one or more intersection points with a defined window.
• Learned how to determine the region codes for different points using bitwise
operations, helping classify points as inside, outside, or partially inside the
viewport.
• Understood the handling of different cases, including when the line is fully visible,
completely outside, or partially visible and requiring clipping.