Description:- In this algorithm, we are given 9 regions on the screen. Out of which one region is of the window and the rest 8 regions are around it given by 4 digit binary. The division of the regions are based on (x_max, y_max) and (x_min, y_min).
The central part is the viewing region or window, all the lines which lie within this region are completely visible. A region code is always assigned to the endpoints of the given line.
To check whether the line is visible or not.

Region Codes
Formula to check binary digits:- TBRL which can be defined as top, bottom, right, and left accordingly.

TBRL Rule
Algorithm
Steps
1) Assign the region codes to both endpoints.
2) Perform OR operation on both of these endpoints.
3) if OR = 0000,
then it is completely visible (inside the window).
else
Perform AND operation on both these endpoints.
i) if AND ? 0000,
then the line is invisible and not inside the window. Also, it can’t be considered for clipping.
ii) else
AND = 0000, the line is partially inside the window and considered for clipping.
4) After confirming that the line is partially inside the window, then we find the intersection with the boundary of the window. By using the following formula:-
Slope:- m= (y2-y1)/(x2-x1)
a) If the line passes through top or the line intersects with the top boundary of the window.
x = x + (y_wmax – y)/m
y = y_wmax
b) If the line passes through the bottom or the line intersects with the bottom boundary of the window.
x = x + (y_wmin – y)/m
y = y_wmin
c) If the line passes through the left region or the line intersects with the left boundary of the window.
y = y+ (x_wmin – x)*m
x = x_wmin
d) If the line passes through the right region or the line intersects with the right boundary of the window.
y = y + (x_wmax -x)*m
x = x_wmax
5) Now, overwrite the endpoints with a new one and update it.
6) Repeat the 4th step till your line doesn’t get completely clipped
Given a set of lines and a rectangular area of interest, the task is to remove lines that are outside the area of interest and clip the lines which are partially inside the area.
Input : Rectangular area of interest (Defined by
below four values which are coordinates of
bottom left and top right)
x_min = 4, y_min = 4, x_max = 10, y_max = 8
A set of lines (Defined by two corner coordinates)
line 1 : x1 = 5, y1 = 5, x2 = 7, y2 = 7
Line 2 : x1 = 7, y1 = 9, x2 = 11, y2 = 4
Line 2 : x1 = 1, y1 = 5, x2 = 4, y2 = 1
Output : Line 1 : Accepted from (5, 5) to (7, 7)
Line 2 : Accepted from (7.8, 8) to (10, 5.25)
Line 3 : Rejected
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are inside the given rectangular area.
The algorithm can be outlines as follows:-
Nine regions are created, eight "outside" regions and one
"inside" region.
For a given line extreme point (x, y), we can quickly
find its region's four bit code. Four bit code can
be computed by comparing x and y with four values
(x_min, x_max, y_min and y_max).
If x is less than x_min then bit number 1 is set.
If x is greater than x_max then bit number 2 is set.
If y is less than y_min then bit number 3 is set.
If y is greater than y_max then bit number 4 is set

There are three possible cases for any given line.
- Completely inside the given rectangle : Bitwise OR of region of two end points of line is 0 (Both points are inside the rectangle)
- Completely outside the given rectangle : Both endpoints share at least one outside region which implies that the line does not cross the visible region. (bitwise AND of endpoints != 0).
- Partially inside the window : Both endpoints are in different regions. In this case, the algorithm finds one of the two points that is outside the rectangular region. The intersection of the line from outside point and rectangular window becomes new corner point and the algorithm repeats

Pseudo Code:
Step 1 : Assign a region code for two endpoints of given line.
Step 2 : If both endpoints have a region code 0000
then given line is completely inside.
Step 3 : Else, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely
outside.
Step 3.2 : Else line is partially inside.
Step 3.2.1 : Choose an endpoint of the line
that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the
rectangular boundary (based on region code).
Step 3.2.3 : Replace endpoint with the intersection point
and update the region code.
Step 3.2.4 : Repeat step 2 until we find a clipped line either
trivially accepted or trivially rejected.
Step 4 : Repeat step 1 for other lines
Below is implementation of above steps.
C++
#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;
const int x_max = 10;
const int y_max = 8;
const int x_min = 4;
const int y_min = 4;
int computeCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
void cohenSutherlandClip(double x1, double y1,
double x2, double y2)
{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
bool accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
}
else if (code1 & code2) {
break;
}
else {
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
if (code_out & TOP) {
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if (code_out & BOTTOM) {
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if (code_out & RIGHT) {
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if (code_out & LEFT) {
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
cout << "Line accepted from " << x1 << ", "
<< y1 << " to " << x2 << ", " << y2 << endl;
}
else
cout << "Line rejected" << endl;
}
int main()
{
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(7, 9, 11, 4);
cohenSutherlandClip(1, 5, 4, 1);
return 0;
}
|
Java
import java.io.*;
class GFG {
static final int INSIDE = 0;
static final int LEFT = 1;
static final int RIGHT = 2;
static final int BOTTOM = 4;
static final int TOP = 8;
static final int x_max = 10;
static final int y_max = 8;
static final int x_min = 4;
static final int y_min = 4;
static int computeCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
static void cohenSutherlandClip(double x1, double y1,
double x2, double y2)
{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
boolean accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
}
else if ((code1 & code2) != 0) {
break;
}
else {
int code_out;
double x = 0, y = 0;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
if ((code_out & TOP) != 0) {
x = x1
+ (x2 - x1) * (y_max - y1)
/ (y2 - y1);
y = y_max;
}
else if ((code_out & BOTTOM) != 0) {
x = x1
+ (x2 - x1) * (y_min - y1)
/ (y2 - y1);
y = y_min;
}
else if ((code_out & RIGHT) != 0) {
y = y1
+ (y2 - y1) * (x_max - x1)
/ (x2 - x1);
x = x_max;
}
else if ((code_out & LEFT) != 0) {
y = y1
+ (y2 - y1) * (x_min - x1)
/ (x2 - x1);
x = x_min;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
System.out.println("Line accepted from " + x1
+ ", " + y1 + " to " + x2
+ ", " + y2);
}
else
System.out.println("Line rejected");
}
public static void main(String[] args)
{
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(7, 9, 11, 4);
cohenSutherlandClip(1, 5, 4, 1);
}
}
|
Python3
INSIDE = 0
LEFT = 1
RIGHT = 2
BOTTOM = 4
TOP = 8
x_max = 10.0
y_max = 8.0
x_min = 4.0
y_min = 4.0
def computeCode(x, y):
code = INSIDE
if x < x_min:
code |= LEFT
elif x > x_max:
code |= RIGHT
if y < y_min:
code |= BOTTOM
elif y > y_max:
code |= TOP
return code
def cohenSutherlandClip(x1, y1, x2, y2):
code1 = computeCode(x1, y1)
code2 = computeCode(x2, y2)
accept = False
while True:
if code1 == 0 and code2 == 0:
accept = True
break
elif (code1 & code2) != 0:
break
else:
x = 1.0
y = 1.0
if code1 != 0:
code_out = code1
else:
code_out = code2
if code_out & TOP:
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1)
y = y_max
elif code_out & BOTTOM:
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1)
y = y_min
elif code_out & RIGHT:
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1)
x = x_max
elif code_out & LEFT:
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1)
x = x_min
if code_out == code1:
x1 = x
y1 = y
code1 = computeCode(x1, y1)
else:
x2 = x
y2 = y
code2 = computeCode(x2, y2)
if accept:
print("Line accepted from %.2f, %.2f to %.2f, %.2f" % (x1, y1, x2, y2))
else:
print("Line rejected")
cohenSutherlandClip(5, 5, 7, 7)
cohenSutherlandClip(7, 9, 11, 4)
cohenSutherlandClip(1, 5, 4, 1)
|
C#
using System;
public class GFG{
public const int INSIDE = 0;
public const int LEFT = 1;
public const int RIGHT = 2;
public const int BOTTOM = 4;
public const int TOP = 8;
public const int x_max = 10;
public const int y_max = 8;
public const int x_min = 4;
public const int y_min = 4;
public static int computeCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
public static void cohenSutherlandClip(double x1, double y1,
double x2, double y2)
{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
bool accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
}
else if ((code1 & code2)!=0) {
break;
}
else {
int code_out;
double x=0.0;
double y=0.0;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
if ((code_out & TOP)!=0) {
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if ((code_out & BOTTOM)!=0) {
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if ((code_out & RIGHT)!=0) {
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if ((code_out & LEFT)!=0) {
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
Console.WriteLine("Line accepted from "+x1+", "
+y1+" to "+x2+", "+y2);
}
else
Console.WriteLine("Line rejected");
}
static public void Main (){
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(7, 9, 11, 4);
cohenSutherlandClip(1, 5, 4, 1);
}
}
|
Javascript
const INSIDE = 0;
const LEFT = 1;
const RIGHT = 2;
const BOTTOM = 4;
const TOP = 8;
const x_max = 10;
const y_max = 8;
const x_min = 4;
const y_min = 4;
function computeCode(x, y)
{
let code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
function cohenSutherlandClip(x1, y1, x2, y2)
{
let code1 = computeCode(x1, y1);
let code2 = computeCode(x2, y2);
let accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
}
else if (code1 & code2) {
break;
}
else {
let code_out;
let x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
if (code_out & TOP) {
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if (code_out & BOTTOM) {
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if (code_out & RIGHT) {
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if (code_out & LEFT) {
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
console.log("Line accepted from", x1, ",", y1, "to", x2, ",", y2);
}
else
console.log("Line rejected");
}
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(7, 9, 11, 4);
cohenSutherlandClip(1, 5, 4, 1);
|
Output:
Line accepted from 5.00, 5.00 to 7.00, 7.00
Line accepted from 7.80, 8.00 to 10.00, 5.25
Line rejected
Below is C++ code with Graphics using graphics.h
C++
#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], byte[4], i, var;
setcolor(RED);
bits[0] = sign(xmin - mylines.x1);
byte[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
byte[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
byte[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
byte[3] = sign(mylines.y2 - ymax);
string initial = "", end = "", temp = "";
for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (byte[i] == 0)
end += '0';
else
end += '1';
}
float m = (mylines.y2 - mylines.y1) / (float)(mylines.x2 - mylines.x1);
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] & byte[i]);
if (val == 0)
temp += '0';
else
temp += '1';
}
if (temp != "0000")
return;
for (i = 0; i < 4; i++) {
if (bits[i] == byte[i])
continue;
if (i == 0 && bits[i] == 1) {
var = round(m * xmin + c);
mylines.y1 = var;
mylines.x1 = xmin;
}
if (i == 0 && byte[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 && byte[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 && byte[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 && byte[i] == 1) {
var = round((float)(ymax - c) / m);
mylines.y2 = ymax;
mylines.x2 = var;
}
bits[0] = sign(xmin - mylines.x1);
byte[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
byte[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
byte[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
byte[3] = sign(mylines.y2 - ymax);
}
initial = "", end = "";
for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (byte[i] == 0)
end += '0';
else
end += '1';
}
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].x1 = 30;
mylines[0].y1 = 65;
mylines[0].x2 = 55;
mylines[0].y2 = 30;
mylines[1].x1 = 60;
mylines[1].y1 = 20;
mylines[1].x2 = 100;
mylines[1].y2 = 90;
mylines[2].x1 = 60;
mylines[2].y1 = 100;
mylines[2].x2 = 80;
mylines[2].y2 = 70;
mylines[3].x1 = 85;
mylines[3].y1 = 50;
mylines[3].x2 = 120;
mylines[3].y2 = 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 Cohen–Sutherland algorithm can be used only on a rectangular clip window. For other convex polygon clipping windows, Cyrus–Beck algorithm is used. We will be discussing Cyrus–Beck Algorithm in next set.
Related Post :
Polygon Clipping | Sutherland–Hodgman Algorithm
Point Clipping Algorithm in Computer Graphics
Reference:
1) https://en.wikipedia.org/wiki/Cohen–Sutherland_algorithm
2)Book:- Computer Graphics: By Donald Hearn, M. Pauline Baker
3) College notes
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...