 GeeksforGeeks App
Open App Browser
Continue

# Line Clipping | Set 1 (Cohen–Sutherland Algorithm)

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.

1. Completely inside the given rectangle : Bitwise OR of region of two end points of line is 0 (Both points are inside the rectangle)
2. 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).
3. 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++

 `// C++ program to implement Cohen Sutherland algorithm``// for line clipping.``#include ``using` `namespace` `std;` `// Defining region codes``const` `int` `INSIDE = 0; ``// 0000``const` `int` `LEFT = 1; ``// 0001``const` `int` `RIGHT = 2; ``// 0010``const` `int` `BOTTOM = 4; ``// 0100``const` `int` `TOP = 8; ``// 1000` `// Defining x_max, y_max and x_min, y_min for``// clipping rectangle. Since diagonal points are``// enough to define a rectangle``const` `int` `x_max = 10;``const` `int` `y_max = 8;``const` `int` `x_min = 4;``const` `int` `y_min = 4;` `// Function to compute region code for a point(x, y)``int` `computeCode(``double` `x, ``double` `y)``{``    ``// initialized as being inside``    ``int` `code = INSIDE;` `    ``if` `(x < x_min) ``// to the left of rectangle``        ``code |= LEFT;``    ``else` `if` `(x > x_max) ``// to the right of rectangle``        ``code |= RIGHT;``    ``if` `(y < y_min) ``// below the rectangle``        ``code |= BOTTOM;``    ``else` `if` `(y > y_max) ``// above the rectangle``        ``code |= TOP;` `    ``return` `code;``}` `// Implementing Cohen-Sutherland algorithm``// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)``void` `cohenSutherlandClip(``double` `x1, ``double` `y1,``                         ``double` `x2, ``double` `y2)``{``    ``// Compute region codes for P1, P2``    ``int` `code1 = computeCode(x1, y1);``    ``int` `code2 = computeCode(x2, y2);` `    ``// Initialize line as outside the rectangular window``    ``bool` `accept = ``false``;` `    ``while` `(``true``) {``        ``if` `((code1 == 0) && (code2 == 0)) {``            ``// If both endpoints lie within rectangle``            ``accept = ``true``;``            ``break``;``        ``}``        ``else` `if` `(code1 & code2) {``            ``// If both endpoints are outside rectangle,``            ``// in same region``            ``break``;``        ``}``        ``else` `{``            ``// Some segment of line lies within the``            ``// rectangle``            ``int` `code_out;``            ``double` `x, y;` `            ``// At least one endpoint is outside the``            ``// rectangle, pick it.``            ``if` `(code1 != 0)``                ``code_out = code1;``            ``else``                ``code_out = code2;` `            ``// Find intersection point;``            ``// using formulas y = y1 + slope * (x - x1),``            ``// x = x1 + (1 / slope) * (y - y1)``            ``if` `(code_out & TOP) {``                ``// point is above the clip rectangle``                ``x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);``                ``y = y_max;``            ``}``            ``else` `if` `(code_out & BOTTOM) {``                ``// point is below the rectangle``                ``x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);``                ``y = y_min;``            ``}``            ``else` `if` `(code_out & RIGHT) {``                ``// point is to the right of rectangle``                ``y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);``                ``x = x_max;``            ``}``            ``else` `if` `(code_out & LEFT) {``                ``// point is to the left of rectangle``                ``y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);``                ``x = x_min;``            ``}` `            ``// Now intersection point x, y is found``            ``// We replace point outside rectangle``            ``// by intersection point``            ``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;``        ``// Here the user can add code to display the rectangle``        ``// along with the accepted (portion of) lines``    ``}``    ``else``        ``cout << ``"Line rejected"` `<< endl;``}` `// Driver code``int` `main()``{``    ``// First Line segment``    ``// P11 = (5, 5), P12 = (7, 7)``    ``cohenSutherlandClip(5, 5, 7, 7);` `    ``// Second Line segment``    ``// P21 = (7, 9), P22 = (11, 4)``    ``cohenSutherlandClip(7, 9, 11, 4);` `    ``// Third Line segment``    ``// P31 = (1, 5), P32 = (4, 1)``    ``cohenSutherlandClip(1, 5, 4, 1);` `    ``return` `0;``}`

## Java

 `// Java program to implement Cohen Sutherland algorithm``// for line clipping.``import` `java.io.*;` `class` `GFG {` `    ``// Defining region codes``    ``static` `final` `int` `INSIDE = ``0``; ``// 0000``    ``static` `final` `int` `LEFT = ``1``; ``// 0001``    ``static` `final` `int` `RIGHT = ``2``; ``// 0010``    ``static` `final` `int` `BOTTOM = ``4``; ``// 0100``    ``static` `final` `int` `TOP = ``8``; ``// 1000` `    ``// Defining x_max, y_max and x_min, y_min for``    ``// clipping rectangle. Since diagonal points are``    ``// enough to define a rectangle``    ``static` `final` `int` `x_max = ``10``;``    ``static` `final` `int` `y_max = ``8``;``    ``static` `final` `int` `x_min = ``4``;``    ``static` `final` `int` `y_min = ``4``;` `    ``// Function to compute region code for a point(x, y)``    ``static` `int` `computeCode(``double` `x, ``double` `y)``    ``{``        ``// initialized as being inside``        ``int` `code = INSIDE;` `        ``if` `(x < x_min) ``// to the left of rectangle``            ``code |= LEFT;``        ``else` `if` `(x > x_max) ``// to the right of rectangle``            ``code |= RIGHT;``        ``if` `(y < y_min) ``// below the rectangle``            ``code |= BOTTOM;``        ``else` `if` `(y > y_max) ``// above the rectangle``            ``code |= TOP;` `        ``return` `code;``    ``}` `    ``// Implementing Cohen-Sutherland algorithm``    ``// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)``    ``static` `void` `cohenSutherlandClip(``double` `x1, ``double` `y1,``                                    ``double` `x2, ``double` `y2)``    ``{``        ``// Compute region codes for P1, P2``        ``int` `code1 = computeCode(x1, y1);``        ``int` `code2 = computeCode(x2, y2);` `        ``// Initialize line as outside the rectangular window``        ``boolean` `accept = ``false``;` `        ``while` `(``true``) {``            ``if` `((code1 == ``0``) && (code2 == ``0``)) {``                ``// If both endpoints lie within rectangle``                ``accept = ``true``;``                ``break``;``            ``}``            ``else` `if` `((code1 & code2) != ``0``) {``                ``// If both endpoints are outside rectangle,``                ``// in same region``                ``break``;``            ``}``            ``else` `{``                ``// Some segment of line lies within the``                ``// rectangle``                ``int` `code_out;``                ``double` `x = ``0``, y = ``0``;` `                ``// At least one endpoint is outside the``                ``// rectangle, pick it.``                ``if` `(code1 != ``0``)``                    ``code_out = code1;``                ``else``                    ``code_out = code2;` `                ``// Find intersection point;``                ``// using formulas y = y1 + slope * (x - x1),``                ``// x = x1 + (1 / slope) * (y - y1)``                ``if` `((code_out & TOP) != ``0``) {``                    ``// point is above the clip rectangle``                    ``x = x1``                        ``+ (x2 - x1) * (y_max - y1)``                              ``/ (y2 - y1);``                    ``y = y_max;``                ``}``                ``else` `if` `((code_out & BOTTOM) != ``0``) {``                    ``// point is below the rectangle``                    ``x = x1``                        ``+ (x2 - x1) * (y_min - y1)``                              ``/ (y2 - y1);``                    ``y = y_min;``                ``}``                ``else` `if` `((code_out & RIGHT) != ``0``) {``                    ``// point is to the right of rectangle``                    ``y = y1``                        ``+ (y2 - y1) * (x_max - x1)``                              ``/ (x2 - x1);``                    ``x = x_max;``                ``}``                ``else` `if` `((code_out & LEFT) != ``0``) {``                    ``// point is to the left of rectangle``                    ``y = y1``                        ``+ (y2 - y1) * (x_min - x1)``                              ``/ (x2 - x1);``                    ``x = x_min;``                ``}` `                ``// Now intersection point x, y is found``                ``// We replace point outside rectangle``                ``// by intersection point``                ``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);``            ``// Here the user can add code to display the``            ``// rectangle along with the accepted (portion``            ``// of) lines``        ``}``        ``else``            ``System.out.println(``"Line rejected"``);``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``// First Line segment``        ``// P11 = (5, 5), P12 = (7, 7)``        ``cohenSutherlandClip(``5``, ``5``, ``7``, ``7``);` `        ``// Second Line segment``        ``// P21 = (7, 9), P22 = (11, 4)``        ``cohenSutherlandClip(``7``, ``9``, ``11``, ``4``);` `        ``// Third Line segment``        ``// P31 = (1, 5), P32 = (4, 1)``        ``cohenSutherlandClip(``1``, ``5``, ``4``, ``1``);``    ``}``}` `// This code is contributed by jain_mudit.`

## Python3

 `# Python program to implement Cohen Sutherland algorithm``# for line clipping.` `# Defining region codes``INSIDE ``=` `0`  `# 0000``LEFT ``=` `1`  `# 0001``RIGHT ``=` `2`  `# 0010``BOTTOM ``=` `4`  `# 0100``TOP ``=` `8`  `# 1000` `# Defining x_max, y_max and x_min, y_min for rectangle``# Since diagonal points are enough to define a rectangle``x_max ``=` `10.0``y_max ``=` `8.0``x_min ``=` `4.0``y_min ``=` `4.0`  `# Function to compute region code for a point(x, y)``def` `computeCode(x, y):``    ``code ``=` `INSIDE``    ``if` `x < x_min:  ``# to the left of rectangle``        ``code |``=` `LEFT``    ``elif` `x > x_max:  ``# to the right of rectangle``        ``code |``=` `RIGHT``    ``if` `y < y_min:  ``# below the rectangle``        ``code |``=` `BOTTOM``    ``elif` `y > y_max:  ``# above the rectangle``        ``code |``=` `TOP``    ``return` `code`  `# Implementing Cohen-Sutherland algorithm``# Clipping a line from P1 = (x1, y1) to P2 = (x2, y2)``# Implementing Cohen-Sutherland algorithm``# Clipping a line from P1 = (x1, y1) to P2 = (x2, y2)``def` `cohenSutherlandClip(x1, y1, x2, y2):` `    ``# Compute region codes for P1, P2``    ``code1 ``=` `computeCode(x1, y1)``    ``code2 ``=` `computeCode(x2, y2)``    ``accept ``=` `False` `    ``while` `True``:` `        ``# If both endpoints lie within rectangle``        ``if` `code1 ``=``=` `0` `and` `code2 ``=``=` `0``:``            ``accept ``=` `True``            ``break` `        ``# If both endpoints are outside rectangle``        ``elif` `(code1 & code2) !``=` `0``:``            ``break` `        ``# Some segment lies within the rectangle``        ``else``:` `            ``# Line needs clipping``            ``# At least one of the points is outside,``            ``# select it``            ``x ``=` `1.0``            ``y ``=` `1.0``            ``if` `code1 !``=` `0``:``                ``code_out ``=` `code1``            ``else``:``                ``code_out ``=` `code2` `            ``# Find intersection point``            ``# using formulas y = y1 + slope * (x - x1),``            ``# x = x1 + (1 / slope) * (y - y1)``            ``if` `code_out & TOP:``                ``# Point is above the clip rectangle``                ``x ``=` `x1 ``+` `(x2 ``-` `x1) ``*` `(y_max ``-` `y1) ``/` `(y2 ``-` `y1)``                ``y ``=` `y_max``            ``elif` `code_out & BOTTOM:``                ``# Point is below the clip rectangle``                ``x ``=` `x1 ``+` `(x2 ``-` `x1) ``*` `(y_min ``-` `y1) ``/` `(y2 ``-` `y1)``                ``y ``=` `y_min``            ``elif` `code_out & RIGHT:``                ``# Point is to the right of the clip rectangle``                ``y ``=` `y1 ``+` `(y2 ``-` `y1) ``*` `(x_max ``-` `x1) ``/` `(x2 ``-` `x1)``                ``x ``=` `x_max``            ``elif` `code_out & LEFT:``                ``# Point is to the left of the clip rectangle``                ``y ``=` `y1 ``+` `(y2 ``-` `y1) ``*` `(x_min ``-` `x1) ``/` `(x2 ``-` `x1)``                ``x ``=` `x_min` `            ``# Now intersection point (x, y) is found``            ``# We replace point outside clipping rectangle``            ``# by intersection point``            ``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))` `        ``# Here the user can add code to display the rectangle``        ``# along with the accepted (portion of) lines` `    ``else``:``        ``print``(``"Line rejected"``)` `# Driver script` `# First line segment``# P11 = (5, 5), P12 = (7, 7)``cohenSutherlandClip(``5``, ``5``, ``7``, ``7``)` `# Second line segment``# P21 = (7, 9), P22 = (11, 4)``cohenSutherlandClip(``7``, ``9``, ``11``, ``4``)` `# Third line segment``# P31 = (1, 5), P32 = (4, 1)``cohenSutherlandClip(``1``, ``5``, ``4``, ``1``)`

## C#

 `using` `System;` `public` `class` `GFG{``  ` `// C# program to implement Cohen Sutherland algorithm``// for line clipping.` `// Defining region codes``public` `const` `int` `INSIDE = 0; ``// 0000``public` `const` `int` `LEFT = 1; ``// 0001``public` `const` `int` `RIGHT = 2; ``// 0010``public` `const` `int` `BOTTOM = 4; ``// 0100``public` `const` `int` `TOP = 8; ``// 1000` `// Defining x_max, y_max and x_min, y_min for``// clipping rectangle. Since diagonal points are``// enough to define a rectangle``public` `const` `int` `x_max = 10;``public` `const` `int` `y_max = 8;``public` `const` `int` `x_min = 4;``public` `const` `int` `y_min = 4;` `// Function to compute region code for a point(x, y)``public` `static` `int` `computeCode(``double` `x, ``double` `y)``{``    ``// initialized as being inside``    ``int` `code = INSIDE;` `    ``if` `(x < x_min) ``// to the left of rectangle``        ``code |= LEFT;``    ``else` `if` `(x > x_max) ``// to the right of rectangle``        ``code |= RIGHT;``    ``if` `(y < y_min) ``// below the rectangle``        ``code |= BOTTOM;``    ``else` `if` `(y > y_max) ``// above the rectangle``        ``code |= TOP;` `    ``return` `code;``}` `// Implementing Cohen-Sutherland algorithm``// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)``public` `static` `void` `cohenSutherlandClip(``double` `x1, ``double` `y1,``                         ``double` `x2, ``double` `y2)``{``    ``// Compute region codes for P1, P2``    ``int` `code1 = computeCode(x1, y1);``    ``int` `code2 = computeCode(x2, y2);` `    ``// Initialize line as outside the rectangular window``    ``bool` `accept = ``false``;` `    ``while` `(``true``) {``        ``if` `((code1 == 0) && (code2 == 0)) {``            ``// If both endpoints lie within rectangle``            ``accept = ``true``;``            ``break``;``        ``}``        ``else` `if` `((code1 & code2)!=0) {``            ``// If both endpoints are outside rectangle,``            ``// in same region``            ``break``;``        ``}``        ``else` `{``            ``// Some segment of line lies within the``            ``// rectangle``            ``int` `code_out;``            ``double` `x=0.0;``            ``double` `y=0.0;` `            ``// At least one endpoint is outside the``            ``// rectangle, pick it.``            ``if` `(code1 != 0)``                ``code_out = code1;``            ``else``                ``code_out = code2;` `            ``// Find intersection point;``            ``// using formulas y = y1 + slope * (x - x1),``            ``// x = x1 + (1 / slope) * (y - y1)``            ``if` `((code_out & TOP)!=0) {``                ``// point is above the clip rectangle``                ``x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);``                ``y = y_max;``            ``}``            ``else` `if` `((code_out & BOTTOM)!=0) {``                ``// point is below the rectangle``                ``x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);``                ``y = y_min;``            ``}``            ``else` `if` `((code_out & RIGHT)!=0) {``                ``// point is to the right of rectangle``                ``y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);``                ``x = x_max;``            ``}``            ``else` `if` `((code_out & LEFT)!=0) {``                ``// point is to the left of rectangle``                ``y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);``                ``x = x_min;``            ``}` `            ``// Now intersection point x, y is found``            ``// We replace point outside rectangle``            ``// by intersection point``            ``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);``        ``// Here the user can add code to display the rectangle``        ``// along with the accepted (portion of) lines``    ``}``    ``else``        ``Console.WriteLine(``"Line rejected"``);``}` `// Driver code` `    ``static` `public` `void` `Main (){``        ``// First Line segment``    ``// P11 = (5, 5), P12 = (7, 7)``    ``cohenSutherlandClip(5, 5, 7, 7);` `    ``// Second Line segment``    ``// P21 = (7, 9), P22 = (11, 4)``    ``cohenSutherlandClip(7, 9, 11, 4);` `    ``// Third Line segment``    ``// P31 = (1, 5), P32 = (4, 1)``    ``cohenSutherlandClip(1, 5, 4, 1);` `        ` `    ``}``}` `// This code is contributed by akashish__`

## Javascript

 `// JavaScript program to implement Cohen Sutherland algorithm``// for line clipping.` `// Defining region codes``const INSIDE = 0; ``// 0000``const LEFT = 1; ``// 0001``const RIGHT = 2; ``// 0010``const BOTTOM = 4; ``// 0100``const TOP = 8; ``// 1000` `// Defining x_max, y_max and x_min, y_min for``// clipping rectangle. Since diagonal points are``// enough to define a rectangle``const x_max = 10;``const y_max = 8;``const x_min = 4;``const y_min = 4;` `// Function to compute region code for a point(x, y)``function` `computeCode(x, y)``{``    ``// initialized as being inside``    ``let code = INSIDE;` `    ``if` `(x < x_min) ``// to the left of rectangle``        ``code |= LEFT;``    ``else` `if` `(x > x_max) ``// to the right of rectangle``        ``code |= RIGHT;``    ``if` `(y < y_min) ``// below the rectangle``        ``code |= BOTTOM;``    ``else` `if` `(y > y_max) ``// above the rectangle``        ``code |= TOP;` `    ``return` `code;``}` `// Implementing Cohen-Sutherland algorithm``// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)``function` `cohenSutherlandClip(x1, y1, x2, y2)``{``    ``// Compute region codes for P1, P2``    ``let code1 = computeCode(x1, y1);``    ``let code2 = computeCode(x2, y2);` `    ``// Initialize line as outside the rectangular window``    ``let accept = ``false``;` `    ``while` `(``true``) {``        ``if` `((code1 == 0) && (code2 == 0)) {``            ``// If both endpoints lie within rectangle``            ``accept = ``true``;``            ``break``;``        ``}``        ``else` `if` `(code1 & code2) {``            ``// If both endpoints are outside rectangle,``            ``// in same region``            ``break``;``        ``}``        ``else` `{``            ``// Some segment of line lies within the``            ``// rectangle``            ``let code_out;``            ``let x, y;` `            ``// At least one endpoint is outside the``            ``// rectangle, pick it.``            ``if` `(code1 != 0)``                ``code_out = code1;``            ``else``                ``code_out = code2;` `            ``// Find intersection point;``            ``// using formulas y = y1 + slope * (x - x1),``            ``// x = x1 + (1 / slope) * (y - y1)``            ``if` `(code_out & TOP) {``                ``// point is above the clip rectangle``                ``x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);``                ``y = y_max;``            ``}``            ``else` `if` `(code_out & BOTTOM) {``                ``// point is below the rectangle``                ``x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);``                ``y = y_min;``            ``}``            ``else` `if` `(code_out & RIGHT) {``                ``// point is to the right of rectangle``                ``y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);``                ``x = x_max;``            ``}``            ``else` `if` `(code_out & LEFT) {``                ``// point is to the left of rectangle``                ``y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);``                ``x = x_min;``            ``}` `            ``// Now intersection point x, y is found``            ``// We replace point outside rectangle``            ``// by intersection point``            ``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);``        ``// Here the user can add code to display the rectangle``        ``// along with the accepted (portion of) lines``    ``}``    ``else``        ``console.log(``"Line rejected"``);``}` `// Driver code``// First Line segment``// P11 = (5, 5), P12 = (7, 7)``cohenSutherlandClip(5, 5, 7, 7);` `// Second Line segment``// P21 = (7, 9), P22 = (11, 4)``cohenSutherlandClip(7, 9, 11, 4);` `// Third Line segment``// P31 = (1, 5), P32 = (4, 1)``cohenSutherlandClip(1, 5, 4, 1);` `// The code is contributed by Nidhi goel`

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++

 `// C++ program to implement Cohen Sutherland algorithm``// for line clipping.``// including libraries``#include ``#include ``using` `namespace` `std;` `// Global Variables``int` `xmin, xmax, ymin, ymax;` `// Lines where co-ordinates are (x1, y1) and (x2, y2)``struct` `lines {``    ``int` `x1, y1, x2, y2;``};` `// This will return the sign required.``int` `sign(``int` `x)``{``    ``if` `(x > 0)``        ``return` `1;``    ``else``        ``return` `0;``}` `// CohenSutherLand LineClipping Algorithm As Described in theory.``// This will clip the lines as per window boundaries.``void` `clip(``struct` `lines mylines)``{``    ``// arrays will store bits``    ``// Here bits implies initial Point whereas byte implies end points``    ``int` `bits, byte, i, var;``    ``// setting color of graphics to be RED``    ``setcolor(RED);` `    ``// Finding Bits``    ``bits = sign(xmin - mylines.x1);``    ``byte = sign(xmin - mylines.x2);``    ``bits = sign(mylines.x1 - xmax);``    ``byte = sign(mylines.x2 - xmax);``    ``bits = sign(ymin - mylines.y1);``    ``byte = sign(ymin - mylines.y2);``    ``bits = sign(mylines.y1 - ymax);``    ``byte = sign(mylines.y2 - ymax);` `    ``// initial will used for initial coordinates and end for final``    ``string initial = ``""``, end = ``""``, temp = ``""``;` `    ``// convert bits to string``    ``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'``;``    ``}` `    ``// finding slope of line y=mx+c as (y-y1)=m(x-x1)+c``    ``// where m is slope m=dy/dx;` `    ``float` `m = (mylines.y2 - mylines.y1) / (``float``)(mylines.x2 - mylines.x1);``    ``float` `c = mylines.y1 - m * mylines.x1;` `    ``// if both points are inside the Accept the line and draw``    ``if` `(initial == end && end == ``"0000"``) {``        ``// inbuilt function to draw the line from(x1, y1) to (x2, y2)``        ``line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);``        ``return``;``    ``}` `    ``// this will contain cases where line maybe totally outside for partially inside``    ``else` `{``        ``// taking bitwise end of every value``        ``for` `(i = 0; i < 4; i++) {` `            ``int` `val = (bits[i] & byte[i]);``            ``if` `(val == 0)``                ``temp += ``'0'``;``            ``else``                ``temp += ``'1'``;``        ``}``        ``// as per algo if AND is not 0000 means line is completely outside hence draw nothing and return``        ``if` `(temp != ``"0000"``)``            ``return``;` `        ``// Here contain cases of partial inside or outside``        ``// So check for every boundary one by one``        ``for` `(i = 0; i < 4; i++) {``            ``// if both bit are same hence we cannot find any intersection with boundary so continue``            ``if` `(bits[i] == byte[i])``                ``continue``;``            ``// Otherwise there exist a intersection` `            ``// Case when initial point is in left xmin``            ``if` `(i == 0 && bits[i] == 1) {``                ``var = round(m * xmin + c);``                ``mylines.y1 = var;``                ``mylines.x1 = xmin;``            ``}``            ``// Case when final point is in left xmin``            ``if` `(i == 0 && byte[i] == 1) {``                ``var = round(m * xmin + c);``                ``mylines.y2 = var;``                ``mylines.x2 = xmin;``            ``}``            ``// Case when initial point is in right of xmax``            ``if` `(i == 1 && bits[i] == 1) {``                ``var = round(m * xmax + c);``                ``mylines.y1 = var;``                ``mylines.x1 = xmax;``            ``}``            ``// Case when final point is in right of xmax``            ``if` `(i == 1 && byte[i] == 1) {``                ``var = round(m * xmax + c);``                ``mylines.y2 = var;``                ``mylines.x2 = xmax;``            ``}``            ``// Case when initial point is in top of ymin``            ``if` `(i == 2 && bits[i] == 1) {``                ``var = round((``float``)(ymin - c) / m);``                ``mylines.y1 = ymin;``                ``mylines.x1 = var;``            ``}``            ``// Case when final point is in top of ymin``            ``if` `(i == 2 && byte[i] == 1) {``                ``var = round((``float``)(ymin - c) / m);``                ``mylines.y2 = ymin;``                ``mylines.x2 = var;``            ``}``            ``// Case when initial point is in bottom of ymax``            ``if` `(i == 3 && bits[i] == 1) {``                ``var = round((``float``)(ymax - c) / m);``                ``mylines.y1 = ymax;``                ``mylines.x1 = var;``            ``}``            ``// Case when final point is in bottom of ymax``            ``if` `(i == 3 && byte[i] == 1) {``                ``var = round((``float``)(ymax - c) / m);``                ``mylines.y2 = ymax;``                ``mylines.x2 = var;``            ``}``            ``// Updating Bits at every point``            ``bits = sign(xmin - mylines.x1);``            ``byte = sign(xmin - mylines.x2);``            ``bits = sign(mylines.x1 - xmax);``            ``byte = sign(mylines.x2 - xmax);``            ``bits = sign(ymin - mylines.y1);``            ``byte = sign(ymin - mylines.y2);``            ``bits = sign(mylines.y1 - ymax);``            ``byte = sign(mylines.y2 - ymax);``        ``} ``// end of for loop``        ``// Initialize initial and end to NULL``        ``initial = ``""``, end = ``""``;``        ``// Updating strings again by bit``        ``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 now both points lie inside or on boundary then simply draw the updated line``        ``if` `(initial == end && end == ``"0000"``) {``            ``line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);``            ``return``;``        ``}``        ``// else line was completely outside hence rejected``        ``else``            ``return``;``    ``}``}` `// Driver Function``int` `main()``{``    ``int` `gd = DETECT, gm;` `    ``// Setting values of Clipping window``    ``xmin = 40;``    ``xmax = 100;``    ``ymin = 40;``    ``ymax = 80;` `    ``// initialize the graph``    ``initgraph(&gd, &gm, NULL);` `    ``// Drawing Window using Lines``    ``line(xmin, ymin, xmax, ymin);``    ``line(xmax, ymin, xmax, ymax);``    ``line(xmax, ymax, xmin, ymax);``    ``line(xmin, ymax, xmin, ymin);` `    ``// Assume 4 lines to be clipped``    ``struct` `lines mylines;` `    ``// Setting the coordinated of  4 lines``    ``mylines.x1 = 30;``    ``mylines.y1 = 65;``    ``mylines.x2 = 55;``    ``mylines.y2 = 30;` `    ``mylines.x1 = 60;``    ``mylines.y1 = 20;``    ``mylines.x2 = 100;``    ``mylines.y2 = 90;` `    ``mylines.x1 = 60;``    ``mylines.y1 = 100;``    ``mylines.x2 = 80;``    ``mylines.y2 = 70;` `    ``mylines.x1 = 85;``    ``mylines.y1 = 50;``    ``mylines.x2 = 120;``    ``mylines.y2 = 75;` `    ``// Drawing Initial Lines without clipping``    ``for` `(``int` `i = 0; i < 4; i++) {``        ``line(mylines[i].x1, mylines[i].y1,``             ``mylines[i].x2, mylines[i].y2);``        ``delay(1000);``    ``}` `    ``// Drawing clipped Line``    ``for` `(``int` `i = 0; i < 4; i++) {``        ``// Calling clip() which in term clip the line as per window and draw it``        ``clip(mylines[i]);``        ``delay(1000);``    ``}``    ``delay(4000);``    ``getch();``    ``// For Closing the graph.``    ``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.

2)Book:-  Computer Graphics: By Donald Hearn, M. Pauline Baker

3) College notes

This article is contributed by Saket Modi and Anirudh Sharma. 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.