## Pages

### Liang-Barsky Algorithm CG

This program is for implementing Liang Barsky Algorithm. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2

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

main()
{
int i, gm,gd;
int x1, x2, y1, y2, xmin, xmax, ymin, ymax, xx1, xx2, yy1, yy2;
float t1, t2, p, q, temp;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:/tc/bgi/");
xmin=150;
xmax=450;
ymin=100;
ymax=400;
rectangle(xmin,ymin,xmax,ymax);

printf("Enter the starting point: ");
scanf("%d%d",&x1,&y1);
printf("Enter the ending point: ");
scanf("%d%d",&x2,&y2);
printf("Line before clipping:");
line(x1,y1,x2,y2);
getch();
clrscr();

printf("Line after clipping:");
rectangle(xmin,ymin,xmax,ymax);

p=-(x2-x1);
p=(x2-x1);
p=-(y2-y1);
p=(y2-y1);
q=(x1-xmin);
q=(xmax-x1);
q=(y1-ymin);
q=(ymax-y1);

for(i=0;i<4;i++)
{
if(p[i]==0)
{
printf("\nLine is parallel to one of the clipping boundary");
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}
if(y2>ymax)
{
y2=ymax;
}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}
if(x2>xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);
}
}
getch();
return(0);
}
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.