Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
ACADEMIC YEAR: 2024-25
Course: Computer Graphics Lab
Course code: CSL303
Year: SE Sem: III
Experiment No.: 7:
Aim: - To implement Cohen Sutherland Line Clipping Algorithm.
Name: Shubham Buktar
Roll Number: 19
Date of Performance:19/09/2024
Date of Submission:26/09/2024
Evaluation
Performance Indicator Max. Marks Marks Obtained
Performance 5
Understanding 5
Journal work and timely submission. 10
Total 20
Performance Exceed Expectations Meet Expectations Below
Indicator (EE) (ME) Expectations (BE)
Performance 5 3 2
Understanding 5 3 2
Journal work and
10 8 4
timely submission.
Checked by
Name of Faculty : Ms. Kranti Gule
Signature :
Date :
CSL303- Computer Graphics Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
Aim Write a program to implement Cohen Sutherland Line Clipping Algorithm
in C.
Objective To implement Cohen Sutherland Line Clipping Algorithm for clipping a line
x1,y1 and x2, y2 with respect to a clipping window Xmin, Xmax, Ymin,
Ymax.
Theory Cohen Sutherland Line Clipping Algorithm:
In the algorithm, first of all, it is detected whether line lies inside the screen
or it is outside the screen. All lines come under any one of the following
categories:
1. Visible
2. Not Visible
3. Clipping Case
1. Visible: If a line lies within the window, i.e., both endpoints of the line
lies within the window. A line is visible and will be displayed as it is.
2. Not Visible: If a line lies outside the window it will be invisible and
rejected. Such lines will not display. If any one of the following
inequalities is satisfied, then the line is considered invisible. Let A (x1,y2)
and B (x2,y2) are endpoints of line.
xmin,xmax are coordinates of the window.
ymin,ymax are also coordinates of the window.
x1>xmax
x2>xmax
y1>ymax
y2>ymax
x1<xmin
x2<xmin
y1<ymin
y2<ymin
3. Clipping Case: If the line is neither visible case nor invisible case. It
is considered to be clipped case. First of all, the category of a line is found
based on nine regions given below. All nine regions are assigned codes.
Each code is of 4 bits. If both endpoints of the line have end bits zero, then
the line is considered to be visible.
CSL303- Computer Graphics Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
The center area is having the code, 0000, i.e., region 5 is considered a
rectangle window.
Conditions
Case I:
Bitwise OR of region of 2 endpoints of line = 0000 → Line lies inside the
window
Case II :
1. Completely invisible
Bitwise OR ≠ 0000
1. Partially Visible
Bitwise AND ≠ 0000
Completely invisible
Case III :
Partially Visible
1. Choose the endpoints of the line.
2. Find the intersection point with the window.
3. Replace the endpoints with the intersection point.
Algorithm Step 1: Assign a region code for each end point.
Step 2: Perform Bitwise OR = 0000.
Accept line & draw
Step 3: Perform Bitwise AND
If result ≠ 0000 then reject the line.
else clip the line
→ Select the endpoints.
→ Find the intersection point at the window.
→ Replace endpoints with the intersection pt & update
the region code.
CSL303- Computer Graphics Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
Program #include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main() {
int a[4], b[4];
float m, xnew, ynew;
float x1 = 100, y1 = 100, xh = 300, yh = 300, xa =
10, ya = 200, xb = 250, yb = 150;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\tc\\BGI");
setcolor(5);
line(xa, ya, xb, yb);
setcolor(12);
rectangle(x1, y1, xh, yh);
// Slope of the line
m = (yb - ya) / (xb - xa);
// Finding region codes for xa, ya
if (xa < x1) a[3] = 1;
else a[3] = 0;
if (xa > xh) a[2] = 1;
else a[2] = 0;
if (ya < y1) a[1] = 1;
else a[1] = 0;
if (ya > yh) a[0] = 1;
else a[0] = 0;
// Finding region codes for xb, yb
if (xb < x1) b[3] = 1;
else b[3] = 0;
if (xb > xh) b[2] = 1;
else b[2] = 0;
if (yb < y1) b[1] = 1;
else b[1] = 0;
if (yb > yh) b[0] = 1;
else b[0] = 0;
printf("Press a key to continue\n");
CSL303- Computer Graphics Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
getch();
// Cohen-Sutherland clipping conditions
if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3]
== 0 &&
b[0] == 0 && b[1] == 0 && b[2] == 0 && b[3]
== 0) {
printf("No clipping\n");
line(xa, ya, xb, yb);
}
else if (a[0] && b[0] || a[1] && b[1] || a[2] && b[2]
|| a[3] && b[3]) {
clrscr();
printf("Line discarded\n");
rectangle(x1, y1, xh, yh);
}
else {
if (a[3] == 1 && b[3] == 0) {
ynew = (m * (x1 - xa)) + ya;
setcolor(12);
rectangle(x1, y1, xh, yh);
setcolor(0);
line(xa, ya, xb, yb);
setcolor(15);
line(x1, ynew, xb, yb);
}
else if (a[2] == 1 && b[2] == 0) {
xnew = xa + (y1 - ya) / m;
setcolor(0);
line(xa, ya, xb, yb);
setcolor(15);
line(xnew, yh, xb, yb);
}
else if (a[0] == 1 && b[0] == 0) {
xnew = xa + (yh - ya) / m;
setcolor(0);
line(xa, ya, xb, yb);
setcolor(15);
line(xnew, yh, xb, yb);
}
}
getch();
closegraph();
}
CSL303- Computer Graphics Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
Output
Conclusion 1. What action does the algorithm take when a
line segment is partially inside the clipping
window?
2. What is the significance of the logical AND
operation on outcodes?
CSL303- Computer Graphics Lab
Vidyavardhini’s College of Engineering & Technology
Department of Computer Science & Engineering (Data Science)
CSL303- Computer Graphics Lab