---
title: "Cohen-Sutherland line clipping"
subject: "Computer Graphics"
chapter: 3
key: "semester_5/computer_graphics:cohen-sutherland-explain"
source: https://www.bctengineer.com/q/semester_5/computer_graphics/cohen-sutherland-explain
site: BCT Engineering
---

# Cohen-Sutherland line clipping

Computer Graphics · Chapter 3: Two –Dimensional Transformations

- Asked in: 2074 Chaitra, 8 marks; 2078 Bhadra, 4 marks (asked 2 times)
- Video: [Cohen Sutherland Line Clipping Algorithm | Computer Graphics](https://www.youtube.com/watch?v=l2ywWjNiG1g) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_5/computer_graphics/cohen-sutherland-explain
- Whole chapter: https://www.bctengineer.com/md/semester_5/computer_graphics/chapter-3

## Question

Explain the Sutherland-Cohen (Cohen-Sutherland) line clipping algorithm with an example.

## Short answer

Cohen-Sutherland clipping uses 4-bit region codes T B R L to test acceptance/rejection: both endpoints 0000 accept; AND nonzero reject; otherwise intersect with window edges. For window (0,0)-(50,50) and line (-20,20)-(60,80), clipping yields visible portion from (0,35) to (20,50).

## Answer

The **Cohen-Sutherland algorithm** clips a line against a rectangular window quickly, by first testing trivially for acceptance or rejection using 4-bit **region codes**.

#### Region codes
The plane is divided into 9 regions around the window; each end point gets a code of 4 bits **T B R L**:

```text
 1001 | 1000 | 1010
 -----+------+-----
 0001 | 0000 | 0010      (middle = window)
 -----+------+-----
 0101 | 0100 | 0110
```
- bit 1 (L) = 1 if x < xmin;  bit 2 (R) = 1 if x > xmax
- bit 3 (B) = 1 if y < ymin;  bit 4 (T) = 1 if y > ymax

#### Algorithm
```text
1. Find the region codes of P1 and P2.
2. If both codes are 0000: the line is completely inside, accept.
3. If (code1 AND code2) ≠ 0000: both end points are outside the same
   edge, completely outside, reject.
4. Otherwise (partly inside): 
   a. choose an end point with a non-zero code;
   b. find the intersection of the line with the window edge indicated
      by the first bit set (L, R, B, T order);
        x = xmin or xmax:  y = y1 + m(x − x1)
        y = ymin or ymax:  x = x1 + (y − y1)/m
   c. replace the end point by the intersection point, recompute its code;
   d. go to step 2.
```

#### Example
Window: xmin = 0, ymin = 0, xmax = 50, ymax = 50. Line P1(−20,20) to P2(60,80). Slope m = (80−20)/(60+20) = 0.75.

```text
P1=(-20,20) code 0001; P2=(60,80) code 1010
Clip P1 against x = xmin = 0: new point (0,35)
P1=(0,35) code 0000; P2=(60,80) code 1010
Clip P2 against x = xmax = 50: new point (50,72.5)
P1=(0,35) code 0000; P2=(50,72.5) code 1000
Clip P2 against y = ymax = 50: new point (20,50)
P1=(0,35) code 0000; P2=(20,50) code 0000
Both codes 0000: accept
```

Codes: P1 = 0001 (left), P2 = 1010 (top and right). AND = 0000, so the line is partly visible and needs clipping. After the clipping steps above, the visible part is from (0,35) to (20,50).

**Answer: clipped line (0,35) to (20,50).** A line such as (10,10)-(40,40) (both codes 0000) is accepted directly, and (60,10)-(70,40) (both codes 0010, AND ≠ 0) is rejected directly.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When is a line trivially accepted?
   A. Both end point codes are 0000
   B. The AND of codes is non-zero
   C. Either code is 0000
   D. Codes are equal
   Answer: A. Both ends lie in the window. (Key point: Trivial accept)

2. When is a line trivially rejected?
   A. Either code is non-zero
   B. The bitwise AND of the two codes is not 0000
   C. The OR of codes is 0000
   D. Both codes are 0000
   Answer: B. A shared outside bit means both ends are on the same outer side. (Key point: Trivial reject)

3. A point with code 1010 lies:
   A. below the bottom and to the left
   B. above the top and to the left
   C. above the top edge and to the right of the window
   D. only to the right
   Answer: C. Bits are T B R L; 1010 sets T and R. (Key point: Region code)

4. To clip against x = xmin the intersection y is found from:
   A. x = x1 + (y − y1)/m with y = ymin
   B. y = m·x1
   C. y = y1 + xmin/m
   D. y = y1 + m(x − x1) with x = xmin
   Answer: D. For vertical edges use the line equation with x fixed; e.g. P1(−20,20) clipped at x = 0 gives y = 35. (Key point: Intersection)

## Related in Computer Graphics

- [Point clipping and Cohen-Sutherland (5,130)-(50,5)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acohen-sutherland-point-clip-5-130) — Computer Graphics, chapter 3
- [Cohen-Sutherland: window (10,10)-(40,40)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acohen-sutherland-window-10-40) — Computer Graphics, chapter 3
- [Cohen-Sutherland: two lines vs window A-D](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acohen-sutherland-two-lines-window-a) — Computer Graphics, chapter 3
- [Cohen-Sutherland: (0,120)-(130,5)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acohen-sutherland-p1-0-120) — Computer Graphics, chapter 3
