C Graphics
C Graphics
C Graphics
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.
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
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:
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
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]
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- 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();
cputs() function is used to display a text message in the monitor screen in the graphics
cputs(“text message”);
i) initgraph() Function:
initgraph() function is used to set the graphics mode. It has the following form:
initgraph(graphic_driver,graphic_mode,driver_path);
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
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();
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);
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();
setcolor() function is used to specify the color for a graphical picture going to be
displayed of the form; setcolor(m); [m represents color]
setbkcolor() function is used to set the background color for the monitor screen. It has
the following form:
setpalatte() Function is used to set the background color with the index.
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);
e.g.- putpixel(100,150,4);
putpixel(100,150,RED);
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);
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);
e.g.- moveto(200,300);
lineto() function is used to draw a line from the current position to a specific coordinate.
It has the form:
lineto(x,y);
e.g.-moveto(100,80);
lineto(100,250);
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);
e.g.- moveto(150,100);
linerel(200,0);
outtext() function is used to display a text message in the graphics mode at the current
position of the form:
outtext(“text message”);
outtextxy(m,n,”text message”);
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();
scanf(“%d%d”,&x1,&y1);
scanf(“%d%d”,&x2,&y2);
initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);
setbkcolor(RED);
line(x1,y1,x2,y2);
getch();
closegraph();
}
10 Graphics in C
OUTPUT
:
11 Graphics in C
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
int gd,gm,s;
gd=DETECT;
clrscr();
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);
getch();
closegraph();
}
12 Graphics in C
OUTPUT
13 Graphics in C
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
main()
Int gd,gm,a;
gd=DETECT;
clrscr();
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);
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
rectangle(x1,y1,x2,y2);
e.g.- rectangle(100,80,300,200);
16 Graphics in C
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
Int gd,gm,x1,x2,y1,y2;
gd=DETECT;
clrscr();
scanf(“%d%d”,&x1,&y1);
scanf(“%d%d”,&x2,&y2);
initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);
setbkcolor(WHITE);
setcolor(BLUE);
rectangle(x1,y1,x2,y2);
getch();
closegraph();
}
17 Graphics in C
OUTPUT
18 Graphics in C
circle() function is used to draw a circle specifying its centre and radius. It has the form:
circle(x,y,r);
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);
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,
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm,x1,y1,r;
clrscr();
scanf(“%d%d”,&x1,&y1);
scanf(“%d”,&r);
initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);
setbkcolor(WHITE);
setcolor(RED);
circle(x1,y1,r);
outtextxy(115,50,”CIRCLE”);
getch();
closegraph();
}
20 Graphics in C
OUTPUT
21 Graphics in C
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
Int gd=DETECT,gm,x1,y1,angle1,angle2,xr,yr;
clrscr();
scanf(“%d%d”,&x1,&y1);
scanf(“%d”,xr);
scanf(“%d”,&yr);
angle1=0;
angle2=360;
initgraph(&gd,&gm,”C:TURBOC3\\BGI”);
setbkcolor(WHITE);
setcolor(BROWN);
ellipse(x1,y1,angle1,angle2,xr,yr);
getch();
closegraph();
}
22 Graphics in C
OUTPUT
23 Graphics in C
#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);
getch();
closegraph();
}
24 Graphics in C
OUTPUT
25 Graphics in C
#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;
delay(100);
outtextxy(20,450,”Graph of sin(x)”);
getch();
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