You are on page 1of 31

Abstract

The C Graphics is for those who want to learn fundamentals of Graphics programming,
without any prior knowledge of graphics. The thesis contains lots of fundamental
graphic programs like drawing of various geometrical shapes (such as rectangle,
triangle, circle etc.), use of mathema-tical functions in drawing curves, coloring an
object with different objects and patterns and sim-ple animation programs like jumping
balls and moving cars etc. This project will provide you an overview of computer
graphics and its fundamentals.

In the first part of this project, we discuss about the Computer graphics, the monitor, C
gra-phics, Graphics Drivers & Graphics Mode. In the second part of the thesis we discuss
the func-tions which are used in the graphics programs.

In the final part of the project we discuss about various C graphics programs for
drawing geo-metrical figures and graphs such as triangle, ellipse, graph of sine etc.
CONTENTS

1. Introduction to C
2. History of C
3. Introduction to Graphics:
a. C Graphics
i. Graphics Drivers and
ii. Graphics Modes
b. Setting text mode
i. text mode() Function
ii. text background() Function
iii. text color() Function
iv. gotoxy() Function
v. clrscr() Function
vi. cputs() Function
c. Setting Graphic Mode
i. initgraph() Function
ii. closegraph() Function
iii. getmaxx() Function
iv. getmaxy() Function
v. restorecrtmode() Function
vi. setcolor() Function
vii. setbkcolor() Function
viii. setpalatte() Function
ix. putpixel() Function
x. line() Function
xi. moveto() Function
xii. lineto() Function
xiii. linerel() Function
xiv. outtext() Function
xv. outtextxy() Function
xvi. rectangle() Function
xvii. circle() Function
xviii. arc() Function
xix. ellipse() Function
4.Conclusion
5.References
1 Graphics in C

1. Introduction to C

C
Language is one of the most powerful programming languages used to write
computer programs for variety of applications. C is very simple, precise, reliable
and easy language to use and understand. Probably due to its simplicity most
programmers prefer C language than any other languages such as FORTRAN, PASCAL,
PL/I etc.

C is also called a general purpose programming language. A typical C program is


made up of several functions which may be contained in one or more source files called
C library. Every C program must have a function named main where the program
execution always begins.

Programs written in C are efficient & fast. It is many times faster than BASIC (Proga-
mming language), i.e. a program to increment a variable from 0 to 15000 takes about
one second in C while it takes more than 50 seconds in an interpreter.

C has flexible features to write programs for numerical, commercial and graphical
applications. It is also used by engineers to write programs for lower level accessing
along with assembly language.

C is highly portable that means that C program written for one computer can be run
on other computers. Many operating system programs for latest computers and
compilers are written using C language.
2 Graphics in C

2. History of C

C is a programming language which has been closely


associated with the UNIX operating system for which it was
developed. Many of the important ideas of C stem from the
language BCPL (or B are type less languages) developed by
Martin Richards.

In 1972, Dennis Ritchie at AT & T Bell laboratories writes C


and in 1978 the publication of the C programming language by Kernigham & Ritchie
caused a revolution in the computing world.

In 1983, the ANSI established a committee to provide a modern comprehensive


definition of C. The resulting definition, the ANSI standard or ANSIC was completed late
1988.

The programming language C was written down by Kernighan and Ritchie, in a now
classic book called “The C Programming Language” (Kernighan has said that he had no
part in the design of C language. It’s entirely Dennis Ritchie’s work, but he is the author
of the famous “HELLO WORLD” program & many other UNIX programs).

For years the book”The C programming Language 1st edition” was the standard in the
language C. In 1983 a committee was formed by American National Standard Institute
(ANSI) to develop a modern definition for the programming language C. In 1988 they
delivered the final standard definition ANSI C. Later on ANSI C standard was adopted by
the INTERNATIONAL STANDARD ORGANIZATION (ISO).
3 Graphics in C

3. Introduction to graphics
Computer graphics:
Computer graphics is one of the most powerful and interesting aspect of computers.
There are many things we can do in graphics apart from drawing figures of various
shapes. All videogames, animation, multimedia predominantly works using computer
graphics.

a. C Graphics:
C language is one of the famous language that support programs to generate drawings.
Computer Aided Design and Drafting (CADD). Computer Aided Engineering (CAE) and
multimedia are applications that generate drawings.

There are large numbers of functions in C which are used for putting pixel on a
graphic screen to form lines, shapes and patterns. Graphics programming is a very
crucial feature of C language. Graphics programming make the C language able to
produce image, create projects, generate animations and develop games etc. The header
file which is used to start a graphics program will be “graphics.h” i.e. if user want to
produce any graphical image on the output screen of C compiler then he has to firstly
introduce the “graphics.h” header file which contains all the definitions and
explanations of the graphics function that user needs in the graphics programming.

i) Graphics Drivers:

 To understand the concept of graphics driver we have to understand the concept


of device drivers.
 Device drivers are some small programs which directly communicate with the
hardware.
 We cannot communicate with any machine directly here are some small
programs which are needed to communicate with machines. These programs are
known as Device drivers.
 Graphics Drivers are the small parts of the device drivers and are applicable on
graphics mode.

ii) Graphics Mode:

 User has to switch over the graphics mode just before providing the graphical
images.
 Every graphics mode has a specific number. Out of all available modes in the
computer system one would like to switch over the graphics mode which offers
the best resolution.
4 Graphics in C

b. Setting Text Modes:


A text mode with a specific number of characters can be set. Some commonly used text
modes are:

Symbol Mode value Text mode


BW40 0 Black and white with 25 rows and 40 columns
C40 1 Color with 25 rows and 40 columns
BW80 2 Black and white with 25 rows and 80 columns
C80 3 Color with 25 rows and 80 columns
LASTMODE 1 Restore previous

i) textmode() Function:
textmode() function is used to set the number of rows and columns on the
monitor screen. It has the following form.
textmode(m); [where m is an integer]

e.g.- textmode; or textmode(C40);

Will set the text mode in color with 25 rows and 40 columns.
ii) textbackground() Function:
textbackground() function is used to set the background color in the text
mode. It has the following form:
textbackground(n); [where n in an integer referring background color]

e.g.- textbackground(1); or textbackground(BLUE); will set the background


color blue.

The following is the value list corresponding to colors:

Symbol Color Symbol Color value


BLACK 0 LIGHTBLUE 9
BLUE 1 LIGHTGREEN 10
GREEN 2 LIGHTCYAN 11
CYAN 3 LIGHTRED 12
RED 4 LIGHTMAGENTA 13
MAGENTA 5 YELLOW 14
BROWN 6 WHITE 15
LIGHTGRAY 7 BLINK 128
DARKGRAY 8

iii) textcolor() Function:


textcolor() function is used to set the color for the text going to be displayed.
It has the following form:
5 Graphics in C

textcolor(n); [n is a integer referring textcolor]

e.g.- textcolor(4); or textcolor(RED); will set the text color Red.


iv) gotoxy() Function:
gotoxy() function is used to move the cursor to any specific location on the
screen. It has the following form:
Gotoxy(x,y); [x & y are integers which refer the column & row positions
respectively]

e.g- gotoxy(20,10); will move the cursor to the 20th column in the 10th row.

v) clrscr() Function:

clrscr() function is used to clear the screen to display any new text on the
screen. It has the following form: clrscr();

vi) cputs Function():

cputs() function is used to display a text message in the monitor screen in the graphics

mode. It has the following form:

cputs(“text message”);

e.g.-cputs(“press any key”); will display the text message.

SETTING GRAPHIC MODES:


In order to display any picture in a monitor, the graphics mode is set for resolution and
color. All graphics function can be included in a program using #include<stdio.h>

i) initgraph() Function:

initgraph() function is used to set the graphics mode. It has the following form:

initgraph(graphic_driver,graphic_mode,driver_path);

while, graphic_driver is an integer or symbol corresponding to th monitor type. The


driver DETECT is used to detect the monitor type.

graphic_mode is also an integer corresponding to the monitor type which is set


automatically using DETECT drivers.

e.g.- int gd,gm;

gd=DETECT;

initgraph(&gd,&gm,””);

which initialize the gm with automatic detection & setting in the possible resolution of
the monitor type.
6 Graphics in C

ii) closegraph() Function:

closegraph() function is used to terminate the graphics mode which is set by the
initgraph() function. It restores the original video mode. It has the following form.

closegraph();

iii) getmaxx() Function:

getmaxx() function is used to get the maximum value of x-coordinate in the current
resolution.

e.g.- mx=getmaxx();

will return and assign the maximum value of x coordinate to the variable mx.

printf(“%d”,mx);

iv) getmaxy() Function:

getmaxy() function is used to get the maximum value of y-coordinate in the current
resolution.

e.g- my=getmaxy();

will return and assign the maximum value of y-coordinate to the variable my.

printf(“%d”,my);

v) restorecrtmode() Function:

restorecrtmode() function is also used to terminate the graphics mode and restore the
original video mode. getgraphmode() & set graphmode() functions can be used with
restorecrtmode() function to switch between graphics mode & original video mode. It
has the following form:

restorecrtmode();

vi) setcolor() Function:

setcolor() function is used to specify the color for a graphical picture going to be
displayed of the form; setcolor(m); [m represents color]

e.g.- setcolor(4); or setcolor(RED);

vii) setbkccolor() Function:

setbkcolor() function is used to set the background color for the monitor screen. It has
the following form:

setbkcolor(m); [m=integer represent background color]

e.g.- setbkcolor(1); or setbkcolor(BLUE);

viii) setpalatte() Function:


7 Graphics in C

setpalatte() Function is used to set the background color with the index.

e.g.- setpalatte(0,4); or setpalatte(0,RED);

viii) putpixel() Function:

putpixel() function is used to draw a point on the monitor screen in the given
coordinate position. It has the following form: putpixel(x,y,n);

x & y are the integers representing the coordinates

n is an integer referring color code of the pixel

e.g.- putpixel(100,150,4);

putpixel(100,150,RED);

ix) line() Function:

line() function is used to draw a line on the monitor screen in the given coordinate
position. It has the following form:

line(x1,y1,x2,y2);

x1 and y1 are the integers representing the starting coordinates

x2 and y2 are the ending coordinates

e.g.- line(80,50,300,200);

x) moveto() Function:

moveto() function is used to move the control from the current position to the specified
coordinate of the form:

moveto(x,y);

where x & y are new coordinate position

e.g.- moveto(200,300);

xi) lineto() Function:

lineto() function is used to draw a line from the current position to a specific coordinate.
It has the form:

lineto(x,y);

x & y are integers representing the ending point of the line.

e.g.-moveto(100,80);

lineto(100,250);

xii) linerel() Function:


8 Graphics in C

linerel() function is used to draw a line from the current position to a point by
specifying its distance relative to the current position. It has the form:

linerel(dx,dy);

dx & dy representing the horizontal & vertical distance.

e.g.- moveto(150,100);

linerel(200,0);

xiii) outtext() Function:

outtext() function is used to display a text message in the graphics mode at the current
position of the form:

outtext(“text message”);

xiv) outtextxy() Function:

outtextxy() function is used to display a text message in a specific location on the


monitor screen which is of the form:

outtextxy(m,n,”text message”);

m specifies position of x axis.

n specifies position of y axis.

e.g.- outtextxy(20,450,”Press any key to continue….”);

will display at a position 20 in x-axis and 450 in y-axis.

Some examples:
1) Program to draw a straight line in the given position.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

main()

int gd,gm,x1,x2,y1,y2;

gd=DETECT;
9 Graphics in C

clrscr();

printf(“\nEnter start point coordinate:”);

scanf(“%d%d”,&x1,&y1);

printf(“\nEnter end point coordinate:”);

scanf(“%d%d”,&x2,&y2);

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

setbkcolor(RED);

line(x1,y1,x2,y2);

outtextxy(20,450,”Press any key to continue….”);

getch();

closegraph();

}
10 Graphics in C

OUTPUT
:
11 Graphics in C

2) Program to draw a square with a given side.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

main()

int gd,gm,s;

gd=DETECT;

clrscr();

printf(“Enter side of square:”);

scanf(“%d”,&s);

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

setbkcolor(LIGHTGRAY);

setcolor(RED);

moveto(50,50);

lineto(50+s,50);

lineto(50+s,50+s);

lineto(50,50+s);

lineto(50,50);

outtextxy(20,450,”Press any key to continue…”);

getch();

closegraph();

}
12 Graphics in C

OUTPUT
13 Graphics in C

3) Program to draw an equilateral triangle with a given side.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

main()

Int gd,gm,a;

gd=DETECT;

clrscr();

printf(”Enter side of equilateral triangle:”);

scanf(“%d”,&a);

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

setbkcolor(BROWN);

setcolor(WHITE);

moveto(150,250);

linerel(a,0);

linerel(-a/2,-sqrt(3.0)/2*a);

lineto(150,250);

outtextxy(20,450,”Press any key to continue……..”);

getch();

closegraph();

[Here the linerel() function is used to draw the triangle by giving the relative distance of other
coordinates. The altitude of the triangle is calculated using (√3/2)a, and minus sign is used to
select the third corner above the bottom corners to complete a triangle]
14 Graphics in C
OUTPUT
15 Graphics in C

xv) rectangle() Function:

rectangle() function is used to draw a rectangle by specifying the diagonal coordinates.


It has the following form:

rectangle(x1,y1,x2,y2);

x1 & y1 represents the top left coordinates.

X2 & y2 represents the bottom right coordinates.

e.g.- rectangle(100,80,300,200);
16 Graphics in C

4) Program to draw a rectangle with the given sides.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

main()

Int gd,gm,x1,x2,y1,y2;

gd=DETECT;

clrscr();

printf(“\nEnter left top coordinates:”);

scanf(“%d%d”,&x1,&y1);

printf(“\nEnter right bottom coordinates:”);

scanf(“%d%d”,&x2,&y2);

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

setbkcolor(WHITE);

setcolor(BLUE);

rectangle(x1,y1,x2,y2);

outtextxy(20,450,”Press any key to continue….”);

getch();

closegraph();

}
17 Graphics in C

OUTPUT
18 Graphics in C

xvi) circle() Function:

circle() function is used to draw a circle specifying its centre and radius. It has the form:

circle(x,y,r);

x & y are integers representing the centre of the circle.

r is an integer representing the radius of the circle.

xvii) arc() Function:

arc() function is used to draw a circular arc by specifying the centre, radius and starting
& ending angles. It has the following form:

arc(x,y,sa,ea,r);

where, x & y are integers representing the centre of the arc.

sa is an integer representing starting angle of the arc.

ea is an integer representing ending angle of the arc.

r is an integer representing the radius of the arc.

xviii) ellipse() Function:

ellipse() function is used to draw an ellipse by specifying the centre, radius, start & end
angles and the semi minor axes of the ellipse of the form:

ellipse(x,y,sa,ea,xr,yr);

where,

x & y represents centre of the ellipse

sa represents starting angle of the ellipse

ea represents ending angle of the ellipse

xr represents semi-major axis of the ellipse

yr represents semi-minor axis of the ellipse


19 Graphics in C

5) Program to draw a circle of given radius.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm,x1,y1,r;

clrscr();

printf(“Enter the centre coordinates:”);

scanf(“%d%d”,&x1,&y1);

printf(“\n\nEnter the radius:”);

scanf(“%d”,&r);

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

setbkcolor(WHITE);

setcolor(RED);

circle(x1,y1,r);

outtextxy(115,50,”CIRCLE”);

outtextxy(20,450,”Press any key to continue…..”);

getch();

closegraph();

}
20 Graphics in C

OUTPUT
21 Graphics in C

6) Program to draw an ellipse.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

main()

Int gd=DETECT,gm,x1,y1,angle1,angle2,xr,yr;

clrscr();

printf(“\nEnter the coordinates:”);

scanf(“%d%d”,&x1,&y1);

printf(“\nEnter the semi-major axis:”);

scanf(“%d”,xr);

printf(“\nEnter the semi minor axis:”);

scanf(“%d”,&yr);

angle1=0;

angle2=360;

initgraph(&gd,&gm,”C:TURBOC3\\BGI”);

setbkcolor(WHITE);

setcolor(BROWN);

ellipse(x1,y1,angle1,angle2,xr,yr);

outtextxy(20,450,”Press any key to continue….”);

getch();

closegraph();

}
22 Graphics in C

OUTPUT
23 Graphics in C

7) Program to draw a circle inside the square.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

main()

int gd=DETECT,gm,x1,x2,y1,y2;

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

setbkcolor(WHITE);

setcolor(RED);

rectangle(50,60,200,210);

circle(125,135,30);

outtextxy(20,450,”Press any key to continue……”);

getch();

closegraph();

}
24 Graphics in C

OUTPUT
25 Graphics in C

8) Program to draw the graph of sinx.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

int main()

int gd=DETECT,gm,angle=0;

double x,y;

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
/*generate a sine wave*/

for(x=0;x<getmaxx();x=x+3)

{
/*calculate y value given to x*/

y=50*sin(angle*3.141/180);

y=getmaxy()/2-y;

putpixel(x,y,15); /*color a pixel at the given position*/

delay(100);

angle=angle+5; /*increment angle*/

outtextxy(20,450,”Graph of sin(x)”);

getch();

closegraph(); /*deallocate memory allocated for graphics screen*/

return0;

}
26 Graphics in C
OUTPUT
27 Graphics in C

4. Conclusion

From the above programs we came to know about the basics of graphics in a
computer language. The C language helps us to get started with some small
graphics programs such as drawing lines, squares, graphs etc. Here the main part
of the graphics programs is the library function”graphics.h” (which is the header
file for a graphics program).
We've seen the #include and main function before; main must appear in every
program you intend to run, and the #include gives us access to printf (as well as
scanf). (As you might have guessed, the io in stdio.h stands for "input/output";
std just stands for "standard.") The keyword int declares this is a number to be
an integer. The “math.h” is a library function that deals with all the mathematical
functions.
28 Graphics in C

5. References

 A First Course In Programming With C by T Jeyapoovan, VIKAS®


PUBLISHING HOUSE PVT LTD,
 Programming in ANSI by E.Balagurusamy, Sixth Edition, McGraw Hill
Education Private Limited
 Programming in C (3rd Edition) by Stephen Kochan, Paperback: 576 pages,
Publisher: Sams; 3 edition (July 8, 2004)
 The C Programming by Language by Brian W. Kernighan & Dennis Ritchie
Paperback: 274 pages, Publisher: Prentice Hall PTR; 2 edition (March 22,
1988)
 The C++ Programming Language (Special 3rd Edition), Publisher: Addison-
Wesley Professional; 3 edition (February 15, 2000)
 Google

You might also like