Write a c program to clip a line using Cohen and Sutherland line clipping algorithm.

Here Below is the C program that implements the Cohen-Sutherland Line Clipping Algorithm. This algorithm is used to clip a line segment to a rectangular viewport defined by the window's coordinates. The program determines whether the line is entirely inside, partially inside, or entirely outside the clipping window.

	#include <stdio.h>
	#include <stdlib.h>
	#include <graphics.h>

    // Define the region codes
    #define INSIDE 0  // 0000
    #define LEFT 1    // 0001
    #define RIGHT 2   // 0010
    #define BOTTOM 4  // 0100
    #define TOP 8     // 1000
    
    // Define the clipping window (viewport)
    int xmin = 100, ymin = 100, xmax = 300, ymax = 300;
    
    // Compute the region code for a point (x, y)
    int computeCode(int x, int y) {
        int code = INSIDE;
    
        if (x < xmin)  // to the left of the window
            code |= LEFT;
        else if (x > xmax)  // to the right of the window
            code |= RIGHT;
        if (y < ymin)  // below the window
            code |= BOTTOM;
        else if (y > ymax)  // above the window
            code |= TOP;
    
        return code;
    }
    
    // Cohen-Sutherland line clipping algorithm
    void cohenSutherlandClip(int x1, int y1, int x2, int y2) {
        int code1 = computeCode(x1, y1);
        int code2 = computeCode(x2, y2);
        int accept = 0;
    
        while (1) {
            if ((code1 == 0) && (code2 == 0)) {
                // Both endpoints are inside the viewport
                accept = 1;
                break;
            } else if (code1 & code2) {
                // Both endpoints are outside the viewport, and on the same side
                break;
            } else {
                // Some portion of the line is inside the viewport
                int codeOut;
                int x, y;
    
                // Determine which endpoint is outside the viewport
                if (code1 != 0)
                    codeOut = code1;
                else
                    codeOut = code2;
    
                // Find the intersection point
                if (codeOut & TOP) {
                    x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
                    y = ymax;
                } else if (codeOut & BOTTOM) {
                    x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
                    y = ymin;
                } else if (codeOut & RIGHT) {
                    y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
                    x = xmax;
                } else if (codeOut & LEFT) {
                    y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
                    x = xmin;
                }
    
                // Update the outside point to the intersection point
                if (codeOut == code1) {
                    x1 = x;
                    y1 = y;
                    code1 = computeCode(x1, y1);
                } else {
                    x2 = x;
                    y2 = y;
                    code2 = computeCode(x2, y2);
                }
            }
        }
    
        // If the line is accepted, plot it
        if (accept) {
            line(x1, y1, x2, y2);
        } else {
            printf("Line rejected\n");
        }
    }
    
    int main() {
        int gd = DETECT, gm;
        initgraph(&gd, &gm, "");  // Initialize graphics mode
    
        // Draw the clipping window (rectangular viewport)
        rectangle(xmin, ymin, xmax, ymax);
    
        int x1, y1, x2, y2;
        printf("Enter the coordinates of the first endpoint of the line (x1, y1): ");
        scanf("%d %d", &x1, &y1);
        printf("Enter the coordinates of the second endpoint of the line (x2, y2): ");
        scanf("%d %d", &x2, &y2);
    
        // Call the Cohen-Sutherland line clipping algorithm
        cohenSutherlandClip(x1, y1, x2, y2);
    
        // Wait for the user to press a key before closing the graphics window
        getch();
        closegraph();
        return 0;
    }