List Of Experiments
Object 1: WAP to Create Basic Shapes.
Object 2: WAP to Create Circle Shapes.
Object 3: WAP to Create Kite Shapes.
Object 4: WAP to Create Fish Shapes.
Object 5: WAP to Create Flag Shapes.
Object 6: WAP to Create Hut Shapes.
Object 7:- Implementation of line generation using DDA algorithms.
Object 8:- Implementation of Line generation using Bresenham’s algorithms.
Object 9:- Implementation of polygon filling.
Object 10:- Implementation of Circle generation using Bresenham’s algorithms.
Hardware and Software requirement
a) Software requirement : Turbo C / C++
b) Hardware requirement
• Intel Pentium III800 MHz Processor
• Intel chipset 810 Motherboard
• 14” colour Monitor
• Mouse
• Keyboard
• 2GB HDD
• 256 MB RAM
BASIC GRAPHICS FUNCTION
1) INITGRAPH
• Initializes the graphics system.
Declaration
• Void far initgraph(int far *graphdriver)
Remarks
• To start the graphic system, you must first call initgraph.
• Initgraph initializes the graphic system by loading a graphics driver from disk (or
validating a registered driver) then putting the system into graphics mode.
• Initgraph also resets all graphics settings (color, palette, current position, viewport, etc)
to their defaults then resets graph.
2) GETPIXEL, PUTPIXEL
• Getpixel gets the color of a specified pixel.
• Putpixel places a pixel at a specified point.
Decleration
• Unsigned far getpixel(int x, int y)
• Void far putpixel(int x, int y, int color)
Remarks
• Getpixel gets the color of the pixel located at (x,y);
• Putpixel plots a point in the color defined at (x, y).
Return value
• Getpixel returns the color of the given pixel.
• Putpixel does not return.
3) CLOSE GRAPH
• Shuts down the graphic system.
Decleration
• Void far closegraph(void);
Remarks
• Close graph deallocates all memory allocated by the graphic system.
• It then restores the screen to the mode it was in before you called initgraph.
Return value
• None.
4) ARC, CIRCLE, PIESLICE
• arc draws a circular arc.
• Circle draws a circle
• Pieslice draws and fills a circular pieslice
Decleration
• Void far arc(int x, int y, int stangle, int endangle, int radius);
• Void far circle(int x, int y, int radius);
• Void far pieslice(int x, int y, int stangle, int endangle, int radius);
Remarks
• Arc draws a circular arc in the current drawing color
• Circle draws a circle in the current drawing color
• Pieslice draws a pieslice in the current drawing color, then fills it using the current fill
pattern and fill color.
5) ELLIPSE, FILLELIPSE, SECTOR
• Ellipse draws an elliptical arc.
• Fillellipse draws and fills ellipse.
• Sector draws and fills an elliptical pie slice.
Decleration
• Void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius)
• Void far fillellipse(int x, int y, int xradius, int yradius)
• Void farsectoe(int x, int y, int stangle, int endangle, int xradius, int yradius)
Remarks
• Ellipse draws an elliptical arc in the current drawing color.
• Fillellipse draws an elliptical arc in the current drawing color and than fills it with fill
color and fill pattern.
• Sector draws an elliptical pie slice in the current drawing color and than fills it using the
pattern and color defined by setfillstyle or setfillpattern.
6) FLOODFILL
• Flood-fills a bounded region.
Decleration
• Void far floodfill(int x, int y, int border)
Remarks
• Floodfills an enclosed area on bitmap device.
• The area bounded by the color border is flooded with the current fill pattern and fill
color.
• (x,y) is a “seed point”
If the seed is within an enclosed area, the inside will be filled.
If the seed is outside the enclosed area, the exterior will be filled.
• Use fillpoly instead of floodfill wherever possible so you can maintain code
compatibility with future versions.
• Floodfill doesnot work with the IBM-8514 driver.
Return value
• If an error occurs while flooding a region, graph result returns ‘1’.
7) GETCOLOR, SETCOLOR
• Getcolor returns the current drawing color.
• Setcolor returns the current drawing color.
Decleration
• Int far getcolor(void);
• Void far setcolor(int color)
Remarks
• Getcolor returns the current drawing color.
• Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
• To set a drawing color with setcolor , you can pass either the color number or the
equivalent color name.
8) LINE,LINEREL,LINETO
• Line draws a line between two specified pints.
• Onerel draws a line relative distance from current position(CP).
• Linrto draws a line from the current position (CP) to(x,y).
Decleration
• Void far lineto(int x, int y)
Remarks
• Line draws a line from (x1, y1) to (x2, y2) using the current color, line style and
thickness. It does not update the current position (CP).
• Linerel draws a line from the CP to a point that is relative distance (dx, dy) from the
CP, then advances the CP by (dx, dy).
• Lineto draws a line from the CP to (x, y), then moves the CP to (x,y).
Return value
• None
9) RECTANGLE
• Draws a rectangle in graphics mode.
Decleration
• Void far rectangle(int left, int top, int right, int bottom)
Remarks
• It draws a rectangle in the current line style, thickness and drawing color.
•(left, top) is the upper left corner of the rectangle, and (right, bottom) is its lower right
corner.
Return value
• None.
Object 1: WAP to Create Basic Shapes.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x=
300,y=150,radius=50;
initgraph(&gd, &gm, "C:\\TC\\BGI");
rectangle(left, top, right, bottom);
circle(x, y, radius);
bar(left + 300, top, right + 300, bottom);
line(left - 10, top + 150, left + 410, top + 150);
ellipse(x, y + 200, 0, 360, 100, 50);
outtextxy(left + 100, top + 325, "this is my first shape/graphics");
getch();
closegraph();
return 0;
}
Object 2: WAP to Create Circle Shapes.
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r,c1;
initgraph(&gd,&gm,"");
cout<<"enter x,y,r";
cin>>x>>y>>r;
circle(x,y,r);
c1=rand();
setfillstyle(SOLID_FILL,c1);
floodfill(x,y,2);
getch();
closegraph();
}
Object 3: WAP to Create Kite Shapes.
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a,b,gd=DETECT,gm,i;
initgraph(&gd,&gm," ");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380);
setcolor(4);
getch();
closegraph();
}
OUTPUT:
Object 4: WAP to Create Fish Shapes.
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm ,"");
ellipse(200,200,0,360,50,30);
line(250,200,280,170);
line(280,170,280,230);
line(280,230,250,200);
circle(160,190,3);
getch();
closegraph();
}
OUTPUT:
Object 5: WAP to Create Flag Shapes.
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
setcolor(5);
line(20,50,20,180);
line(20,50,100,50);
line(20,70,100,70);
line(20,90,100,90);
line(20,110,100,110);
line(100,50,100,110);
circle(60,80,10);
line(52,80,68,80);
line(60,72,60,88);
setfillstyle(SOLID_FILL,22);
floodfill(21,51,5);
setfillstyle(SOLID_FILL,7);
floodfill(21,75,5);
setfillstyle(SOLID_FILL,2);
floodfill(21,95,5);
setfillstyle(SOLID_FILL,7);
floodfill(81,75,5);
setfillstyle(SOLID_FILL,7);
floodfill(61,81,5);
getch();
closegraph();
}
OUTPUT:
Object 6: WAP to Create Hut Shapes.
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
line(100,100,150,50);
line(150,50,200,100);
line(100,100,200,100);
line(150,50,350,50);
line(200,100,350,100);
line(350,50,350,100);
circle(150,75,10);
rectangle(100,100,200,300);
rectangle(200,100,350,300);
rectangle(250,175,300,225);
line(250,175,300,225);
line(300,175,250,225);
line(125,300,125,225);
line(175,300,175,225);
arc(150,225,0,180,25);
getch();
closegraph();
}
OUTPUT:
Object 7:- Implementation of line generation using DDA algorithms.
#include<stdio.h>
#include<conio.h>
#include<graphics.h> void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
printf("enter first point");
scanf("%d%d",&x1,&y1);
printf("enter second point");
scanf("%d%d",&x2,&y2);
x=x1;
y=y1;
putpixel(x,y,7);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/s;
yi=dy/s;
x=x1;
y=y1;
putpixel(x,y,7);
for(k=0;k<s;k++)
{
x=x+xi;
y=y+yi;
putpixel(x,y,7);
}
getch();
closegraph();
}
OUTPUT:
Object 8:- Implementation of line generation using Bresenham’s
algorithms.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:");
printf("\nEnter the x-coordinate of the first point ::"); scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::"); scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2dy-dx);
while(x<=x2)
{
if(p<0)
{
x=x+1;
p=2*x-dx;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();
}
OUTPUT:
Object 9:- Implementation of polygon filling.
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi"); int p=1,x;
int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
drawpoly(6,a);
for(int i=100;i<200;i++)
{ p=1;
for(int j=100;j<=200;j++)
{
x=getpixel(j,i);
for(int d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1] )
break;
else
{
if(x>0&&d==10)
p++;
if(p%2==0)
putpixel(j,i,4);
}}}}
getch();
closegraph();
}
OUTPUT
Object 10:- Implementation of Circle generation using Bresenham’s
algorithms.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlepoints(int,int);
void main()
{
int x,y,p,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
clrscr();
printf("enter the radius");
scanf("%d",&r);
x=0;y=r;p=1-r;
while(x<y)
{
x++;
if(p>0)
{
p=p+2*(x-y)+1;
y--;
}
else
p=p+2*x+1;
circlepoints(x,y);
}
getch();
closegraph();
}
void circlepoints(int x,int y)
{
putpixel(x+300,y+300,8);
putpixel(x+300,-y+300,8);
putpixel(-x+300,y+300,8);
putpixel(-x+300,-y+300,8);
putpixel(y+300,x+300,8);
putpixel(y+300,-x+300,8);
putpixel(-y+300,x+300,8);
putpixel(-y+300,-x+300,8);
}
OUTPUT: