Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Computer GraphicsRSS Feeds

Program of Line clipping by Cohen Sutherland

Posted By: Easy Tutor     Category: C++ Programming     Views: 12287

Write a program of line clipping by Cohen Sutherland

Code for Program of Line clipping by Cohen Sutherland in C++ Programming

#include <iostream.h>
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
FILE *fp;

class clipping
{
private:
    int x;
    int y;
    int code[4];
public:
    clipping();
    void getxy(char p[5]);
    void encode(int left,int right,int bottom,int top);
    int getcode(int k);
    float getx(void);
    float gety(void);
    friend int accept(clipping p1,clipping p2);
    friend int reject(clipping p1,clipping p2);
    friend void swap_points(clipping &p1,clipping &p2);
    int point_inside(void);
    void clipping::operator =(clipping p2);
    void changex(int winy,float m);
    void changey(int winx,float m);
};

clipping::clipping()
{
    int i;
    x = 0;
    y = 0;
    for(i=0;i<4;i++)
        code[i] = 0;
}

float clipping::getx(void)
{
    return(x);
}

float clipping::gety(void)
{
    return(y);
}

void clipping::getxy(char p[5])
{
    cout << "\nEnter the coordinates of point " << p;
    cout << "\nX :-  ";
    fscanf(fp,"%d",&x);
    cout << "\nY :-  ";
    fscanf(fp,"%d",&y);
}

void clipping::encode(int left,int right,int bottom,int top)
{
    if(x < left)
        code[0] = 1;
    else
        code[0] = 0;
    if (x > right)
        code[1] = 1;
    else
        code[1] = 0;
    if (y > bottom)
        code[2] = 1;
    else
        code[2] = 0;
    if (y < top)
        code[3] = 1;
    else
        code[3] = 0;
}

int clipping::getcode(int k)
{
    if (code[k] == 1)
        return(1);
    return(0);
}

int accept(clipping p1,clipping p2)
{
    int k;
    for(k=0;k<4;k++)
        if (p1.getcode(k) || p2.getcode(k))
            return(0);
    return(1);
}

int reject(clipping p1,clipping p2)
{
    int k;
    for(k=0;k<4;k++)
        if (p1.getcode(k) && p2.getcode(k))
            return(1);
    return(0);
}

int clipping::point_inside(void)
{
    if (code[0] || code[1] || code[2] || code[3])
        return(0);
    return(1);
}

void clipping::operator =(clipping p2)
{
    int i;
    x = p2.x;
    y = p2.y;
    for (i=0;i<4;i++)
        code[i] = p2.code[i];
}

void swap_points(clipping &p1,clipping &p2)
{
    clipping tmp;
    tmp = p1;
    p1 = p2;
    p2 = tmp;
}

void clipping::changex(int winy,float m)
{
    x = x + (winy - y) / m;
    y = winy;
}

void clipping::changey(int winx,float m)
{
    y = y + (winx - x) * m;
    x = winx;
}

void main()
{
    int driver = DETECT,mode;
    initgraph(&driver,&mode,"c:\\tc\\bgi");
    clipping p1,p2,tmp;
    int left,top,right,bottom,done=0,draw=0;
    float m;
    fp = fopen("cohensut.in","r");
    clearviewport();
    p1.getxy("p1");
    p2.getxy("p2");
    left = getmaxx() / 2 - 50;
    right = getmaxx() / 2 + 50;
    bottom = getmaxy() / 2 + 50;
    top = getmaxy() / 2 - 50;
    clearviewport();
    rectangle(left,top,right,bottom);
    line(p1.getx(),p1.gety(),p2.getx(),p2.gety());
    while(done == 0)
    {
        p1.encode(left,right,bottom,top);
        p2.encode(left,right,bottom,top);
        if (accept(p1,p2))
        {
            done = 1;
            draw = 1;
        }
        elseif (reject(p1,p2))
        {
            done = 1;
        }
        else
        {
            if (p1.point_inside())
                swap_points(p1,p2);
            m = (p2.gety() - p1.gety()) / (p2.getx() - p1.getx());
            if(p1.getcode(0))
                p1.changey(left,m);
            elseif(p1.getcode(1))
                p1.changey(right,m);
            elseif (p1.getcode(2))
                p1.changex(bottom,m);
            elseif (p1.getcode(3))
                p1.changex(top,m);
        }
        cout << "\nPress any key to continue...\n";
        getch();
        clearviewport();
        rectangle(left,top,right,bottom);
        line(p1.getx(),p1.gety(),p2.getx(),p2.gety());
    }
    if (draw)
    {
        rectangle(left,top,right,bottom);
        line(p1.getx(),p1.gety(),p2.getx(),p2.gety());
    }
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program of Line clipping by Cohen Sutherland Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of Line clipping by Cohen Sutherland is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!