Computer Graphics
Computer Graphics
LAB
Aim:
Program to implement DDA algorithm.
Description:
In a 2-D plane if we connect two points (x0, y0) and (x1, y1), we get a
line segment. But in computer graphics we can’t directly join any two
coordinate points, for that we calculate intermediate point’s coordinate
and put a pixel for each intermediate point, of the desired color with
help of functions. For generating any line segment we need
intermediate points and for calculating them we have can use a basic
algorithm called DDA (Digital differential analyzer) line generating
algorithm.
Algorithm:
Digital Differential Analyzer (DDA) algorithm is the simple line
generation algorithm which is explained step by step here.
Step 1 − Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2 − Calculate the difference between two end points.
dx = X1 - X0
dy = Y1 - Y0
Steps = absolute(dx);
else
Steps = absolute(dy);
x = x + Xincrement ;
y = y + Yincrement ;
Code:
Result:
Discussion:
Advantages:
1. It is the simplest algorithm and it does not require special skills for
implementation.
2. It is a faster method for calculating pixel positions than the direct use of equation
y = mx + b. It eliminates the multiplication in the equation by making use of raster
characteristics.
Disadvantages:
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. Hence end point accuracy is poor.
Aim:
Program to implement Bresenham algorithm.
Description:
An accurate and efficient raster line-generating algorithm, developed by
Bresenham, which uses only incremental integer calculations. In addition, it can
also be adapted to display circles and other curves. The basic principle is to find
the optimum raster location to represent the straight line. To accomplish this, the
algorithm always increments x/y by one unit based on the flow of the line. Then,
the increment in other variable is found on the basis of the distance between the
actual line location & the nearest pixel.
Algorithm:
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point.
3. calculate δx, δy, 2δy and 2δy-δx
4. obtain P0 as follows:
5. P0 = 2δy - δx
6. δx = |X2-X1|
7. δy = |Y2-Y1|
8. At each xk along the line, starting at k = 0, perform the following test:
9. if(Pk < 0):
10. next_point = (Xk+1,Yk)
11. Pk+1 = Pk + 2δy
12. else:
13. next_point = (Xk+1,Yk+1)
14. Pk+1 = Pk + 2δy -2δx
15. Repeat lines 9-14 for δx-1 times.
Code:
Result:
Aim:
To draw a circle using the Mid-Point Algorithm
Description:
It runs the same screen pass as in the Bresenham’s Algorithm and in each screen
pass 8 points are drawn simultaneously. This is possible by the dual-axis symmetry
of the circle. Only the first half of the first quadrant is calculated, rest of the
points are their symmetric reflections along different axis.
Algorithm:
1. Input radius r and circle center (xc , yc )
2. Set the coordinates for first point as:
3. (X0,Y0) = (0,r)
4. Calculate the initial value of the decision parameter as:
5. P0 = 5/4 - r
6. At each xk position, starting at k = 0, perform the following test:
7. if(Pk < 0):
8. next_point = (Xk+1,Yk)
9. Pk+1 = Pk + 2Xk+1 + 1
10. else:
11. next_point = (Xk+1, Yk-1)
12. Pk+1 = Pk + 2Xk+1 + 1 - 2Yk+1
13. //2xk+1 = 2xk + 2 and 2yk+1 = 2yk − 2.
14. Determine symmetry points in the other seven octants.
15. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and plot the coordinate
values as follows:
16. x = x + Xc
17. y = y + Yc
18. repeat Lines 6-17 until x>=y
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void pixel(int xc,int yc,int x,int y);
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,Pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
printf("*** Mid-Point Subdivision algorithm of circle ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of Yc \t");
scanf("%d",&yc);
printf("Enter the Radius of circle\t");
scanf("%d",&r);
x=0;
y=r;
Pk=1-r;
pixel(xc,yc,x,y);
while(x<y)
{
if(Pk<0)
{
x=x+1;
Pk=Pk+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+(2*x)-(2*y)+1;
}
pixel(xc,yc,x,y);
}
getch();
closegraph();
}
void pixel(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
putpixel(xc-x,yc+y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc-x,7);
putpixel(xc+x,yc-y,7);
Results:
Aim:
To draw an ellipse using the Mid-Point Algorithm
Description:
● Ellipses in Computer Graphics:
o Just like lines, ellipses are another primitive shape used in computer
graphics.
o An ellipse is defined as the set of points such that the sum of the
distances from two fixed positions (foci) is the same for all points. If
the distances to the two foci from any point on the ellipse are labeled
d 1 and d 2 , then the general equation of an ellipse can be stated as :
o Ellipse equations are greatly simplified if the major and minor axes
are oriented to align with the coordinate axes. An ellipse in "standard
position" has major and minor axes oriented parallel to the x and y
axes respectively.
Algorithm:
1. Input radii rx and ry and ellipse center (xc, yc)
2. Set the coordinates for first point as:
3. (X0,Y0) = (0,r)
4. Calculate the initial value of the decision parameter as:
5. P10 = ry^2 + rx^2/4 - rx^2ry
6. At each xk position in octant 1, starting at k = 0, perform the following test:
7. if (P1k < 0):
8. next_point = (Xk+1,Yk)
9. P1 (k+1) = P1k + 2ry^2x(k+1) + ry^2
10. Else:
11. next_point = (Xk+1, Yk-1)
12. P1(k+1) = P1k + 2ry^2x(k+1) + ry^2 - 2rx^2y(k+1)
14. Determine symmetry points in the other three octants.
15. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and
plot the coordinate values as follows:
16. x = x + Xc
17. y = y + Yc
18. Repeat Lines 7-17 until 2ry^2x >= 2rx^2y.
19. Calculate the initial value of the decision parameter as:
20. p20 = ry^2(x0 + 1/2)^2 + rx^2(y0 - 1)^2 - rx^2ry^2
21. At each xk position in octant 2, starting at k = 0, perform the following test:
22. if(P2k > 0):
23. next_point = (Xk,Yk-1)
24. P2(k+1) = P2k - 2rx^2y(k+1) + rx^2
25. else:
26. next_point = (Xk+1,Yk-1)
27. P2(k+1) = P2k - 2rx^2y(k+1) + rx^2 - 2rx^2y(k+1)
28. Determine symmetry points in the other three octants.
29. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and
plot the coordinate values as follows:
30. x = x + Xc
31. y = y + Yc
32. Repeat lines 22 - 31 till y > 0
Code:
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}
Result:
Aim:
To write a program and implement Flood Fill Algorithm
Description:
Sometimes we come across an object where we want to fill the area and its
boundary with different colors. We can paint such objects with a specified interior
color instead of searching for particular boundary color as in boundary filling
algorithm.
Instead of relying on the boundary of the object, it relies on the fill color. In other
words, it replaces the interior color of the object with the fill color. When no more
pixels of the original interior color exist, the algorithm is completed.
In Flood Fill algorithm we start with some seed and examine the neighboring
pixels, however pixels are checked for a specified interior color instead of
boundary color and is replaced by a new color. It can be done using 4 connected
or 8 connected region method.
ALGORITHM:
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till the
boundary pixels reached.
Step 4 − Change the default color with the fill color at the seed point.
Step 6 − Exit
There is a problem with this technique. Consider the case as shown below where we tried to fill
the entire region. Here, the image is filled only partially. In such cases, 4-connected pixels
technique cannot be used.
CODE:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
voidfloodFill(intx,inty,intoldcolor,intnewcolor)
{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel
int main()
{
intgm,gd=DETECT,radius;
intx,y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(x,y,radius);
floodFill(x,y,0,15);
delay(5000);
closegraph();
return 0;
}
OUTPUT:
Aim:
To implement Boundary Fill Algorithm
Description:
The boundary fill algorithm works as its name. This algorithm picks a point inside
an object and starts to fill until it hits the boundary of the object. The color of the
boundary and the color that we fill should be different for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire
object. The boundary fill algorithm can be implemented by 4-connected pixels or
8-connected pixels.
Algorithm:
1. Create a function named as boundaryfill with 4 parameters
(x,y,f_color,b_color).
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
{
int gd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Boundary Fill algorithm ***");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y); x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}
Result:
1. Boundary Fill is used for the coloring figures in computer graphics. Here,
area gets colored with pixels of a chosen color as boundary this giving the
technique its name.
2. Boundary fill fills the chosen area with a color until the given colored
boundary is found.
3. This algorithm is also recursive in nature as the function returns when the
pixel to be colored is the boundary color or is already the fill color.
Experiment-7
Aim:
To implement Cohen-Sutherland Line Clipping Algorithm
Description:
This algorithm uses the clipping window as shown in the following figure. The
minimum coordinate for the clipping region is (XWmin, YWmin) and the maximum
coordinate for the clipping region is (XWmax, YWmax).
We will use 4-bits to divide the entire region. These 4 bits represent the Top,
Bottom, Right, and Left of the region as shown in the following figure. Here,
the TOP and LEFT bit is set to 1 because it is the TOP-LEFT corner.
There are 3 possibilities for the line −
● Line can be completely inside the window (This line should be accepted).
● Line can be completely outside of the window (This line will be completely
removed from the region).
● Line can be partially inside the window (We will find intersection point and
draw only that portion of line that is inside region).
Algorithm:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int visibility(PT p1,PT p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
Result:
Before clipping:
After clipping:
Experiment-8
Aim:
Write a program to implement clipping of Polygon Algorithm.
Description:
Algorithm:
1. for each edge test the following cases:
i. if v1 in outside and v2 inside:
a. find intersection
b. store inner v and intersection
ii. if v1 and v2 are inside:
a. save both
iii. if v1 is inside and v2 outside:
a. find intersection
b. store only the intersection
iv. if v1 and v2 are outside
a. ignore both
Code:
#include<iostream>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
using namespace std;
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 >= xmin && x2 >= xmin)
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 < ymin && y2 >= ymin)
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
int main()
int gdriver=DETECT,gmode,n,poly[20];
float xi,yi,xf,yf,polyy[20];
// clrscr();
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cin>>n;
int i;
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
Output:
Before:
After:
Aim:
Write a program to implement Translation, Rotation and Scaling of an object.
Description:
A translation process moves every point a constant distance in a specified
direction. It can be described as a rigid motion. A translation can also be
interpreted as the addition of a constant vector to every point, or as shifting the
origin of the coordinate system.
Suppose, If point P(X, Y) is to be translated by amount Dx and Dy to a new location
P’(X’, Y’) then new coordinates can be obtained by adding Dx to X and Dy to Y as:
X’ = Dx + X
Y’ = Dy + Y
Or
P’ = T + P
where P’ = [X’,Y’] ; T = [Dx,Dy]; P = [X,Y];
Algorithm:
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object
4. Translation
a. Get the translation value tx, ty
b. Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
c. Plot (x’,y’)
Code:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
Output:
Description:
In rotation, we rotate the object at particular angle θ (theta) from its origin. From
the following figure, we can see that the point P(X, Y) is located at angle φ from the
horizontal X coordinate with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new
location, you will get a new point P’ (X’, Y’).
Substituting equation (1) & (2) in (3) & (4) respectively, we will get
x′=xcosθ−ysinθ
y′=xsinθ+ycosθ
Representing the above equation in matrix form we have the Roation matrix R as,
Algorithm:
1. Start
4. Rotation
void ddaline(int x1, int y1, int x2, int y2,int color)
int dx = x2-x1;
int dy = y2-y1;
int steps,k=1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else steps=abs(dy);
xsteps= dx/(float)steps;
ysteps= dy/(float)steps;
putpixel(ROUND(x),ROUND(y),color);
while(k<=steps)
{
x+=xsteps;
y+=ysteps;
putpixel(ROUND(x), ROUND(y),color);
k++;
}
}
void rotate(int x1, int y1, int x2, int y2, float theta)
int main()
{
int x1, x2, y1, y2;
float theta;
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
getch();
closegraph();
return 0;
}
OUTPUT:
SCALING
Description:
Such a scaling changes the diameter of an object by a factor between the scale
factors, the area by a factor between the smallest and the largest product of two
scale factors, and the volume by the product of all three.
The scaling is uniform if and only if the scaling factors are equal (vx = vy = vz). If all
except one of the scale factors are equal to 1, we have directional scaling.
In the case where vx = vy = vz = k, the scaling is also called an enlargement or
dilation by a factor k, increasing the area by a factor of k2 and the volume by a
factor of k3.
A scaling in the most general sense is any affine transformation with a
diagonalizable matrix. It includes the case that the three directions of scaling are
not perpendicular. It includes also the case that one or more scale factors are equal
to zero (projection), and the case of one or more negative scale factors. The latter
corresponds to a combination of scaling proper and a kind of reflection. Along lines
in a particular direction we take the reflection in the point of intersection with a
plane that need not be perpendicular; therefore it is more general than ordinary
reflection in the plane.
If we provide values less than 1 to the scaling factor S, then we can reduce the
size of the object. If we provide values greater than 1, then we can increase the
size of the object.
Algorithm:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#define ROUND(a) ((int)(a+0.5))
void thickline(int X0, int Y0, int X1, int Y1) {
// calculate dx & dy
int dx = X1 - X0;
int dy = Y1 - Y0;
if(errorcode != grOk) {
printf("Graphics Error: %s\n",grapherrormsg(errorcode));
printf("Press key to halt");
getch();
exit(1);
}
thickline(x0,y0,x0,y1);
thickline(x0,y0,x1,y0);
thickline(x0,y1,x1,y1);
thickline(x1,y0,x1,y1);
delay(100);
closegraph();
clrscr();
scaling(&x0,&y0,2,2);
scaling(&x0,&y1,2,2);
scaling(&x1,&y0,2,2);
scaling(&x1,&y1,2,2);
gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "..\\");
errorcode = graphresult();
if(errorcode != grOk) {
printf("Graphics Error: %s\n",grapherrormsg(errorcode));
printf("Press key to halt");
getch();
exit(1);
}
thickline(x0,y0,x0,y1);
thickline(x0,y0,x1,y0);
thickline(x0,y1,x1,y1);
thickline(x1,y0,x1,y1);
getch();
closegraph();
return 0;
}
Output:
Before Scaling:
After Scaling:
Aim:
Write a program to implement Liang Barsky Line Clipping algorithm.
Description:
● The Liang–Barsky algorithm uses the parametric equation of a line and inequalities describing the
range of the clipping window to determine the intersections between the line and the clip window.
With these intersections it knows which portion of the line should be drawn.
● This algorithm is significantly more efficient than Cohen–Sutherland.
● The idea of the Liang–Barsky clipping algorithm is to do as much testing as possible before
computing line intersections.
Algorithm:
1. Set tmin=0 and tmax=0
2. Calculate the values of tL, tR, tT, and tB (tvalues).
2.1 if t < tmin or t > tmax ignore it and go to the next edge
2.2 otherwise classify the tvalue as entering or exiting value (using inner product to
classify)
2.3 if t is entering value set tmin = t ;
if t is exiting value set tmax = t
3. If tmin < tmax then draw a line from (x1 + dxtmin, y1 + dytmin) to (x1 + dxtmax, y1 +
dytmax)
4. If the line crosses over the window, you will see (x1 + dxtmin, y1 + dytmin) and (x1 +
dxtmax, y1 + dytmax) are intersection between line and edge.
CODE:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int x1,y1,x2,y2,xmax,xmin,ymax,ymin,xx1,yy1,xx2,yy2,dx,dy,i;
int p[4],q[4];
float t1,t2,t[4];
cout<<"Enter the lower co-ordinates of window";
cin>>xmin>>ymin;
cout<<"Enter the upper co-ordinates of window";
cin>>xmax>>ymax;
setcolor(RED);
rectangle(xmin,ymin,xmax,ymax);
cout<<"Enter x1:";
cin>>x1;
cout<<"Enter y1:";
cin>>y1;
cout<<"Enter x2:";
cin>>x2;
cout<<"Enter y2:";
cin>>y2;
line(x1,y1,x2,y2);
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;
Result:
Before Clipping:
After clipping: