You are on page 1of 10

B K Birla Institute of Engineering & technology (BKBIET)-Pilani

Computer Science Vth Semester


Computer Graphics (LAB)
Lab Sheet – I Date: 10/9/2009

In a C Program first of all you need to initialize the graphics drivers on the computer. This is
done using the initgraph method provided in graphics.h library. In the next few pages we will
discuss graphics.h library in details. Important functions in graphic.h library will be discuees in
details and samples programmes will be provided to show the power of C programming
language.

Graphics mode Initialization


First of all we have to call the initgraph function that will intialize the graphics mode on the
computer. initigraph have the following prototype.

void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
Initgraph initializes the graphics 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 graphresult to 0.

*graphdriver
Integer that specifies the graphics driver to be used. You can give graphdriver a value using a
constant of the graphics_drivers enumeration type.

*graphmode
Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver
= DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver.
You can give *graphmode a value using a constant of the graphics_modes enumeration type.

*pathtodriver
Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.

1. If they’re not there, initgraph looks in the current directory.


2. If pathtodriver is null, the driver files must be in the current directory.
*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or
you’ll get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set
to the current graphics mode. You can tell initgraph to use a particular graphics driver and mode,
or to autodetect the attached video adapter at run time and pick the corresponding driver. If you
tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode.

Normally, initgraph loads a graphics driver by allocating memory for the driver (through
_graphgetmem), then loading the appropriate .BGI file from disk.As an alternative to this
dynamic loading scheme, you can link a graphics driver file (or several of them) directly into
your executable program file.

Here is a sample program that initializes the graphics mode in C Language.

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
The graphics programming in c language is discussed in brief to provide an over view to the
beginner.

/* Sample program to draw a circle*/


#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,""); /* initialization of graphic mode */
circle(150,150,100);
getch();
closegraph(); /* Restore orignal screen mode */
}
/* End of program */
Normally the screen which u view in DOS is in the text mode which means it is meant for text.
And for graphics u need to initialize graphics mode.

circle(x coordinate ,y coordinate , radius);


The circle command takes a X coordinate which means Vertical axis and Y coordinate which
means Horizontal axis. And the last one is the radius of the circle. closegraph();

With out this function the screen mode will still remain in graphic mode and when u come out, to
DOS you will see a different screen, which is not in the text mode.

/*A program to draw a space with stars*/


#include<graphics.h>
main()
{
int gd=DETECT,gm;
int i,x,y;
initgraph(&gd,&gm,"");
line(0,0,640,0);
line(0,0,0,480);
line(639,0,639,480);
line(639,479,0,479);
for(i=0;i<=1000;i++)
{
x=rand()%639;
y=rand()%480;
putpixel(x,y,15);
}
getch();
closegraph();
}
/* End of program */
/*Here a sample program to illustrate how to use BARS which are used for visual statistics */
#include<graphics.h>
main() {
int gd=DETECT,gm,maxx,maxy,x,y,button;
initgraph(&gd,&gm,"");
line(80,150,200,150);
line(80,150,80,50);
settextstyle(1,HORIZ_DIR,1);
outtextxy(100,153,"<-X axis");
settextstyle(1,VERT_DIR,1);
outtextxy(60,50,"<-Y axis");
bar(100,100,120,150);
bar(130,120,150,150);
getch();
closegraph();
}

Graphics library provided by borland C is most widely used library for graphics programming.
Mostly this graphics library is restricted to be used under 16 bit C programming and MS DOS
environment. As discussed earlier that first of all you need to initialize the graphics drivers on
the computer. This is done using the initgraph() method provided in graphics.h library.
graphics.h is used to include the reference to the graphics library, but actual graphics library can
be found in lib directory with the name of graphics.lib. In Dev C++ there is no default graphics
library, but there are some third party graphics libraries to be used with Dev C++. You can
download this library here, after downloading the library you can read through the manual and
copy all the files at their desired locations. Also you will have to add reference to this graphics
library in your projects or programs.

Adding the graphics.h in your C programs


If you are using borland C/C++ compiler to program, then you will have to follow these simple
steps in orderd to get your C or C++ program running in graphics mode.

• First you will need to add #include <graphics.h> reference at the top of you C/C++
program.
• Next step is to initialize the graphics environment. This can be achieved using the
function

void far initgraph(int far *driver, int far *mode, char far *path)

• Here path is the actual path to the graphics library. You can give the path to this library
by giving the complete path, like “C:\\TC\\BGI”, where BGI is the graphics library directory.
• After initializing the graphics mode you can check for any error which may happen while
initializing the graphics mode. The function used to find out any errors is int errorcode =
graphresult() which returns the error code for the specific error.
• If you pass this errorcode to grapherrormsg() function the, it will return the complete
description of the error message. And if the errorcode==grOk the you are on the way to
develop your first graphics program using C/C++.

If you are using Dev C++ compiler for your graphcis programs then you can follow these simple
steps to configure your environment for graphics programming.

Simple graphics functions


First part of the post discusses the necesary functions to get the program ready to draw shapes
and styles, second post provides description of necessary functions to draw different shapes and
fill them with colors or textures. And the last part is a sample program to demonstrate that how
you can write programs in graphics mode.

Function circle
Draws the circle on the screen using a point(x,y) and the radius for the circle.

void far circle(int x, int y, int radius)

Function line
This function draws a line in the graphics mode.

void far line(int startx, int starty, int endx, int endy)

Function outtext
Displays a text string on the grpahics screen.

void far outtext(char far *str)

Function outtextxy
Displays the text at a specified position in graphics mode.

void far outtext(int x, int y, char *str)

Function rectangle
Draws a rectangular box on the screen using the points given in the parameters.

void far rectangle(int left, int top, int right, int bottom)
Q1. Implementation of line generation using slope’s method, DDA and Bresenham’s
algorithms

/* DDA Line Drawing Algorithm */

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>

void ddaLine(int x1,int y1,int x2,int y2)


{
float x=x1,y=y1,dx,dy;
int length,i;

putpixel(x1,y1,WHITE);

if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
dx=(float)(x2-x1)/length;
dy=(float)(y2-y1)/length;
for(i=1;i<=length;i++)
{
x=x+dx;
y=y+dy;
putpixel((int)x,(int)y,WHITE);
}
}

void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");

printf("Enter the starting co-ordinates: ");


scanf("%d %d",&x1,&y1);
printf("Enter the ending co-ordinates: ");
scanf("%d %d",&x2,&y2);

ddaLine(x1,y1,x2,y2);
getch();
}
/* DDA Line Drawing Algorithm */

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>

void ddaLine(int x1,int y1,int x2,int y2)


{
float x=x1,y=y1,dx,dy;
int length,i;

putpixel(x1,y1,WHITE);

if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
dx=(float)(x2-x1)/length;
dy=(float)(y2-y1)/length;
for(i=1;i<=length;i++)
{
x=x+dx;
y=y+dy;
putpixel((int)x,(int)y,WHITE);
}
}

void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");

printf("Enter the starting co-ordinates: ");


scanf("%d %d",&x1,&y1);
printf("Enter the ending co-ordinates: ");
scanf("%d %d",&x2,&y2);

ddaLine(x1,y1,x2,y2);
getch();
}
/* Bressenhams Line Drawing Algorithm */

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>

void bshmLine(int x1,int y1,int x2,int y2)


{
int x=x1,y=y1,dx,dy,s1,s2;
int length,i,dp,temp,swap=0;

putpixel(x1,y1,WHITE);
dx=abs(x2-x1);
dy=abs(y2-y1);

if(x2<x1) s1=-1;
else if(x2>x1) s1=1;
else s1=0;

if(y2<y1) s2=-1;
else if(y2>y1) s2=1;
else s2=0;

dp=2*dy-dx;
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
swap=1;
}
for(i=1;i<=dx;i++)
{
if(dp<0)
{
if(swap) putpixel(x,y=y+s2,WHITE);
else putpixel(x=x+s1,y,WHITE);
dp+=2*dy;
}
else
{
putpixel(x=x+s1,y=y+s2,WHITE);
dp=dp+2*dy-2*dx;
}
}
}

void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");

printf("Enter the starting co-ordinates: ");


scanf("%d %d",&x1,&y1);
printf("Enter the ending co-ordinates: ");
scanf("%d %d",&x2,&y2);

bshmLine(x1,y1,x2,y2);
getch();
}

You might also like