Sie sind auf Seite 1von 28

INDEX

Sr. No

Topic

Write a program to implement DDA Algorithm for line.

Write a program to implement Bresenhams line Drawing Algorithm

Write a program to implement Bresenhams Circle Drawing Algorithm

Write a program to implement Midpoint Circle Algorithm

Write a program to implement Bezier Curve Algorithm

Write a program to implement Cohen Sutherland line Clipping Algorithm

Write a program to implement Mid-point Line Clipping Algorithm

Write a program to implement Flood Fill Algorithm

Write a program to implement Boundary Fill Algorithm

10

Write a program to implement Liang Barsky line clipping Algorithm.

Practical 1:
Write a program to implement DDA Algorithm for line.
Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,x2,y1,y2,dx,dy,length;
int i,gd,gm;
clrscr();
/* Read two end points of line
--------------------------------------------*/
printf("Enter the value of X1 : \t");
scanf("%f",&x1);
printf("Enter the value of Y1 : \t");
scanf("%f",&y1);
printf("Enter the value of X2 : \t");
scanf("%f",&x2);
printf("Enter the value of y2 : \t");
scanf("%f",&y2);
/* Initialize graphics mode
--------------------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
/*Factor 0.5 is added to round the values*/
y=y1+0.5;
/*Factor 0.5 is added to round the values*/
i=1;
/*Initialize loop counter*/

while(i<=length)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
delay(100); /*Delay is purposely inserted to see observe the line drawing process
*/
}
getch();
closegraph();
}
Output

Practical 2:
Write a program to implement Bresenhams line Drawing Algorithm
Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,x2,y1,y2,dx,dy,e;
int i,gd,gm;
clrscr();
/* Read two end points of line
--------------------------------------------*/
printf("Enter the value of X1 : \t");
scanf("%f",&x1);
printf("Enter the value of Y1 : \t");
scanf("%f",&y1);
printf("Enter the value of X2 : \t");
scanf("%f",&x2);
printf("Enter the value of y2 : \t");
scanf("%f",&y1);
/* Initialize graphics mode
--------------------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
/* Initialize starting point
--------------------------------------------*/
x=x1;
y=y1;
/* Initialize initialize decision variable
--------------------------------------------*/
e=2*dy-dx;
i=1;
/*Initialize loop counter*/
do
{
putpixel(x,y,15);
while(e>=0)
{

y=y+1;
e=e-2*dx;
}
x=x+1;
e=e-2*dx;
i=i+1;
}
while(i<=dx);
getch();
closegraph();
}
Output:

Practical 3:
Write a program to implement Bresenhams Circle Drawing Algorithm
Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float d;
int gd,gm,x,y;
int r;
clrscr();
/* Read the radius of the circle
---------------------------------- */
printf("Enter the radius of a circle :");
scanf("%d",&r);
/* Initialise graphics mode
------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
/* Initialise starting points
-------------------------------*/
x = 0;
y = r;
/* initialise the decision variable
---------------------------------------*/
d = 3 - 2 * r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+y,200-x,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-y,200+x,15);
putpixel(200-x,200+y,15);
if (d <= 0)
{
d = d + 4*x + 6;

}
else
{
d = d + 4*(x-y) + 10;
y = y - 1;
}
x = x + 1;
delay(1000); /* Delay is purposely inserted to see
observe the line drawing process */
}
while(x < y);
getch();
closegraph();
}
Output:

Practical 4
Write a program to implement Midpoint Circle Algorithm
Code:
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void driver()
{
int gd=DETECT,gmode;
initgraph(&gd,&gmode,"d:\\tc\\bgi");
}
void circlept(int xcentre,int ycentre,int x,int y,int radius)
{
putpixel(xcentre+x,ycentre+y,RED);
putpixel(xcentre-x,ycentre+y,RED);
putpixel(xcentre+x,ycentre-y,RED);
putpixel(xcentre-x,ycentre-y,RED);
putpixel(xcentre+y,ycentre+x,RED);
putpixel(xcentre-y,ycentre+x,RED);
putpixel(xcentre+y,ycentre-x,RED);
putpixel(xcentre-y,ycentre-x,RED);
}
void midptcircle(int xcentre,int ycentre,int radius)
{
int p,x,y;
x=0;
y=radius;
p=1-radius;
circlept(xcentre,ycentre,x,y,radius);
while(x<y)
{
if(p<0)
x++;
else
{
x++;
y--;
}
if(p<0)

p=p+ 2*x +1;


else
p=p+2*(x-y)+1;
circlept(xcentre,ycentre,x,y,radius);
}
}
void main()
{
int xcentre,ycentre,radius;
driver();
getch();
setbkcolor(DARKGRAY);
cleardevice();
printf("\n Enter x co-ordinate of centre::\t");
scanf("%d",&xcentre);
printf("\n Enter y co-ordinate of centre::\t");
scanf("%d",&ycentre);
printf("\n Enter Radius of the circle:: \t");
scanf("%d",&radius);
cleardevice();
midptcircle(xcentre,ycentre,radius);
getch();
closegraph();
}
Output:

Practical 5:
Write a program to implement Bezier Curve Algorithm
Code:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int gd = DETECT, gm, maxx, maxy;
float s[4][2];
void line1(float x2, float y2)
{
line(s[0][0],s[0][1],x2,y2);
s[0][0]=x2;
s[0][1]=y2;
}
void bezier(float xb,float yb, float xc, float yc,float xd, float yd, int n)
{
float xab,yab,xbc, ybc, xcd, ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd, yabcd;
if(n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab=(s[0][0]+xb)/2;
yab=(s[0][1]+yb)/2;
xbc=(xb+xc)/2;
ybc=(yb+yc)/2;
xcd=(xc+xd)/2;
ycd=(yc+yd)/2;
xabc=(xab+xbc)/2;
yabc=(yab+ybc)/2;
xbcd=(xbc+xcd)/2;
ybcd=(ybc+ycd)/2;
xabcd=(xabc+xbcd)/2;
yabcd=(yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);

bezier(xbcd,ybcd, xcd,ycd,xd,yd,n);
}
}
void main()
{
int i,gd,gm;
float t1,t2;
clrscr();
gd=DETECT;
initgraph(&gd,&gm,"D:\\TC\\BGI");
for(i=0;i<4;i++) {
printf("Enter x and Y :");
scanf("%f%f",&t1,&t2);
s[i][0]=t1;
s[i][1]=t2; }
bezier(s[1][0],s[1][1],s[2][0],s[2][1],s[3][0],s[3][1],8);
getch();
}
Output:

Practical 6
Write a program to implement Cohen Sutherland line Clipping Algorithm
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
/* Defining structure for end point of line */
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2,int cl);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
{
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,"d:\\tc\\bgi");
cleardevice();
printf("\n\n\t\tENTER END-POINT 1 (x,y): ");
scanf("%d,%d",&p1.x,&p1.y);
printf("\n\n\t\tENTER END-POINT 2 (x,y): ");
scanf("%d,%d",&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:
cleardevice(); /* Line conpletely visible */

case 1:

case 2:

drawwindow();
drawline(p1,p2,15);
break;
cleardevice(); /* Line completely invisible */
drawwindow();
break;
cleardevice(); /* line partly visible */
p1=resetendpt (p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;

}
getch();
closegraph();
return(0);
}
/* Function to draw window */
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
/* Function to draw line between two points
---------------------------------------------*/
void drawline (PT p1,PT p2,int cl)
{
setcolor(cl);
line(p1.x,p1.y,p2.x,p2.y);
}
/* Function to set code of the coordinates
--------------------------------------------*/
PT setcode(PT p)
{
PT ptemp;
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) /* LEFT */
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
/* Function to determine visibility of line
--------------------------------------------*/
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);
}
/* Function to find new end points
--------------------------------------*/
PT resetendpt (PT p1,PT p2)
{
PT temp;
int x,y,i;
float m,k;
if( p1.code[3]=='1') /* Cutting LEFT Edge */

x=150;
if(p1.code[2]=='1') /* Cutting RIGHT Edge */
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') /* Cutting TOP Edge */
y=100;
if(p1.code [1]=='1') /* Cutting BOTTOM Edge */
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);
}

Output:

Practical 7
Write a program to implement Mid-point Line Clipping Algorithm
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
/* Defining structure for end point of line */
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
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);
main()
{
int gd=DETECT,gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,"d:\\tc\\bgi");
cleardevice();
printf("\n\n\n\t Enter END-POINT 1 (x,y): ");
scanf("%d,%d",&p1.x,&p1.y);
printf("\n\n\n\t Enter END-POINT 2 (x,y): ");
scanf("%d,%d",&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
cleardevice();
drawwindow();
midsub(p1,p2);
getch();
closegraph();
return(0);

}
midsub(PT p1,PT p2)
{
PT mid;
int v;
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0: /*line completely visible*/

case 1:

drawline(p1,p2,15);
break;
/*line completely invisible*/

break;
case 2:cleardevice();
/*line partially visible*/
mid.x=p1.x+(p2.x-p1.x)/2;
mid.y=p1.y+(p2.y-p1.y)/2;
midsub(p1,mid);
mid.x=mid.x+1;
mid.y=mid.y+1;
midsub(mid,p2);
break;
}
}
/* Function to draw window
-----------------------------------------*/
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
}
/*Function to draw line between two points
-------------------------------------------------------------------*/
void drawline(PT p1,PT p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}

/*Function to set code of the cordinates


-------------------------------------------------------------------*/
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; /* TOP */
else
ptemp.code[0]='0';
if(p.y>400)
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);
}
/*Function to determine visibility of line
-------------------------------------------------------------------*/
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);
}
Output:

Practical 8
Write a program to implement Flood Fill Algorithm
Code:
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd,gm;
/* Initialize graphics mode
--------------------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\bgi");
rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}
flood_fill(x,y,old_color,new_color)
{
if(getpixel(x,y)= old_color)
{
putpixel(x,y,new_color);
flood(x+1,y,old_color,new_color);
flood(x-1,y,old_color,new_color);
flood(x,y+1,old_color,new_color);
flood(x,y-1,old_color,new_color);
flood(x+1,y+1,old_color,new_color);
flood(x-1,y-1,old_color,new_color);
flood(x+1,y-1,old_color,new_color);
flood(x-1,y+1,old_color,new_color);
}
}

Output

Practical 9
Write a program to implement Boundary Fill Algorithm
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , 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(x,y)
int x , 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);
}
void main()
{
int x,y,n,i;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"d:\\tc\\bgi");

/*- 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();
}
Output:

Practical 10
Write a program to implement Liang Barsky line clipping Algorithm.
Code:
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");

getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );

else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordinate are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);
printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"D:\\TC\\BGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}
}

Output:

Das könnte Ihnen auch gefallen