Computer graphics program to implement Cohen-Sutherland Line Clipping Algorithm

0
684

Cohen-Sutherland Line Clipping Graphics Algorithm

Computer graphics program in C++

Cohen-Sutherland Line Clipping Algorithm

C++

 

#include <iostream>
#include<stdio.h>
#include<graphics.h>
using namespace std;

int x_max = 200;
int y_max = 200;
int x_min = 100;
int y_min = 50;

int INSIDE = 0;
int LEFT = 1;
int RIGHT = 2;
int BOTTOM = 4;
int TOP = 8;

void draw(float x1, float y1, float x2, float y2)
{
    float h = (float)getwindowheight();
    float w = (float)getwindowwidth();
    line(x1+w/2, (h/2)-y1,x2+w/2, (h/2)-y2);
}
void drawBoundary()
{
    draw(x_min,y_min, x_max, y_min);
    draw(x_min, y_min, x_min, y_max);
    draw(x_max, y_min, x_max, y_max);
    draw(x_min,y_max, x_max, y_max);
}
int compute(double x, double y)
{
    int code = INSIDE;
    if (x < x_min)       code |= LEFT;
    else if (x > x_max)  code |= RIGHT;
    if (y < y_min)       code |= BOTTOM;
    else if (y > y_max)  code |= TOP;
    return code;
}
void ClearDev()
{
    float h = (float)getwindowheight();
    float w = (float)getwindowwidth();
    cleardevice();
    line(0,h/2, w, h/2);
    line(w/2,0,w/2,h);
}

void cohenSutherland(double x1, double y1,
                      double x2, double y2)
{

    int code1 = compute(x1, y1);
    int code2 = compute(x2, y2);
    bool accept = false;
    while (true)
    {
        if ((code1 == 0) && (code2 == 0))
        {
            accept = true;
            break;
        }
        else if (code1 & code2)
        {
            break;
        }
        else
        {
            int code_out;
            double x, y;
            if (code1 != 0)
                code_out = code1;
            else
                code_out = code2;
            if (code_out & TOP)
            {
                x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
                y = y_max;
            }
            else if (code_out & BOTTOM)
            {
                x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
                y = y_min;
            }
            else if (code_out & RIGHT)
            {
                y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
                x = x_max;
            }
            else if (code_out & LEFT)
            {
                y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
                x = x_min;
            }

            if (code_out == code1)
            {
                x1 = x;
                y1 = y;
                code1 = compute(x1, y1);
            }
            else
            {
                x2 = x;
                y2 = y;
                code2 = compute(x2, y2);
            }
        }
    }
    if (accept)
    {
        cout <<"Line accepted from " << x1 << ", "
             << y1 << " to "<< x2 << ", " << y2 << endl;
        ClearDev();
        drawBoundary();
        draw(x1,y1,x2,y2);
    }
    else
        cout << "Line rejected" << endl;
}

int main()
{
    int gd = DETECT, gm;
    initgraph (&gd, &gm, "");
    setcolor(CYAN);
    float h = (float)getwindowheight();
    float w = (float)getwindowwidth();
    line(0,h/2, w, h/2);
    line(w/2,0,w/2,h);
    drawBoundary();
    int x1=25,y1=25,x2=200,y2=225;
    draw(x1,y1,x2,y2);
    printf("Clip\n");
    int ans;
    cin>>ans;
    if(ans)
        cohenSutherland(x1,y1,x2,y2);
    getch();
    return 0;
}
Cohen-Sutherland Line Clipping Graphics Algorithm

LEAVE A REPLY