Clipping Lines with Cohen-Sutherland Algorithm in C++

Cohen-Sutherland Algorithm in C++

Clipping Lines with Cohen-Sutherland Algorithm in C++

Clipping is a fundamental concept in computer graphics that involves determining the visible portion of an object and discarding the invisible part. The Cohen-Sutherland Line Clipping Algorithm is a popular method for clipping lines against a rectangular window. In this blog post, we’ll explore the basics of the algorithm and provide a simple C++ implementation.

Understanding the Cohen-Sutherland Algorithm

The Cohen-Sutherland Line Clipping Algorithm divides the 2D plane into nine regions. These regions are determined by comparing the coordinates of each endpoint of a line segment with the boundaries of a rectangular clipping window. The regions are:

  • Inside (0000): Both endpoints are inside the window.
  • Left (0001): Endpoint is to the left of the window.
  • Right (0010): Endpoint is to the right of the window.
  • Bottom (0100): Endpoint is below the window.
  • Top (1000): Endpoint is above the window.
  • Bottom-Left (0101): Endpoint is in the bottom-left corner.
  • Bottom-Right (0110): Endpoint is in the bottom-right corner.
  • Top-Left (1001): Endpoint is in the top-left corner.
  • Top-Right (1010): Endpoint is in the top-right corner.

The algorithm then iteratively applies clipping rules to determine the visible portion of the line segment.

Implementing the Algorithm in C++

#include <iostream>
using namespace std;

// Region codes
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;

// Window boundaries
const int xLeft = 50;
const int xRight = 100;
const int yBottom = 50;
const int yTop = 100;

int computeRegionCode(int x, int y) {
    int code = INSIDE;

    if (x < xLeft)
        code |= LEFT;
    else if (x > xRight)
        code |= RIGHT;

    if (y < yBottom)
        code |= BOTTOM;
    else if (y > yTop)
        code |= TOP;

    return code;
}

void cohenSutherland(int x1, int y1, int x2, int y2) {
    int code1 = computeRegionCode(x1, y1);
    int code2 = computeRegionCode(x2, y2);
    bool accept = false;

    while (true) {
        if ((code1 == 0) && (code2 == 0)) {
            accept = true;
            break;
        } else if (code1 & code2) {
            break;
        } else {
            int code = (code1 != 0) ? code1 : code2;

            int x, y;

            if (code & TOP) {
                x = x1 + (x2 - x1) * (yTop - y1) / (y2 - y1);
                y = yTop;
            } else if (code & BOTTOM) {
                x = x1 + (x2 - x1) * (yBottom - y1) / (y2 - y1);
                y = yBottom;
            } else if (code & RIGHT) {
                y = y1 + (y2 - y1) * (xRight - x1) / (x2 - x1);
                x = xRight;
            } else if (code & LEFT) {
                y = y1 + (y2 - y1) * (xLeft - x1) / (x2 - x1);
                x = xLeft;
            }

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

    if (accept) {
        cout << "Line accepted from (" << x1 << ", " << y1 << ") to (" << x2 << ", " << y2 << ")" << endl;
    } else {
        cout << "Line rejected" << endl;
    }
}

int main() {
    cohenSutherland(30, 40, 120, 110);

    return 0;
}

Conclusion

The Cohen-Sutherland Line Clipping Algorithm is a powerful tool for efficiently determining the visible part of a line segment within a rectangular window. This simple C++ implementation provides a clear illustration of the algorithm’s principles. Understanding and implementing such algorithms are crucial steps for anyone interested in computer graphics and computational geometry.

Feel free to experiment with different line coordinates and window boundaries to see how the algorithm performs in various scenarios. Happy coding!