0% found this document useful (0 votes)
26 views

Computer Graphics Lab Manual

Updated LAb manual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Computer Graphics Lab Manual

Updated LAb manual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

BRESENHAM’S LINE DRAWING


AIM:
To implement Bresenham’s line drawing Algorithm for drawing lines.

FUNCTIONS USED:
Line ()

The function line () is used to draw a line from(x1, y1) to (x2, y2)
Syntax:

line (x1,y1,x2,y2)
initgraph().

This function takes thee arguments and they are


i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax
Initgraph(gd,gm,path)

ALGORITHM:

Step 1: Start
Step 2: Get the values of the end points as(x1, y1) &(x2, y2)
Step 3: Assign x=x1, y=y1;
Step 4: Compute dx=x2-x1
Step 5: Compute dy=y2-y1
Step 6: Assign sx=x2-x1, sy=y2-y1
Step 7: If dy>dx then interchange the values of dx and dy and assign exch=1
Step 8: Compute p=2xdy-dx
Step 9: Put a pixel on(x,y)
Step 10: If exch=1, y=sy else x=x+sx
Step 11: If p>0 and exch =1, x=x+sx else y=y+sy, p=p-2xdx
Step 12: Compute p=p+2xdy
Step 13: Do steps (9) t0 (12) for dx times
Step 14: Stop

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int signs(int m);
void bres_line(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph(&gd,&gm,"");
cout<<"enter starting value of X-Axis----->";
cin>>x1;
cout<<"enter starting value of Y-Axis----->";
cin>>y1;
cout<<"enter ending value of X-Axis----->";
cin>>x2;
cout<<"enter ending value of y-Axis----->";
cin>>y2;
bres_line(x1,y1,x2,y2);
getch();
closegraph();
}
void bres_line(int x1,int y1,int x2,int y2)
{
int x,y,sx,sy,p,temp,exch=0,i,dx,dy;
x=x1;
y=y1;
dy=abs(y1-y2);
dx=abs(x1-x2);
sx=signs(x2-x1);
sy=signs(y2-y1);
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
exch=1;
}
p=2*dy-dx;
for(i=0;i<=dx;i++)
{putpixel(x,y,15);

if(exch==1)
y+=sy;
else
x+=sx;
if(p>=0)
{
if(exch==1)
x+=sx;
else
y+=sy;p=p-2*dx;}
p=p+2*dy;
}
}
int signs(int m)
{
if(m<0)
return(-1);
else if(m>0)
return(1);
else
return(0);
}

INPUT:

Enter starting value of X-Axis----->100


Enter starting value of Y-Axis----->100
Enter ending value of X-Axis----->200
Enter ending value of Y-Axis----->200

OUTPUT:

RESULT:
Thus the program is executed and verified.
COHEN-SUTHERLAND CLIPPING

AIM:
To implement Cohen-Sutherland clipping Algorithm.

FUNCTIONS USED:

Line()
The function line () is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Setcolor().
This function changes the drawing colour.

Syntax:
Setcolor(value of the color)

Settextstyle().
The function settextstyle() is used to change the style of the text.

Syntax:
Settextstyle(font,direction,charsize)

Where font is the constant value or the font filename, direction is the number either 0 or
1, which makes the output to display in horizontal, or in vertical direction, charsize is the
character size or magnification factor and it varies from 1 to 10.

Outtext().
This function display a text message on upper left of the screen

Syntax:
Outtext(“message”);
ALGORITHM:

Step 1: Create a class sulc with functions drawwindow, drawline, setcode, visibility and
reset endpoint.
Step 2: Using the function line set the parameters to draw window.
Step 3: Using the function defined in class sulc, setcode is used to save the line inside the
window and to the line outside the window.
Step 4: Using the function visibility
i).check the code to know the points inside or outside the window.
ii).if the code value is zero the point is inside the window.
Step 5: Using the function reset end point
i). if the code value for the line is outside the window.
ii).reset the endpoint to the boundary of the window.
Step 6: Initialize the graphics functions
Step 7: Declare the variables x1, x2, y1, y2 of array type.
Step 8: Get the value of two endpoints x1, y1 and x2, y2 to draw the line.
Step 9: Using the object c, display the window before clipping.
Step 10: Using the function setcode, visibility display the clipped window only with lines
inside the window class was displayed after clipping.

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
typedef struct coord
{
int x,y;
char code[4];
}pt;
class sulc
{
public:
void drawwindow();
void drawline(pt p1,pt p2,int c1);
pt setcode(pt p);
int visibility(pt p1,pt p2);
pt resetendpt(pt p1,pt p2);
};
void sulc::drawwindow()
{
setcolor(WHITE);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void sulc::drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
pt sulc::setcode(pt p)
{
pt ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.y>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.y<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int sulc::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);
}
pt sulc::resetendpt(pt p1,pt p2)
{
pt temp;
int x,y,i;
float m,k;
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(temp.y<=350 &&temp.y>=100)
return(temp);
}
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];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
else
return(p1);
}
void main()
{
int gd=DETECT,gm,v;
sulc c1;
pt p1,p2,ptemp;
initgraph(&gd,&gm,"");
int x1[10],y1[10],x2[10],y2[10];
cleardevice();
int i,n;
settextstyle(4,0,4);
outtext("cohen sutherland line clipping");
cout<<"\n\n enter the no.of lines:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\n enter end-point1(x1,y1):";
cin>>x1[i]>>y1[i];
cout<<"\n\n enter end-point2(x2,y2):";
cin>>x2[i]>>y2[i];
}
cleardevice();
settextstyle(0,0,3);
outtext("before clipping");
c1.drawwindow();
for(i=0;i<n;i++)
{
p1.x=x1[i];
p1.y=y1[i];
p2.x=x2[i];
p2.y=y2[i];
c1.drawline(p1,p2,15);
}
getch();
cleardevice();
settextstyle(0,0,3);
outtext("after clipping");
for(i=0;i<n;i++)
{
p2.x=x2[i];
p2.y=y2[i];
p1=c1.setcode(p1);
p2=c1.setcode(p2);
v=c1.visibility(p1,p2);
switch(v)
{
case 0:
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
case 1:
c1.drawwindow();
break;
case 2:
p1=c1.resetendpt(p1,p2);
p2=c1.resetendpt(p2,p1);
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
}
}
getch();
closegraph();
}

INPUT:

Enter the no.of lines: 1


Enter end-point1(x1,y1):30 40
Enter end-point1(x2,y2):300 400

OUTPUT:

Before clipping
After clipping

RESULT:
Thus the program is executed and verified.

You might also like