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

Computer Graphics Program

This document contains 10 programs related to computer graphics algorithms: 1. The first two programs implement DDA and Bresenham's line drawing algorithms. 2. The third program implements Bresenham's circle drawing algorithm. 3. The fourth program implements Cohen-Sutherland line clipping algorithm. 4. The fifth and sixth programs implement boundary fill and flood fill algorithms. 5. The remaining programs implement algorithms for drawing ellipses, lines, circles using midpoint techniques, rotating and translating rectangles, and drawing polygons. All the programs utilize basic graphics functions and take input to draw various shapes on the screen.

Uploaded by

Madhukar Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

Computer Graphics Program

This document contains 10 programs related to computer graphics algorithms: 1. The first two programs implement DDA and Bresenham's line drawing algorithms. 2. The third program implements Bresenham's circle drawing algorithm. 3. The fourth program implements Cohen-Sutherland line clipping algorithm. 4. The fifth and sixth programs implement boundary fill and flood fill algorithms. 5. The remaining programs implement algorithms for drawing ellipses, lines, circles using midpoint techniques, rotating and translating rectangles, and drawing polygons. All the programs utilize basic graphics functions and take input to draw various shapes on the screen.

Uploaded by

Madhukar Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 28

PROGRAM NO # 1

/*DDA line algorithm*/

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT ,gm,x1,x2,y1,y2i,steps;
float xinc,yinc,x,y,;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
cout<<"Enter the coordinates of line (x1,y1,x2,y2)"<<endl;
cin>>x1>>y1>>x2>>y2;
steps=abs(x2-x1);
if(abs(y2-y1)>steps)
steps=abs(x2-x1);
xinc=(x2-x1)/steps;
yinc=(y2-y1)/steps;
x=x1+0.5;
y=y1+0.5;
for(i=1;i<=steps;i++)
{
putpixel(x,y,10);
x=x+xinc;
y=y+yinc;
delay(10);
}
getch();
}
PROGRAM NO # 2
/*Bresenham line algorithm*/

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT ,gm,x1,x2,y1,y2;
int dx.dy,x,y,p,end;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
cout<<"Enter the coordinates of line (x1,y1,x2,y2)"<<endl;
cin>>x1>>y1>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
p=2*(dy-dx);
if(x1>x2){
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
while(x<xend)
{
putpixel(x,y,10);
x++;
if(p<0)
p=p+2*dy;
else
{
y++;
p=p+2*(dy-dx);
}
delay(10);
}
getch();
}
PROGRAM NO # 3
/*Bresenham's Circle algorithm*/

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT ,gm,x1,y1,r;
int x,y,p;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
cout<<"Enter the coordinates of Circle & radius"<<endl;
cin>>x1>>y1>>r;
x=0;
y=r;
p=3-2*r;
while(x<=y)
{
putpixel(x1+x,y1+y,10);
putpixel(x1-x,y1+y,10);
putpixel(x1+x,y1-y,10);
putpixel(x1-x,y1-y,10);
putpixel(x1+y,y1+x,10);
putpixel(x1-y,y1+x,10);
putpixel(x1+y,y1-x,10);
putpixel(x1-y,y1-x,10);
if(p<0)
p+=4*x+++6;
else
P+=4*(x++-y--)+10;
delay(10);
}
getch();
}
PROGRAM NO # 4
/*COHEN SUTHERLAND LINE CLIPPING*/

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

enum boolean(FALSE,TRUE);
enum{TOP=0x8,BOTTOM=0x4,RIGHT=0X2,LEFT=0x1};

int computecode(double x,double y,double xmin,double xmax,double


ymin,double ymax)
{
int code=0;
if(y=ymax)
code | =TOP;
else if (y<ymin)
code | =BOTTOM;
if(x>xmax)
code | =RIGHT;
else if (x<xmin)
code | =LEFT;
return code;
}

void cohen_sutherland_line_clipping(double x0,double y0,double


x1,double y1,double xmin,double ymin,double xmax,double ymax)
{
int outcode0,outcode1,outcodeOut;
boolean accept=FALSE,done=FALSE;
outcode0=Compoutcode(x0,y0,xmin,xmax,ymin,ymax);
outcode1=Compoutcode(x1,y1,xmin,ymin,ymax);
do
{
if(!outcode|outcode1)
{
accept=TRUE;
done=TRUE;
}
else if(outcode0 & outcode1)done=TRUE;
else
{
double x,y;
outcodeOut=outcode0?outcode0:outcode1;
if(outcodeOut&TOP)
{
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else if(outcodeOut&BOTTOM)
{
x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else if(outcodeOut&RIGHT)
{
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
}
else
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
if(outcodeOut==outcode0)
{
x0=x;
y0=y;
outcode0=Compoutcode(x0,y0,xmin,xmax,ymin,ymax);
}
else
{
x1=x;
y1=y;
outcode0=Compoutcode(x1,y1,xmin,xmax,ymin,ymax);
}
}
}
while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
}

void main(){
int driver=VGA,mode=VGAHI;
int x1,y1,x2,y2;
int xmin,ymin,xmax,ymax;
initgraph(&driver,&mode,"e:tc\\bgi");
cout<<"enter the line coordinates(x1,y1,x2,y2):";
cin>>xmin>>ymin>>xmax>>ymax;
rectangle(xmin,ymin,xmax,ymax);
getch();
cleardevice();
cohen_sutherland_line_clipping(x1,y1,x2,xmin,ymin,xmax,ymax);
rectangle(xmin,ymin,xmax,ymax);
getch();
}
PROGRAM NO # 5
/* BOUNDARY FILL */

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void boundaryfill(int x,int y,int fill,int boundary)
{
int current;
current = getpixel(x,y);
if((current!= boundary)&&(current!=fill))
{
setcolor(fill);
setpixel(x,y);
boundaryfill(x+1,y,fill,boundary);
boundaryfill(x-1,y,fill,boundary);
boundaryfill(x,y+1,fill,boundary);
boundaryfill(x,y-1,fill,boundary);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,":\\tc\\bgi");
boundaryfill(100,200,4,6);
getch();
}
PROGRAM NO # 6
/* FLOOD FILL */

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void floodfill(int x,int y,int fillcolor,int oddcolor)
{
if(getpixel(x,y)==oddcolor)
{
setcolor(fillcolor);
setpixel(x,y);
floodfill(x+1,y,fillcolor,oddcolor);
floodfill(x-1,y,fillcolor,oddcolor);
floodfill(x,y+1,fillcolor,oddcolor);
floodfill(x,y-1,fillcolor,oddcolor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,":\\tc\\bgi");
floodfill(100,200,4,6);
getch();
}
PROGRAM NO # 7
/*Drawing an Ellipse using Mid Point algorithm*/

#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<alloc.h>
void ellipsemidpoint(int xc,int yc,int rx,int ry);
void lotpoint(int x,int xc,int yc,int y);

int maxx,maxy,xc,yc,rx,ry;

int main(void)

{
int gd=DETECT ,gm,errorcode;
initgraph(&gd,&gm,"e:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphics error :%s\n",grapherrormsg(errorcode));
printf("press any key to hault");
getch();
exit(1);
}
maxx=getmaxx();
maxy=getmaxy(0;
xcenter=250;
ycenter=250;
rx=50;
ry=50;
ellipsemidpoint(xc,yc,rx,ry);
getch();
closegraph();
return(0);
}

void ellipsemidpoint(int xc,int yc,int rx,int ry);


{
int p,px,py,ry2,rx2,trx2,try2;
ry2=ry*ry;
rx2=rx*rx;
try2=2*ry2;
trx2=2*rx2;
x=0;
y=ry;
plotpoint(x,xc,yc,y);
p=abs(ry2-ry2*ry+(0.25*rx2));
px=0;
py=trx2*y;
while(px<py)
{
x=x+1;
px=px+try2;
if(p<0)
{
p=p+ry2+px;
}
else
{
p=p+ry2=px;
}
plotpoint(x,xc,y,yc,y);
}
p=abs(ry2*(x+6.5)*(x+6.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y=y-1;
py=py-trx2;
if(p<0)
{
x=x+1;
px=px+try2;
}
if(p<0)
{
p=p=ry2-py;
}
else
{
p=p+rx2-py+px;
plotpoint(x,xc,yc,y);
}
}
}
void lotpoint(int x,int xc,int yc,int y);
{
putpixel(xc+x,yC+y,2);
putpixel(xc-x,yC+y,2);
putpixel(xc+x,yC-y,2);
putpixel(xc-x,yC-y,2);
}
PROGRAM NO # 8
/*Mid Point line algorithm*/

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT ,gm,x1,x2,y1,y2;
int a,b,x,y,p,xend;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
cout<<"Enter the coordinates of line (x1,y1,x2,y2)"<<endl;
cin>>x1>>y1>>x2>>y2;
b=-(x2-x1);
a=y2-y1;
p=2*a+b;
if(x1>x2)
{
x=x2;P
y=y2;
xend=x1;
}
else{
x=x1;
y=y1;
xend=x2;
}
while(x<xend)
{
putpixel(x,y,10);
x++;
if(p<0)
p=p+2*a;
else
{
y++;
p=p+2*(a+b);
}
delay(10);
}
getch();
}
PROGRAM NO # 9
/*Mid point Circle algorithm*/

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT ,gm,x1,y1,r;
int x,y,p;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
cout<<"Enter the coordinates of Circle & radius"<<endl;
cin>>x1>>y1>>r;
x=0;
y=r;
p=1-r;
while(x<=y)
{
putpixel(x1+x,y1+y,10);
putpixel(x1-x,y1+y,10);
putpixel(x1+x,y1-y,10);
putpixel(x1-x,y1-y,10);
putpixel(x1+y,y1+x,10);
putpixel(x1-y,y1+x,10);
putpixel(x1+y,y1-x,10);
putpixel(x1-y,y1-x,10);
if(p<0)
p+=2*x+++1;
else
P+=2*(x++-y--)+1;
delay(10);
}
getch();
}
PROGRAM NO # 10
/*ROTATION OF A RECTANGLE*/

#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
float x1,y1,x2,y2,a;
int main(void)

{
int gd=DETECT ,gm,errorcode;
initgraph(&gd,&gm,"e:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphics error :%s\n",grapherrormsg(errorcode));
printf("press any key to hault");
getch();
exit(1);
}

do
{
getch();
clrscr();
printf("enter the top left coordinate of rectangle");
scanf("%f %f",&x1,&y1);
printf("enter the bottom right coordinate of rectangle");
scanf("%f %f",&x2,&y2);
printf("enter the value of angle of rotation");
scanf("%f",&a);
cleardevice();
rectangle(x1,y1,x2,y2);
a=a(3.14/180);
x1=(x1*cos(a))-(y1*sin(a));
y1=(x1*sin(a))+(y1*cos(a));
x2=(x2*cos(a))-(y2*sin(a));
y2=(x2*sin(a))+(y2*cos(a));
cout<<"now press akey to see rotation";
getch();
rectangle(x1,y1,x2,y2);
getch();
closegraph();
}
PROGRAM NO # 11
/*TRANSLATION OF A RECTANGLE*/

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

float x1,y1,x2,y2,x,y;
void main(void)
{
int gd=DETECT ,gm,errorcode;
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphics error :%s\n",grapherrormsg(errorcode));
printf("press any key to hault");
getch();
exit(1);
}
do
{
getch();
cleardevice();
cout<<"enter the top left coordinate of rectangle";
cin>>x1>>y1;
cout>>"enter the bottom right coordinate of rectangle";
cin>>x2>>y2;
cout<<"enter the value of translation coordinates:\n";
cin>>x>>y;
cleardevice();
rectangle(x1,y1,x2,y2);
cout<<"now press a key to see translation";
getch();
rectangle(x1+x,y1+y,x2+x,y2+y);
}
getch();
closegraph();
}
PROGRAM NO # 12
/*PROGRAM FOR DRAWING POLYGON*/

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main(0
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c;\tc\bgi”);
int n,ax[100],ay[100],t1,t2,dfx,dfy;
printf(“enter the number of lines in polygon’);
sacnf(“%d”,&n);
if(n<3)
printf(“error! Cannot be drawn”);
else
for(int i=0;i<n;i++)
{
printf(“enter the coordinates”,i+1);
scanf(“%d %d”,&ax[i],&ay[i]);
}
t1=dfx=ax[0];
t2=dfy=ay[0];
setcolor(5);
moveto(dfx,dfy);
for(i=1;i<n;i++)
{
lineto(ax[i],ay[i]);
}
lineto(t1,t2);
getch();
}
PROGRAM NO # 13
/* PROGRAM ON SCANLINE */

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<alloc.h>
#include<graphics.h>
#include<dos.h>
void Initgraph()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */


errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */


{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
void draw_poly(int *x,int *y,int points,int color)
{ int i,j;
setcolor(color);
for(i=0;i<points-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
}
int max(int *a,int points)
{ int i,max =a[0];
for(i=1;i<points;i++)
if(a[i]>max)
max=a[i];
return max;
}
int min(int *a,int points)
{
int i,min =a[0];
for(i=1;i<points;i++)
if(a[i]<min)
min=a[i];
return min;
}
void fill_poly(int *x,int *y,int points,int boundarycolor,int fillcolor)
{ int minx=min(x,points)-1;
int miny=min(y,points)-1;
int maxx=max(x,points)+1;
int maxy=max(y,points)+1;
int i,j;
int even_odd=0;
if(boundarycolor!=fillcolor)
{
for(i=miny;i<=maxy;i++)
{
for(j=minx;j<=maxx;j++)
if(getpixel(j,i)==boundarycolor)
{
for(++j;j<=maxx;j++)
{
if(getpixel(j,i)==boundarycolor)
{
break;
}
putpixel(j,i,fillcolor);
delay(1);
}
}
}
}
else
printf("Error:Filling Not Possible:");
}
void main()
{
int *x,*y,points;
int i;
clrscr();
printf("\nEnter The No. of points in polygun:");
scanf("%d",&points);
x=(int*)malloc(points*sizeof(int));
y=(int*)malloc(points*sizeof(int));
for(i=0;i<points;i++)
{
printf("Enter The %d point coordinate i.e.,(x,y):",i+1);
scanf("%d%d",&x[i],&y[i]);
}
Initgraph();
draw_poly(x,y,points,RED);
fill_poly(x,y,points,RED,YELLOW);
getch();
closegraph();
}
PROGRAM NO # 14
/* PROGRAM ON WLINE */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void Initgraph()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */


errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */


{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
void DDA_line(int x1,int y1,int x2,int y2,int color)
{ int length,i;
float x,y,xinr,yinr;
length=abs(x2-x1);
if(abs(y2-y1)>length)
length=abs(y2-y1);
xinr=(x2-x1)/(length*1.0);
yinr=(y2-y1)/(length*1.0);
x=x1+.5;
y=y1+.5;
for(i=1;i<length;i++)
{
putpixel(x,y,color);
x+=xinr;
y+=yinr;
}
}
void drawline_w(int x1,int y1,int x2,int y2,int width)
{
int flag,x,y;
float wy,wx;
int comm_width=(width-1)/2;
if((x2-x1)==0)
flag=-1;//in y direction doesn't change
else if((y2-y1)==0)
flag=1;//in x direction doesn't change
else
flag=0;
//float length=sqrt(pow((x2-x1),2)+pow((y2-
y1),2));
if(flag==1)
{
for(y=y1,x=x1;x<=x2;x++)
{
for(int i=0;i<comm_width;i++)
{ putpixel(x,y+i,YELLOW);
putpixel(x,y-i,YELLOW);
}
getch();
}
}
else if(flag==-1)
{

for(x=x1,y=y1;y<=y2;y++)
{
for(int i=0;i<comm_width;i++)
{ putpixel(x+i,y,YELLOW);
putpixel(x-i,y,YELLOW);
}
getch();
}
}
else
{
if(x1<x2){
for(x=x1,y=y1;y<=y2;y++,x++)
{
for(int i=0;i<comm_width;i++)
{ putpixel(x+i,y-i,YELLOW);
putpixel(x-i,y+i,YELLOW);
}
getch();
}
}
else{
for(x=x1,y=y1;x>=x2||y<=y2;y++,x--)
{
for(int i=0;i<comm_width;i++)
{ putpixel(x+i,y+i,YELLOW);
putpixel(x-i,y-i,YELLOW);
}
getch();
}

}
}
void main()
{
int x1,y1,x2,y2,width;
printf("\nEnter The (x1,y1) & (x2,y2) coordinate of line:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("\nEnter The Width:");
scanf("%d",&width);
Initgraph();
drawline_w(x1,y1,x2,y2,width);
getch();
closegraph();
}
PROGRAM NO # 15
/* PROGRAM ON LINE CLPPING */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
enum cut_positions{TOP,BOTTOM,RIGHT,LEFT,BOTHY,BOTHX};
void Initgraph()
{
/* request auto detection */
int gdriver = DETECT,gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */


errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */


{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
void draw_window(int window[],int color)
{
setcolor(color);
line(window[LEFT],window[TOP],window[RIGHT],window[TOP]);
line(window[LEFT],window[TOP],window[LEFT],window[BOTTOM]);

line(window[LEFT],window[BOTTOM],window[RIGHT],window[BOTTOM
]);

line(window[RIGHT],window[TOP],window[RIGHT],window[BOTTOM]);
}
void draw_lines(int x1[],int y1[],int x2[],int y2[],int n,int color)
{
setcolor(color);
for(int i=0;i<n;i++)
line(x1[i],y1[i],x2[i],y2[i]);
}
void line_clip(int *x1,int *x2,int *y1,int *y2,int cut_position,int
window[])
{
double m=(*y2-*y1)*(1.0)/(*x2-*x1);
switch(cut_position)
{
case LEFT:
*y1=m*(window[LEFT]-*x1)+*y1;
*x1=window[LEFT];
break;
case RIGHT:
*y2=m*(window[RIGHT]-*x1)+*y1;
*x2=window[RIGHT];
break;
case BOTHX:
*y1=m*(window[LEFT]-*x1)+*y1;
*y2=m*(window[RIGHT]-*x1)+*y1;
*x1=window[LEFT];
*x2=window[RIGHT];
break;
case TOP:
*x1=*x1+(1/m)*(window[TOP]-*y1);
*y1=window[TOP];
break;
case BOTTOM:
*x2=*x1+(1/m)*(window[BOTTOM]-*y1);
*y2=window[BOTTOM];
break;
case BOTHY:
*x1=*x1+(1/m)*(window[TOP]-*y1);
*y1=window[TOP];
*x2=*x1+(1/m)*(window[BOTTOM]-*y1);
*y2=window[BOTTOM];
break;
// default : printf("\nError:Line Not Clipped");
}
}
int xboundary_checking(int x1,int x2,int *window)
{
int cut_position=-1;
/*LEFT and RIGHT checking*/
if((x1<=window[LEFT])&&(x2<=window[LEFT]))
cut_position=-1;
else if((x1>=window[RIGHT])&&(x2>=window[RIGHT]))
cut_position=-1;
else
if(((x1>=window[LEFT])&&(x2>=window[LEFT])&&(x1<=window[RIG
HT])&&(x2<=window[RIGHT])))
cut_position=-1;
else if((x1<=window[LEFT])&&(x2>=window[RIGHT]))
cut_position=BOTHX;
else if(x1>=window[LEFT])
cut_position=RIGHT;
else if(x1<=window[LEFT])
cut_position=LEFT;
else cut_position=-1;
return cut_position;
}
int yboundary_checking(int y1,int y2,int *window)
{
int cut_position=-1;
/*TOP and BOTTOM checking*/
if((y1<window[TOP])&&(y2<window[TOP]))
cut_position=-1;
else if((y1>=window[BOTTOM])&&(y2>=window[BOTTOM]))
cut_position=-1;
else
if(((y1>=window[TOP])&&(y2>=window[TOP])&&(y1<=window[BOTT
OM])&&(y2<=window[BOTTOM])))
cut_position=-1;
else if((y1<=window[TOP])&&(y2>=window[BOTTOM]))
cut_position=BOTHY;
else if(y1>=window[TOP])
cut_position=BOTTOM;
else if(y1<=window[BOTTOM])
cut_position=TOP;
else cut_position=-1;
return cut_position;
}
void main()
{ int *x1,*y1,*x2,*y2;
int n;
int window[4];
int i;
clrscr();
printf("\nEnter The WindowS->\n");
printf("TOP:");
scanf("%d",&window[TOP]);
printf("BOTTOM:");
scanf("%d",&window[BOTTOM]);
printf("RIGHT:");
scanf("%d",&window[RIGHT]);
printf("LEFT:");
scanf("%d",&window[LEFT]);
printf("\n\n\nEnter No. Of Lines:");
scanf("%d",&n);
x1=(int*)malloc(n*sizeof(int));
x2=(int*)malloc(n*sizeof(int));
y1=(int*)malloc(n*sizeof(int));
y2=(int*)malloc(n*sizeof(int));
printf("\nEnter The Line Coordinates:\n\n");
for(i=0;i<n;i++)
{
printf("\nEnter The Line %d points:\n",i+1);
printf("(x1,y1)=");
scanf("%d%d",&x1[i],&y1[i]);
printf("(x2,y2)=");
scanf("%d%d",&x2[i],&y2[i]);
}
Initgraph();
draw_window(window,WHITE);
draw_lines(x1,y1,x2,y2,n,YELLOW);
getch();
cleardevice();
for(i=0;i<n;i++)
{ line_clip(&x1[i],&x2[i],&y1[i],&y2[i],
xboundary_checking(x1[i],x2[i],window),window);
line_clip(&x1[i],&x2[i],&y1[i],&y2[i],
yboundary_checking(y1[i],y2[i],window),window);
}
draw_window(window,WHITE);
draw_lines(x1,y1,x2,y2,n,YELLOW);
getch();
closegraph();
}
COMPUTER GRAPHICS LAB
MCA - 473
INDEX
S.NO PROGRAMS SIGN
1 /*DDA line algorithm*/

2 /*Bresenham line algorithm*/

3 /*Bresenham's Circle algorithm*/

4 /*COHEN SUTHERLAND LINE CLIPPING*/

5 /* BOUNDARY FILL */

6 /* FLOOD FILL */

7 /*Drawing an Ellipse using Mid Point Algorithm*/

8 /*Mid Point line algorithm*/

9 /*Mid point Circle algorithm*/

10 /*ROTATION OF A RECTANGLE*/

11 /*TRANSLATION OF A RECTANGLE*/

12 /*PROGRAM FOR DRAWING POLYGON*/

13 /* PROGRAM ON SCANLINE */

14 /* PROGRAM ON WLINE */

15 /* PROGRAM ON LINE CLPPING */

You might also like