Clipping Algorithms

Cohen-Sutherland Line Clipping Algorithm

The Cohen-Sutherland algorithm clips a line against a rectangular window. It assigns region codes to both endpoints, quickly accepts fully visible lines, rejects fully invisible lines, and calculates intersections only when needed.

Published May 28, 2026 - 11 min read

What is line clipping?

Line clipping is the process of removing the part of a line that lies outside a viewing window. Only the portion inside the window is displayed.

In computer graphics, clipping is useful because the screen or viewport has fixed boundaries. Objects outside those boundaries should not be drawn.

Region codes in Cohen-Sutherland algorithm

The rectangular clipping window divides the plane into nine regions. Each endpoint receives a 4-bit region code:

TOP = 1000
BOTTOM = 0100
RIGHT = 0010
LEFT = 0001

If a point is inside the clipping window, its region code is 0000. If it is outside, one or more bits are set depending on its position.

Accept and reject rules

  • Trivial accept: if both endpoint codes are 0000, the full line is visible.
  • Trivial reject: if the bitwise AND of both codes is not 0000, the full line is outside on the same side.
  • Partial clipping: otherwise, calculate an intersection with the clipping window and repeat.

Steps of Cohen-Sutherland Line Clipping

  1. Define the clipping window using xmin, ymin, xmax, and ymax.
  2. Read the two endpoints of the line.
  3. Compute the region code for each endpoint.
  4. If both codes are zero, accept the line.
  5. If the bitwise AND of the two codes is non-zero, reject the line.
  6. Otherwise, clip the outside endpoint to a window boundary and repeat.

Cohen-Sutherland Line Clipping program in C using graphics.h

This program asks for two endpoints, draws the clipping window, draws the original line in red, and then draws the clipped visible line in green.

C / Line Clipping
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>

#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

int xmin = 150, ymin = 120, xmax = 450, ymax = 340;

int computeCode(float x, float y)
{
    int code = 0;

    if (x < xmin)
        code = code | LEFT;
    else if (x > xmax)
        code = code | RIGHT;

    if (y < ymin)
        code = code | TOP;
    else if (y > ymax)
        code = code | BOTTOM;

    return code;
}

void cohenSutherlandClip(float x1, float y1, float x2, float y2)
{
    int code1 = computeCode(x1, y1);
    int code2 = computeCode(x2, y2);
    int accept = 0;
    float x, y;
    int codeOut;

    while (1)
    {
        if ((code1 == 0) && (code2 == 0))
        {
            accept = 1;
            break;
        }
        else if (code1 & code2)
        {
            break;
        }
        else
        {
            if (code1 != 0)
                codeOut = code1;
            else
                codeOut = code2;

            if (codeOut & TOP)
            {
                x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
                y = ymin;
            }
            else if (codeOut & BOTTOM)
            {
                x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
                y = ymax;
            }
            else if (codeOut & RIGHT)
            {
                y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
                x = xmax;
            }
            else
            {
                y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
                x = xmin;
            }

            if (codeOut == code1)
            {
                x1 = x;
                y1 = y;
                code1 = computeCode(x1, y1);
            }
            else
            {
                x2 = x;
                y2 = y;
                code2 = computeCode(x2, y2);
            }
        }
    }

    if (accept)
    {
        setcolor(GREEN);
        line(x1, y1, x2, y2);
        outtextxy(170, 360, "Green line is the clipped visible line");
    }
    else
    {
        outtextxy(170, 360, "Line is outside the clipping window");
    }
}

int main()
{
    int gd = DETECT, gm;
    float x1, y1, x2, y2;

    printf("Enter x1 y1: ");
    scanf("%f %f", &x1, &y1);

    printf("Enter x2 y2: ");
    scanf("%f %f", &x2, &y2);

    initgraph(&gd, &gm, "");

    setcolor(WHITE);
    rectangle(xmin, ymin, xmax, ymax);
    outtextxy(xmin, ymin - 20, "Clipping Window");

    setcolor(RED);
    line(x1, y1, x2, y2);
    outtextxy(170, 380, "Red line is the original line");
    getch();

    cleardevice();
    setcolor(WHITE);
    rectangle(xmin, ymin, xmax, ymax);
    cohenSutherlandClip(x1, y1, x2, y2);

    getch();
    closegraph();
    return 0;
}

Try it online: open the graphics.h online compiler, paste the program, then use sample inputs 100 160 and 520 310.

Advantages of Cohen-Sutherland algorithm

  • It quickly accepts lines that are fully inside the window.
  • It quickly rejects lines that are fully outside on the same side.
  • It calculates intersections only for partially visible lines.
  • It is easy to implement using bitwise operations.

Limitations

  • It is designed for rectangular clipping windows.
  • For complex polygon clipping, other algorithms are needed.
  • Partially visible lines may require repeated intersection calculations.

Common mistakes

Using the wrong y-direction

In graphics.h, smaller y-values are higher on the screen. That is why y < ymin means the point is above the window and gets the TOP code.

Forgetting the bitwise AND reject test

If code1 & code2 is non-zero, both endpoints share an outside region and the line can be rejected immediately.

Dividing by zero

Horizontal and vertical lines need careful intersection logic. The common algorithm avoids invalid divisions by clipping only against boundaries that the outside code actually crosses.