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

Practical File of Computer Graphics

The document is a practical file for computer graphics that contains 14 programs. Each program draws a different shape or performs a different graphics algorithm. The programs include drawing a stick man, rectangle, lines using DDA and Bresenham's algorithm, circles using various algorithms like midpoint, boundary fill, and flood fill. It also includes programs for line clipping and transformations like translation, scaling, and rotation of triangles. For each program, it lists the page number and provides the code and output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
433 views

Practical File of Computer Graphics

The document is a practical file for computer graphics that contains 14 programs. Each program draws a different shape or performs a different graphics algorithm. The programs include drawing a stick man, rectangle, lines using DDA and Bresenham's algorithm, circles using various algorithms like midpoint, boundary fill, and flood fill. It also includes programs for line clipping and transformations like translation, scaling, and rotation of triangles. For each program, it lists the page number and provides the code and output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Name : shivanjali Tanaji shinde

Class. : SYCO-B
Batch. : B4
Roll No : 144
Sub. : CGR

PRACTICAL FILE OF
COMPUTER
GRAPHICS
Program with
output
INDEX
Program Page Remarks
1. Write a program to draw a stick man 2
2. Write a program to draw a rectangle using 4
line function
3. Write a program to draw a line using DDA’s line 6
drawing algorithm
4. Write a program to draw a line using 9
Bresenham’s line drawing algorithm
5. Write a program to draw a circle using 12
equation of circle
6 Write a program to draw a circle using 14
Bresenham’s circle drawing algorithm
7. Write a program to draw a circle using midpoint 17
circle drawing algorithm
8. Write a program to draw a circle using polar co- 20
ordinates
9. Write a program to fill a circle using Boundary 23
Fill Algorithm
10. Write a program to fill a circle using Flood Fill 27
Algorithm
11. Write a program for line clipping using cohen- 30
Sutherland algorithm
12. Write a program to translate a triangle 36
about the origin
13. Write a program to scale a triangle about a 39
fixed point taken as one of the vertex of
the triangle
14. Write a program to rotate a triangle about 42
a fixed point taken as one of the vertex of
the triangle

1
Write 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();

2
OUTPUT

3
Write 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();

4
OUTPUT

5
Write 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++)

6
{

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);

7
OUTPUT

8
Write 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);

9
putpixel(x1,y1,RED);

while(x1<xn)

x1++;

if(di<0)

di=di+ds;

else

y1++;

di=di+dt;

putpixel(x1,y1,RED);

delay(20);

10
OUTPUT

11
Write 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();

12
OUTPUT

13
Write 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;

14
while(x<y)

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);

15
OUTPUT

16
Write 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)
{

17
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);
}

18
OUTPUT

19
Write 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);

20
}

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);

21
OUTPUT

22
Write 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;

23
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)

24
{

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);

25
OUTPUT

26
Write 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);
}
}

27
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);
}
}

28
OUTPUT

29
Write 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

30
{

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)

31
{

x0 = x; y0 = y;

code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywma
x);

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;

32
PRACTICAL FILE OF COMPUTER GRAPHICS
else if(x >xwmax)

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();

33
clrscr();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );

getch();

closegraph();

34
OUTPUT

35
PRACTICAL NO. 12
Write 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();

36
}

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);

37
OUTPUT

38
Write 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: ");

39
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();
}

40
OUTPUT

After Scaling

41
Write 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)
{

42
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);
}

43
OUTPUT

44
PANKAJ GILL 45
11/CSE/168

You might also like