practical file of computer graphics
DESCRIPTION
Practical File of Computer Graphics for B.Tech Computer Science Students.TRANSCRIPT
Practical File of Computer Graphics
SUBMITTED TO SUBMITTED BY
Mr. Sanjay Kataria Pankaj GillAsst. Professor 11/CSE/168CSE Deptt. CSE-B
111/CSE/168
Practical File of Computer Graphics
INDEXAim Page Remarks
1. Write a program to draw a stick man 22. Write a program to draw a rectangle using
line function4
3. Write a program to draw a line using DDA’s line drawing algorithm
6
4. Write a program to draw a line usingBresenham’s line drawing algorithm
9
5. Write a program to draw a circle using equation of circle
12
6 Write a program to draw a circle usingBresenham’s circle drawing algorithm
14
7. Write a program to draw a circle using midpoint circle drawing algorithm
17
8. Write a program to draw a circle using polar co- ordinates
20
9. Write a program to fill a circle using BoundaryFill Algorithm
23
10. Write a program to fill a circle using Flood FillAlgorithm
27
11. Write a program for line clipping using cohen- Sutherland algorithm
30
12. Write a program to translate a triangle about the origin
36
13. Write a program to scale a triangle about a fixed point taken as one of the vertex of the triangle
39
14. Write a program to rotate a triangle about a fixed point taken as one of the vertex of the triangle
42
PANKAJ GILL
211/CSE/168
Practical File of Computer Graphics
PRACTICAL NO.1Write a program to draw a stick man
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
int x,y,r,c1;
initgraph(&gd,&gm,"");
circle(150,70,70);
circle(120,50,10);
circle(190,50,10);
line(155,60,155,80);
arc(155,100,180,360,20);
line(130,140,130,170);
line(170,140,170,170);
rectangle(80,170,230,260);
line(110,260,110,360);
line(205,260,205,360);
line(80,190,55,240);
line(230,190,255,240);
getch();
}
PANKAJ GILL
411/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 2Write a program to draw a rectangle using line function
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm," ");
line(100,100,100,300);
line(100,100,300,100);
line(100,300,300,300);
line(300,100,300,300);
getch();
}
PANKAJ GILL
611/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 3Write a program to draw a line using DDA’s line drawing
algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
voidlineDDA(int,int,int,int);
void main()
{
int x1,y1,xn,yn;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("enter the starting coordinates of the line:");
scanf("%d%d",&x1,&y1);
printf("enter the ending coordinates of the line:");
scanf("%d%d",&xn,&yn);
lineDDA(x1,y1,xn,yn);
getch();
}
voidlineDDA(int x1,int y1,int xn,intyn)
{
intdx,dy,m,i;
m=(yn-y1)/(xn-x1);
for(i=x1;i<=xn;i++)
{
PANKAJ GILL
711/CSE/168
Practical File of Computer Graphics
if(m<=1)
{
dx=1;
dy=(m*dx);
}
else
{
dy=1;
dx=(dy/m);
}
x1=x1+dx;
y1=y1+dy;
{
putpixel(x1,y1,RED);
delay(20);
}
}
}
PANKAJ GILL
911/CSE/168
Practical File of Computer Graphics
PROGRAM NO.4Write a program to draw a line using Bresenham’s line
drawing algorithm
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
voidlineBres(int,int,int,int);
void main()
{
int x1,y1,xn,yn;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter the starting coordinate at line:");
scanf("%d%d", &x1, &y1);
printf("Enter the ending coordinate at line:");
scanf("%d%d", &xn, &yn);
lineBres(x1,y1,xn,yn);
getch();
}
voidlineBres(int x1,int y1,int xn,intyn)
{
int dx = xn-x1,dy=yn-y1;
int di = 2*dy-dx;
int ds = 2*dy,dt = 2*(dy-dx);
putpixel(x1,y1,RED);
PANKAJ GILL
1011/CSE/168
Practical File of Computer Graphics
while(x1<xn)
{
x1++;
if(di<0)
{
di=di+ds;
}
else
{
y1++;
di=di+dt;
}
putpixel(x1,y1,RED);
delay(20);
}
}
PANKAJ GILL
1211/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 5Write a program to draw a circle using equation of circle
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
int x,y,r,c1;
initgraph(&gd,&gm,"");
circle(200,200,50);
getch();
closegraph();
}
PANKAJ GILL
1411/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 6Write a program to draw a circle using Bresenham’s
circle drawing algorithm
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
voidcircleBres(int,int,int);
voiddrawcircle(int,int,int,int);
void main()
{
intxc,yc,r;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter the centre coordinates of the circle");
scanf("%d%d",&xc,&yc);
printf("Enter radius of circle:");
scanf("%d",&r);
circleBres(xc,yc,r);
getch();
}
voidcircleBres(intxc,intyc,int r)
{
int x=0,y=r;
int d=3-2*r;
while(x<y)
PANKAJ GILL
1511/CSE/168
Practical File of Computer Graphics
{
drawcircle(xc,yc,x,y);
x++;
if(d<0)
d=d+4*(x)+6;
else
{
y--;
d=d+4*(x-y)+10;
drawcircle(xc,yc,x,y);
delay(50);
}
}
}
voiddrawcircle(intxc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc+y,yc+x,YELLOW);
putpixel(xc-x,yc+y,BLUE);
putpixel(xc-y,yc+x,GREEN);
putpixel(xc-x,yc-y,GREEN);
putpixel(xc-y,yc-x,YELLOW);
putpixel(xc+y,yc-x,RED);
putpixel(xc+x,yc-y,YELLOW);
}
PANKAJ GILL
1711/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 7Write a program to draw a circle using midpoint circle
drawing algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
voidcirclemidpoint(int,int,int);
voiddrawcircle(int,int,int,int);
void main()
{
intxc,yc,r;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter center coordinates of the circle: ");
scanf("%d%d",&xc,&yc);
printf("Enter radius of the circle: ");
scanf("%d",&r);
circlemidpoint(xc,yc,r);
getch();
}
voidcirclemidpoint(intxc,intyc,int r)
{
int x=0,y=r;
int p=1-r;
while(x<y)
{
drawcircle(xc,yc,x,y);
x++;
if(p<0)
{
PANKAJ GILL
1811/CSE/168
Practical File of Computer Graphics
p=p+2*x+1;
}
else
{
y--;
p=p+2*(x-y)+1;
}
drawcircle(xc,yc,x,y);
delay(50);
}
}
voiddrawcircle(intxc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,BLUE);
putpixel(xc+x,yc-y,GREEN);
putpixel(xc-x,yc-y,RED);
putpixel(xc+y,yc+xGREEN);
putpixel(xc-y,yc+x,YELLOW);
putpixel(xc+y,yc-x, YELLOW);
putpixel(xc-y,yc-x, YELLOW);
}
PANKAJ GILL
2011/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 8Write a program to draw a circle using polar co-
ordinates
#include<graphics.h>
#include<math.h>
#include<conio.h>
voidacircle(inth,intk,int r);
voiddpixel(intx,inty,inth,int k);
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(YELLOW);
acircle(100,100,100);
getch();
closegraph();
}
voidacircle(inth,intk,int r)
{
inty,x;
int theta;
for(theta=0;theta<=360;theta+=1)
{ x=r*cos(theta);
y=r*sin(theta);
dpixel(x,y,h,k);
}
PANKAJ GILL
2111/CSE/168
Practical File of Computer Graphics
}
voiddpixel(intx,inty,inth,int k)
{
putpixel(x+h,y+k,RED);
putpixel(y+h,x+k,RED);
putpixel(-y+h,x+k,RED);
putpixel(-x+h,y+k,RED);
putpixel(-x+h,-y+k,RED);
putpixel(-y+h,-x+k,RED);
putpixel(y+h,-x+k,RED);
putpixel(x+h,-y+k,RED);
}
PANKAJ GILL
2311/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 9Write a program to fill a circle using Boundary Fill
Algorithm
#include<graphics.h>
#include<math.h>
#include<conio.h>
voiddcircle(inth,intk,int r);
voiddpixel(intx,inty,inth,int k);
voidcfill(intx,int y, intfcolor, intbcolor);
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(YELLOW);
dcircle(30,30,27);
cfill(30,30,BLUE,RED);
getch();
closegraph();
}
voiddcircle(inth,intk,int r)
{
inty,i;
PANKAJ GILL
2411/CSE/168
Practical File of Computer Graphics
for(i=0;i<=r;i++)
{
y=sqrt((r*r-(i)*(i)));
dpixel(i,y,h,k);
}
}
voiddpixel(intx,inty,inth,int k)
{
putpixel(x+h,y+k,RED);
putpixel(y+h,x+k,RED);
putpixel(-y+h,x+k,RED);
putpixel(-x+h,y+k,RED);
putpixel(-x+h,-y+k,RED);
putpixel(-y+h,-x+k,RED);
putpixel(y+h,-x+k,RED);
putpixel(x+h,-y+k,RED);
}
voidcfill(intx,int y, intfcolor, intbcolor)
{
int current;
current=getpixel(x,y);
if(current!=bcolor&& current!=fcolor)
PANKAJ GILL
2511/CSE/168
Practical File of Computer Graphics
{
putpixel(x,y,fcolor);
cfill(x+1,y,BLUE,RED);
cfill(x-1,y,BLUE,RED);
cfill(x,y+1,BLUE,RED);
cfill(x,y-1,BLUE,RED);
}
}
PANKAJ GILL
2711/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 10Write a program to fill a circle using Flood Fill Algorithm
#include<graphics.h>
#include<math.h>
#include<conio.h>
voiddcircle(inth,intk,int r);
voiddpixel(intx,inty,inth,int k);
voidffill(intx,int y, intfcolor, intbcolor);
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(YELLOW);
dcircle(30,30,27);
ffill(30,30,YELLOW,BLACK);
getch();
closegraph();
}
voiddcircle(inth,intk,int r)
{
inty,i;
for(i=0;i<=r;i++)
{
y=sqrt((r*r-(i)*(i)));
dpixel(i,y,h,k);
}
}
PANKAJ GILL
2811/CSE/168
Practical File of Computer Graphics
voiddpixel(intx,inty,inth,int k)
{
putpixel(x+h,y+k,RED);
putpixel(y+h,x+k,RED);
putpixel(-y+h,x+k,RED);
putpixel(-x+h,y+k,RED);
putpixel(-x+h,-y+k,RED);
putpixel(-y+h,-x+k,RED);
putpixel(y+h,-x+k,RED);
putpixel(x+h,-y+k,RED);
}
voidffill(intx,int y, intfcolor, intbcolor)
{
if(getpixel(x,y)==bcolor)
{
putpixel(x,y,fcolor);
ffill(x+1,y,YELLOW,BLACK);
ffill(x-1,y,YELLOW,BLACK);
ffill(x,y+1,YELLOW,BLACK);
ffill(x,y-1,YELLOW,BLACK);
}
}
PANKAJ GILL
3011/CSE/168
Practical File of Computer Graphics
PROGRAM NO. 11Write a program for line clipping using cohen Sutherland
algorithm
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
typedef unsigned intoutcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
voidlineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
intgd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{
accept =1 ; done =1;
}
else
if(code0 & code1)
done = 1;
else
{
PANKAJ GILL
3111/CSE/168
Practical File of Computer Graphics
floatx,y;
codeout = code0 ? code0 : code1;
if(codeout& TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if(codeout& BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout& RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if(codeout == code0)
{
PANKAJ GILL
3211/CSE/168
Practical File of Computer Graphics
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
}
while( done == 0);
if(accept)
line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
intcalcode (x,y,xwmin,ywmin,xwmax,ywmax)
floatx,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y>ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x >xwmax)
PANKAJ GILL
3311/CSE/168
Practical File of Computer Graphics
code |= RIGHT;
else if ( x<xwmin)
code |= LEFT;
return(code);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
intgd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
PANKAJ GILL
3411/CSE/168
Practical File of Computer Graphics
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}
PANKAJ GILL
3611/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 12Write a program to translate a triangle about the origin
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
voidRectAngle(intx,inty,intHeight,int Width);
void Translate(intx,inty,intHeight,int Width);
void main()
{
intgd=DETECT,gm;
intx,y,Height,Width;
initgraph(&gd,&gm," ");
printf("Enter the First point for the Rectangle:");
scanf("%d%d",&x,&y);
printf("Enter the Height&Width for the Rectangle:");
scanf("%d%d",&Height,&Width);
RectAngle(x,y,Height,Width);
getch();
cleardevice();
Translate(x,y,Height,Width);
RectAngle(x,y,Height,Width);
getch();
PANKAJ GILL
3711/CSE/168
Practical File of Computer Graphics
}
voidRectAngle(intx,inty,intHeight,int Width)
{
line(x,y,x+Width,y);
line(x,y,x,y+Height);
line(x+Width,y,x+Width,y+Height);
line(x,y+Height,x+Width,y+Height);
}
void Translate(intx,inty,intHeight,int Width)
{
intNewx,Newy,a,b;
printf("Enter the Transaction coordinates");
scanf("%d%d",&Newx,&Newy);
cleardevice();
a=x+Newx;
b=y+Newy;
RectAngle(a,b,Height,Width);
}
PANKAJ GILL
3911/CSE/168
Practical File of Computer Graphics
PRACTICAL NO. 13Write a program to scale a triangle about a fixed point
taken as one of the vertex of the triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
intsx,sy;
int poly[8];
intgd=DETECT,gm; initgraph(&gd,&gm,"");
cleardevice();
printf("Enter the first coordinates of triangle: ");
scanf("%d%d",&x1,&y1);
printf("Enter the second coordinates of triangle: ");
scanf("%d%d",&x2,&y2);
printf("Enter the third coordinates of triangle: ");
scanf("%d%d",&x3,&y3);
poly[0]=x1;
poly[1]=y1;
poly[2]=x2;
poly[3]=y2;
poly[4]=x3;
poly[5]=y3;
poly[6]=x1;
poly[7]=y1;
cleardevice();
drawpoly(4,poly);
getch();
printf("Enter the scaling factors: ");
PANKAJ GILL
4011/CSE/168
Practical File of Computer Graphics
scanf("%d%d",&sx,&sy);
x4=sx*x1-x1;
y4=sy*y1-y1;
x1=sx*x1-x4;
y1=sy*y1-y4;
x2=sx*x2-x4;
y2=sy*y2-y4;
x3=sx*x3-x4;
y3=sy*y3-y4;
poly[0]=x1;
poly[1]=y1;
poly[2]=x2;
poly[3]=y2;
poly[4]=x3;
poly[5]=y3;
poly[6]=x1;
poly[7]=y1;
getch();
cleardevice();
drawpoly(4,poly);2
getch();
closegraph();
}
PANKAJ GILL
4211/CSE/168
Practical File of Computer Graphics
Program No. 14Write a program to rotate a triangle about a fixed point
taken as one of the vertex of the triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
intgd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle: ");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle: ");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle: ");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(5);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
}
voidTriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
PANKAJ GILL
4311/CSE/168
Practical File of Computer Graphics
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3;
float Angle;
printf("Enter the angle for rotation: ");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=x2+(x1-x2)*cos(Angle)-(y1-y2)*sin(Angle);
b1=y2+(x1-x2)*sin(Angle)+(y1-y2)*cos(Angle);
a2=x2+(x2-x2)*cos(Angle)-(y2-y2)*sin(Angle);
b2=y2+(x2-x2)*sin(Angle)+(y2-y2)*cos(Angle);
a3=x2+(x3-x2)*cos(Angle)-(y3-y2)*sin(Angle);
b3=y2+(x3-x2)*sin(Angle)+(y3-y2)*cos(Angle);
printf("Rotated: ");
TriAngle(a1,b1,a2,b2,a3,b3);
}
PANKAJ GILL