Professional Documents
Culture Documents
COMPUTER GRAPHICS
SUBMITTED BY
SUBMITTED TO
PROF. MANPREET KAUR
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");
scanf("%d%d",&x1,&y1);
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");
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();
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");
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);
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();
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();
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();
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();
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
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");
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;