You are on page 1of 66

PRACTICAL FILE OF

COMPUTER GRAPHICS

SUBMITTED BY

NAME : NAZAM SINGLA


CLASS : M.SC IT (SEM – IV)
ROLL NO. : 905581

SUBMITTED TO
PROF. MANPREET KAUR

SHANTI TARA GIRLS COLLEGE


AHMEDGARH
(Affiliated to Punjabi University Patiala)
INDEX
Sr. No. Aim Page no. Remarks
Introduction of Computer Graphics 04
1. Write a program to draw basic shapes. 05 – 06
2. Write a program of concentric circle 07 – 08
3. Write a program to print a line using DDA 09 – 10
algorithms.
4. Write a program to draw pie chart. 11 – 12
5. Write a program to set the background 13 – 14
color.

6. Write a program a draw a human face. 15 – 16


7. Write to print line using BRESENHAM’S 17 – 18
algorithms.
8. Write a program to draw a circle. 19 – 20
9. Write a program to display the current 21 – 22
position of the y-coordinates.
10. Write a program to display the current 23 – 24
position of the x-coordinates.
11. Write a program to retrieves the color of 25 – 26
pixel.
12. Write a program to retrieves the maximum 27 – 28
y-coordinate for the current graphic mode
and driver using the getmax function.
13. Write a program to retrieves the maximum 29 – 30
x-coordinate for the current graphic mode
and driver using the getmax function.
14. Write a program to retrieves the maximum 31 – 32
number of colors available for the current
graphic mode.
15. Write a program to retrieves the name of 33 – 34
current graphics driver using the
getdrivername function.

16. Write a program to retrieves the current 35 – 36


drawing color using the getcolor function.

17. Write a program to retrieves the current 37 – 38


background color using the getbkcolor
function.
18. Write a program to draw an arc using the 39 – 40
arc function.

19. Write a program to draw a circle with 41 – 42


radius of 100 pixels using the circle
function.

20. Write a program to draw a triangle. 43 – 44

21. Write a program to draw an ellipse. 45 – 46


22. Write a program to display a message and 47 – 48
close the graphics mode when any key is
pressed.

23. Write a program to draw a 3D bar. 49 – 50


24. Write a program to draw a bar. 51 – 52
25. Write a program to draw an arc. 53 – 54
WHAT IS COMPUTER GRAPHICS

Graphics are defined as any sketch or a drawing or a special network that pictorially
represents some meaningful information. Computer Graphics is used where a set of
images needs to be manipulated or the creation of the image in the form of pixels and is
drawn on the computer. Computer Graphics can be used in digital photography, film,
entertainment, electronic gadgets, and all other core technologies which are required. It is
a vast subject and area in the field of computer science. Computer Graphics can be used
in UI design, rendering, geometric objects, animation, and many more. In most areas,
computer graphics is an abbreviation of CG. There are several tools used for the
implementation of Computer Graphics. The basic is the <graphics.h> header file in Turbo-
C, Unity for advanced and even OpenGL can be used for its Implementation.

The term ‘Computer Graphics’ was coined by Verne Hudson and William Fetter from
Boeing who were pioneers in the field.
Computer Graphics refers to several things:
 The manipulation and the representation of the image or the data in a graphical
manner.
 Various technology is required for the creation and manipulation.
 Digital synthesis and its manipulation.
Types of Computer Graphics
 Raster Graphics: In raster, graphics pixels are used for an image to be drawn. It is
also known as a bitmap image in which a sequence of images is into smaller pixels.
Basically, a bitmap indicates a large number of pixels together.
 Vector Graphics: In vector graphics, mathematical formulae are used to draw different
types of shapes, lines, objects, and so on.

Applications
 Computer Graphics are used for an aided design for engineering and
architectural system- These are used in electrical automobiles, electro-mechanical,
mechanical, electronic devices. For example gears and bolts.
 Computer Art – MS Paint.
 Presentation Graphics – It is used to summarize financial statistical scientific or
economic data. For example- Bar chart, Line chart.
 Entertainment- It is used in motion pictures, music videos, television gaming.
 Education and training- It is used to understand the operations of complex systems.
It is also used for specialized system such for framing for captains, pilots and so on.
 Visualization- To study trends and patterns.For example- Analyzing satellite photo of
earth.
1. Write a program to draw basic shapes.
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

int poly[12]={350,450,350,410,430,400,350,350,400,310,350,450};

initgraph(&gd,&gm,"C:\\TC\\BGI");

setbkcolor(BLUE);

setcolor(YELLOW);

circle(100,100,50);

outtextxy(75,170,"circle");

rectangle(200,50,350,150);

outtextxy(240,170,"rectangle");

ellipse(500,100,0,360,100,40);

outtextxy(480,170,"ellipse");

line(100,250,540,250);

outtextxy(300,260,"line");

sector(150,400,30,200,90,50);

outtextxy(120,460,"sector");

drawpoly(6,poly);

fillpoly(6,poly);

outtextxy(340,460,"polygon");
arc(520,410,0,150,60);

outtextxy(500,420,"arc");

getch();

closegraph();

}
2.Write a program of concentric circle.
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

int x=320,y=240,radius;

initgraph(&gd,&gm,"c:\\TC\\BGI");

outtextxy(250,80,"concentric circle");

for(radius=25;radius<=125;radius=radius+20);

circle(x,y,radius);

getch();

closegraph(); }
3.Write a program to print a line using DDA algorithms.
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<dos.h>

void main(){

int gd=DETECT,gm;

int x,y,x1,y1,x2,y2,dy,dx,m;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("enter the firstend points(x1,y1):\n");

scanf("%d%d",&x1,&y1);

printf("\n enter the second endpoints(x2,y2):\n");

scanf("%d%d",&x2,&y2);

dy=(y2-y1);

dx=(x2-x1);

m=dy/dx;

x=x1;

y=y1;
dx=1;

dy=1;

while((x<x2)||(y<=y2))

if(m<1)

x=x+dx;

y=(y+m);

putpixel(x,y,GREEN); }

if(m<1){

y=y+dy;

x=(x/(1/m));

putpixel(x,y,WHITE); } }

getch();

closegraph(); }
4.Write a program to draw pie chart.
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

settextstyle(SANS_SERIF_FONT_HORIZ_DIR,2);
set color(WHITE);

outtextxy(275,80,"PIE CHART");

midx=getmaxx()/2;

midy=getmaxy()/2;

setfillstyle(LINE_FILL,BLUE);

pieslice(midx,midy,0,75,100);

setfillstyle(X HATCH_FILL,RED);

pieslice(midx,midy,75,225,100);

setfillstyle(WIDE_DOT_FILL,GREEN);

pieslice(midx,midy,225,360,100);

getch();
5.Write a program to set the background color.
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm,i;

initgraph(&gd,&gm,"C:\\TC\\BGI");

for(i=0;i<12;i++)

setbkcolor(i);
delay(1000);

getch();

closegraph();

return 0;

}
6.Write a program a draw a human face.
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

circle(300,180,80);

circle(270,150,10);

circle(320,150,10);

line(300,170,300,200);

arc(300,180,240,300,50);

outtextxy(280,280,"HUMANFACE");

getch();

closegraph();

}
7.Write to print line using BRESENHAM’S algorithms.
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

#include<stdlib.h>

void main()

int gd=DETECT,gm;

float x,y,y1,y2,x1,x2,p,dx,dy;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("enter the values=");

scanf("%f%f%f%f",&x1,&x2,&y1,&y2);

dx=x2-x1;

dy=y2-y1;

p=2*dy-dx;

x=x1;

y=y1;

putpixel(x,y,WHITE);

while(x<x2)

if(p<0)

{
p=p+2*dy;

else

p=p+2*dy-2*dx;

y++; }

x++;

putpixel(x,y,15); }

getch();

closegraph(); }
8.Write a program to draw a circle.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\TC\\BGI");

setcolor(RED);

setbkcolor(YELLOW);

circle(250,250,50);

graphdefaults();
getch();

closegraph();

return 0;

}
9.Write a program to display the current position of the y-coordinates.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,y;

char array[100];

initgraph(&gd,&gm,"c:\\TC\\BGI");

y=gety();

sprintf(array,"current position of y=%d",getx());

outtext(array);

getch();

closegraph();

return 0;

}
10.Write a program to display the current position of the x-coordinates.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

char array[100];

initgraph(&gd,&gm,"c:\\TC\\BGI");

sprintf(array,"current position of x=%d",getx());

outtext(array);

getch();

closegraph();

return 0;

}
11.Write a program to retrieves the color of pixel.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,color;

char array[50];

initgraph(&gd,&gm,"c:\\TC\\BGI");

color=getpixel(0,0);

sprintf(array,"color of pixel at(0,0)=%d",color);

outtext(array);

getch();

closegraph();

return 0;

}
12.Write a program to retrieves the maximum y-coordinate for the current
graphic mode and driver using the getmax function.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,max_y;

char array[100];

initgraph(&gd,&gm,"c:\\TC\\BGI");

max_y=getmaxx();

sprintf(array,"maximum y coordinate for current graphics mode and driver=%d",max_y);

outtext(array);

getch();

closegraph();

return 0;

}
13.Write a program to retrieves the maximum x-coordinate for the current
graphic mode and driver using the getmax function.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,max_x;

char array[100];

initgraph(&gd,&gm,"c:\\TC\\BGI");

max_x=getmaxx();

sprintf(array,"maximum x coordinate for current graphics mode and driver=%d",max_x);

outtext(array);

getch();
closegraph();

return 0;

}
14.Write a program to retrieves the maximum number of colors available for
the current graphic mode.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,max_colors;

char a[100];

initgraph(&gd,&gm,"c:\\TC\\BGI");

max_colors=getmaxcolor();

sprintf(a,"maximum number of color for current graphics mode and driver=%d",max_colors+1);

outtextxy(0,40,a);

getch();

closegraph();

return 0;

}
15.Write a program to retrieves the name of current graphics driver using the
getdrivername function.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

char*drivername;

initgraph(&gd,&gm,"c:\\tc\\BGI");

drivername=getdrivername();

outtextxy(200,200,drivername);

getch();

closegraph();

return 0;

}
16.Write a program to retrieves the current drawing color using the getcolor
function.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,drawing_color;

char a[100];

initgraph(&gd,&gm,"c:\\tc\\BGI");

drawing_color=getcolor();

sprintf(a,"current drawing color=%d",drawing_color);

outtextxy(10,10,a);

getch();

closegraph();

return 0;
}
17.Write a program to retrieves the current background color using the
getbkcolor function.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,bkcolor;

char a[100];

initgraph(&gd,&gm,"c:\\tc\\BGI");

bkcolor=getbkcolor();
sprintf

(a,"current backgroung color=%d",bkcolor);

outtextxy(10,10,a);

getch();

closegraph();

return 0;

}
18.Write a program to draw an arc using the arc function.
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

main()

int gd=DETECT,gm;

struct arccoordstype a;

char arr[100];

initgraph(&gd,&gm,"c:\\tc\\BGI");

arc(250,200,0,90,100);

sprintf(arr,"(%d,%d)",a.xstart,a.ystart);

outtextxy(360,195,arr);

sprintf(arr,"(%d,%d)",a.xend,a.yend);

outtextxy(245,85,arr);

getch();

closegraph();

return 0;

}
19.Write a program to draw a circle with radius of 100 pixels using the circle
function.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\BGI");

setcolor(RED);

circle(100,100,RED);

getch();

closegraph();

return 0;

}
20.Write a program to draw a triangle.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm,points[]={320,150,440,340,230,340,320,150};

initgraph(&gd,&gm,"c:\\tc\\BGI");

fillpoly(4,points);

getch();

closegraph();
return 0;

}
21.Write a program to draw an ellipse.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\BGI");

fillellipse(100,100,50,25);

getch();

closegraph();

return 0;

}
22.Write a program to display a message and close the graphics mode when any
key is pressed.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\BGI");

outtext("press any key to close the graphics mode....");

getch();

closegraph();

return 0;

}
23.Write a program to draw a 3D bar.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\BGI");

bar3d(100,100,200,200,20,1);

getch();

closegraph();

return 0;

}
24.Write a program to draw a bar.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\BGI");

bar(100,100,200,200);

getch();
closegraph();

return 0;

}
25.Write a program to draw an arc.
#include<graphics.h>

#include<conio.h>

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\BGI");

arc(100,100,0,135,50);

getch();

closegraph();

return 0;

You might also like