Cohen-Sutherland Line Clipping Algorithm - Graphics & Multimedia Lab - C++ Program

Cohen-Sutherland Line Clipping Algorithm (2)

  C++ program - Graphics & Multimedia Lab
Sourcecode:
/* 
 * File:   cohsuthlineclip.cpp
 * Author: nn
 *
 * Created on 12 January, 2012, 11:30 AM
 *  
 * Cohen–Sutherland clipping algorithm clips a line from
 *P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with 
 *diagonal from (xmin, ymin) to (xmax, ymax).
 */

#include <cstdlib>
#include <graphics.h>
#define xmax 350
#define ymax 350
#define xmin 100
#define ymin 100
using namespace std;

typedef int OutCode;
  
const int INSIDE = 0; // 0000
const int LEFT = 1;   // 0001
const int RIGHT = 2;  // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8;    // 1000

OutCode ComputeOutCode(double x, double y)
{
        OutCode code;
 
        code = INSIDE;          // initialised as being inside of clip window
 
        if (x < xmin)           // to the left of clip window
                code |= LEFT;
        else if (x > xmax)      // to the right of clip window
                code |= RIGHT;
        if (y < ymin)           // below the clip window
                code |= BOTTOM;
        else if (y > ymax)      // above the clip window
                code |= TOP;
 
        return code;
}
void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1)
{ 
        OutCode outcode0 = ComputeOutCode(x0, y0);
        OutCode outcode1 = ComputeOutCode(x1, y1);
        bool accept = false;
 
        while (true) {
                if (!(outcode0 | outcode1)) {//Bitwise OR is 0. Trivially accept and get out of loop
                        accept = true;
                        break;
                } else if (outcode0 & outcode1) {//Bitwise AND is not 0. Trivially reject and get out of loop
                        break;
                } 
                else {

                        double x, y;

                        OutCode outcodeOut = outcode0? outcode0 : outcode1;
                        if (outcodeOut&TOP){           // point is above the clip rectangle
                                x = x0 + (x1-x0)*(ymax-y0)/(y1-y0);
                                y = ymax;
                        } else if(outcodeOut & BOTTOM) { // point is below the clip rectangle
                                x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
                                y = ymin;
                        } else if (outcodeOut & RIGHT) {  // point is to the right of clip rectangle
                                y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
                                x = xmax;
                        } else if (outcodeOut & LEFT) {   // point is to the left of clip rectangle
                                y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
                                x = xmin;
                        }

                        if (outcodeOut == outcode0) {
                                x0 = x;
                                y0 = y;
                                outcode0 = ComputeOutCode(x0, y0);
                        } else {
                                x1 = x;
                                y1 = y;
                                outcode1 = ComputeOutCode(x1, y1);
                        }
                }
               rectangle(xmin, ymin, xmax, ymax);
                line(x0, y0, x1, y1);
                getch();
               // cleardevice();
        }
        
        if(accept) {
               
               rectangle(xmin, ymin, xmax, ymax);
               line(x0, y0, x1, y1);
               printf("\n\tpoints p0=(%d,%d) p1=(%d,%d)",(int)x0,(int)y0,(int)x1,(int)y1);
        }
}

int main(int argc, char** argv)
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
    outtextxy(300,10,"BEFORE LINE CLIPPING");
    rectangle(xmin,ymin,xmax,ymax);
    line(10,200,350,300);
    getch();
    cleardevice();
    outtextxy(300,10,"AFTER LINE CLIPPING");
    CohenSutherlandLineClipAndDraw(10,200,350,300);
    getch();
    closegraph();
    return 0;
}




Output:

nn@linuxmint ~ $ g++ cohsuthlineclip.cpp -lgraph
nn@linuxmint ~ $ g++ cohsuthlineclip.cpp -lgraph
coh.cpp: In function ‘int main(int, char**)’:
coh.cpp:105: warning: deprecated conversion from string constant to ‘char*’
coh.cpp:110: warning: deprecated conversion from string constant to ‘char*’
nn@linuxmint ~ $ ./a.out 


2 comments:

Related Posts Plugin for WordPress, Blogger...