Logo 
Search:

C++ Programming Articles

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

Program of Liang Barsky Algorithm for Line Clipping

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

Write a program of Liang Barsky Algorithm for Line Clipping

Code for Program of Liang Barsky Algorithm for Line Clipping in C++ Programming

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<math.h>

#definefalse 0
#definetrue  1

FILE *fin = fopen("cosuth.in","r");
float u1,u2,dx,dy;
float t1,t2,t3,t4,t5,t6;

class point
{
     int x,y;
     int code[4];
public:
    void getxy()
    {
        fscanf(fin,"%d %d",&x,&y);
    }
    void putxy(int x1,int y1)
    {
        x = x1;
        y = y1;
    }
    void friend clipline(point p1,point p2);
    int friend cliptest(float p,float q,float *u1,float *u2);
    void friend drawline(point p1,point p2);

}p1,p2,pt[20],winmin,winmax;

void clipline(point p1,point p2)
{
    rectangle(80,420,426,160);

    u1 = 0.0;
    u2 = 1.0;

    dx = p2.x - p1.x;
    t1 = p1.x - winmin.x;
    t2 = winmax.x - p1.x;
    t3 = p1.y - winmin.y;
    t4 = winmax.y - p1.y;
    t5 = -1 * dx;


    if (cliptest(t5,t1,&u1,&u2) == 1)
    {
        if (cliptest(dx,t2,&u1,&u2) == 1)
        {
            dy = float(p2.y - p1.y);
            t6 = -1 * dy;
            if (cliptest(t6,t3,&u1,&u2) == 1)
            {
                if (cliptest(dy,t4,&u1,&u2) == 1)
                {
                    if (u1 > 0.0)
                    {
                        (int)p1.x = p1.x + (u1 * dx);
                        (int)p1.y = p1.y + (u1 * dy);
                    }
                    if (u2 < 1.0)
                    {
                        (int)p2.x = p1.x + (u2 * dx);
                        (int)p2.y = p1.y + (u2 * dy);
                    }
                }
            }
        }
    }
    drawline(p1,p2);
    getch();
}

int cliptest(float p,float q,float *u1,float *u2)
{
    float r;
    int flag;

    flag = 1;

    if (p < 0.0)
    {
        r = (float) q / p;
        if (r > *u2)
        {
            flag = 0;
        }
        else
        {
            if (r > *u1)
            {
                *u1 = r;
            }
        }
    }
    else
    {
        if (p > 0.0)
        {
            r = (float) q / p;
            if (r < *u1)
            {
                flag = 0;
            }
            else
            {
                if (r < *u2)
                {
                    *u2 = r;
                }
            }
        }
        else
        {
            if (q < 0.0)
            {
                flag = 0;
            }
        }
    }
    return(flag);
}

void drawline(point p1,point p2)
{
     line(p1.x,p1.y,p2.x,p2.y);
}


void main()
{
    int drv=DETECT,mode,totalpoint,i;

    clrscr();

    printf("\nHow many line will be on the screen : ");
    fscanf(fin,"%d",&totalpoint);

    for(i = 0;i < totalpoint ; i++)
    {
        printf("\nEnter their x and y coordinates of %d point:\n",i+1);
        pt[i].getxy();
    }

    initgraph(&drv,&mode,"c:\\Borlandc\\bgi");
    winmin.putxy(80,160);
    winmax.putxy(420,426);
    rectangle(80,420,426,160);

    i = 0;

    do
    {
        drawline(pt[i],pt[i+1]);
        i++;
    }
    while(i < totalpoint-1);

    getch();
    clearviewport();

    i = 0;
    do
    {
        clipline(pt[i],pt[i+1]);
        i++;
    }
    while(i < totalpoint-1);
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program of Liang Barsky Algorithm for Line Clipping Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of Liang Barsky Algorithm for Line Clipping 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

 

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!