Liang Barsky Line Clipping Algorithm in C and C++

Here you will find out about Liang Barsky line clipping algorithm in C and C++.

This Algorithm was created by Liang and Barsky. It is utilized for line clipping as it is more

effective than Cyrus Beck algorithm and Cohen Sutherland algorithm since it utilizes

increasingly proficient parametric conditions to cut the given line.

These parametric conditions are given as:

x = x1 + tdx

y = y1 + tdy, 0 <= t <= 1

Where dx = x2 – x1 and dy = y2 – y1

Liang Barsky line clipping algorithm utilizes 4 imbalances with 2 parameters

p and q which are characterized in the algorithm underneath.

Algorithm

Peruse 2 endpoints of line as p1 (x1, y1) and p2 (x2, y2).

Peruse 2 corners (left-top and right-base) of the clipping window as (xwmin, ywmin, xwmax, ywmax).

Compute estimations of parameters pi and qi for I = 1, 2, 3, 4 with the end goal that

p1 = – dx, q1 = x1 – xwmin

p2 = dx, q2 = xwmax – x1

p3 = – dy, q3 = y1 – ywmin

p4 = dy, q4 = ywmax – y1

on the off chance that pi = 0, at that point line is parallel to ith limit

on the off chance that qi < 0, at that point line is totally outside limit so dispose of line

else, check whether line is even or vertical and afterward check the line endpoints with the relating limits.

Instate t1 and t2 as

t1 = 0 and t2 = 1

Ascertain values for qi/pi for I = 1, 2, 3, 4.

Select estimations of qi/pi where pi < 0 and allot most extreme out of them as t1.

Select estimations of qi/pi where pi > 0 and allot least out of them as t2.

on the off chance that (t1 < t2)

{

xx1 = x1 + t1dx

xx2 = x1 + t2dx

yy1 = y1 + t1dy

yy2 = y1 + t2dy

line (xx1, yy1, xx2, yy2)

}

Stop.

Points of interest

More productive than different algorithms as line crossing point with limits computations are diminished.

Crossing points of the line are processed just once.

Program for Liang Barsky Line Clipping Algorithm in C and C++

C Program

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
 
void main()
{
	int i,gd=DETECT,gm;
	int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
	float t1,t2,p[4],q[4],temp;
	
	x1=120;
	y1=120;
	x2=300;
	y2=300;
	
	xmin=100;
	ymin=100;
	xmax=250;
	ymax=250;
	
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
	rectangle(xmin,ymin,xmax,ymax);
	dx=x2-x1;
	dy=y2-y1;
	
	p[0]=-dx;
	p[1]=dx;
	p[2]=-dy;
	p[3]=dy;
	
	q[0]=x1-xmin;
	q[1]=xmax-x1;
	q[2]=y1-ymin;
	q[3]=ymax-y1;
	
	for(i=0;i<4;i++)
	{
		if(p[i]==0)
		{
			printf("line 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);
				}
			}
		}
	}
	
	t1=0;
	t2=1;
	
	for(i=0;i<4;i++)
	{
		temp=q[i]/p[i];
		
		if(p[i]<0)
		{
			if(t1<=temp)
				t1=temp;
		}
		else
		{
			if(t2>temp)
				t2=temp;
		}
	}
	
	if(t1<t2)
	{
		xx1 = x1 + t1 * p[1];
		xx2 = x1 + t2 * p[1];
		yy1 = y1 + t1 * p[3];
		yy2 = y1 + t2 * p[3];
		line(xx1,yy1,xx2,yy2);
	}
	
	delay(5000);
	closegraph();
}

C++ Program

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
 
void main()
{
	int i,gd=DETECT,gm;
	int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
	float t1,t2,p[4],q[4],temp;
	
	x1=120;
	y1=120;
	x2=300;
	y2=300;
	
	xmin=100;
	ymin=100;
	xmax=250;
	ymax=250;
	
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
	rectangle(xmin,ymin,xmax,ymax);
	dx=x2-x1;
	dy=y2-y1;
	
	p[0]=-dx;
	p[1]=dx;
	p[2]=-dy;
	p[3]=dy;
	
	q[0]=x1-xmin;
	q[1]=xmax-x1;
	q[2]=y1-ymin;
	q[3]=ymax-y1;
	
	for(i=0;i<4;i++)
	{
		if(p[i]==0)
		{
			cout<<"line 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);
				}
			}
		}
	}
	
	t1=0;
	t2=1;
	
	for(i=0;i<4;i++)
	{
		temp=q[i]/p[i];
		
		if(p[i]<0)
		{
			if(t1<=temp)
				t1=temp;
		}
		else
		{
			if(t2>temp)
				t2=temp;
		}
	}
	
	if(t1<t2)
	{
		xx1 = x1 + t1 * p[1];
		xx2 = x1 + t2 * p[1];
		yy1 = y1 + t1 * p[3];
		yy2 = y1 + t2 * p[3];
		line(xx1,yy1,xx2,yy2);
	}
	
	delay(5000);
	closegraph();
}

Output

Liang Barsky Line Clipping Algorithm in C and C++

Leave a Comment

error: Alert: Content is protected!!