Computer Graphics Program
Computer Graphics Program
Computer Graphics Program
#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};
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);
}
#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;
#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;
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;
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*/
5 /* BOUNDARY FILL */
6 /* FLOOD FILL */
10 /*ROTATION OF A RECTANGLE*/
11 /*TRANSLATION OF A RECTANGLE*/
13 /* PROGRAM ON SCANLINE */
14 /* PROGRAM ON WLINE */