Sunteți pe pagina 1din 5

Turbo C++ graphics Part 2

In first part of the turbo c++ graphics programming series,I explained about the graphics modes &
standard library functions,drivers. If you haven’t read the first part, please go do that now.The
first part describes the details on what you need to start the graphics programming using turbo C++.
In short, you need Turbo C++ version 3.0 to run the program explained in this post.

Drawing functions:
There are many functions available to draw the shapes on the screen; it’s not possible for me to
cover each & every function used in graphics programming so here i have explained the most
widely used & effective functions, the efficient use of them allows you to create smooth graphics.

rectangle (left,top,right,bottom) :
This function draws a rectangle with (left,top) as upper left of the rectangle & (right,bottom) as its
lower right of the corner.All you have to do is to put the right co-ordinates.
e.g. rectangle (10,30,500,400);

ellipse (x,y,stangle,endangle,xrad,yrad) :
This function draws an elliptical arc.Here (x,y) are the co-ordinates of center of the ellipse.
(stangle,endangle) are the starting and ending angles.If stangle=0 and endangle=360 then this
function draws complete ellipse.
e.g.ellipse(100,150,0,360,100,50);

arc (x,y,stangle,endangle,rad) :
This function draws the circular arc of the given color.(x,y) are the center point of the arc & arc
travels from stangle to endangle.(rad) defines the radius of the arc.
e.g. arc(120,160,300,90,70);

line (x1,y1,x2,y2) :
Line function draws a line between two specified points (x,y) towards (x2,y2).This function comes
handy if you want to draw box like shapes or just plotting the graphs etc.
e.g. line(100,50,100,400);

You can set the line style using setlinestyle functions.This function specifies the type of line,pattern
& the thickness that is going to appear on the screen.You have options like
solid,dotted,centered,dashed etc.
e.g. setlinestyle(style,0,1);

putpixel(x,y,color) :
This function is used to put apixel at specified points (x,y).It comes handy when we have to plot a
point of specified color at desired location.(color) can be defined in function as “white” or “BLUE”
or specify the color code.
e.g. putpixel(100,150,WHITE);

bar (left,top,right,bottom):
This function draws the filled-in,rectangular,two-dimmensional bar.It is filled using the fill pattern
and fill color.

Color palettes
The graphics.h has declaration about 16 colors.In order to use the color in your program you have to
use the functions like setcolor( ) ,setbkcolor( ) & setfillstyle( ).The function setcolor( ) sets the
value of current drawing color to color.setfillstyle( ) sets the current fill pattern and fill
color.setbkcolor( ) sets the value for background color,which is by default black.

Below is the table that describes the value for each color that are declared in the graphics.h file.

Color Value
Black 0
Blue 1
GREEN 2
Cyan 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15

Here is an example that makes use of all the available functions explained above, so write the
code,compile and observe the effects.

#include"graphics.h
#include"conio.h"

void main()
{ int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
circle(330,180,100);
rectangle (10,30,500,400);
ellipse(100,150,0,360,100,50);
arc(120,160,300,90,70);
line(100,50,100,400);
getch(); closegraph();
restorecrtmode ();
}

By executing the functions in this program you will observe the circle, rectangle, ellipse, arc & line
on the screen. I hope I have covered decent amount of information regarding the basics of the
graphics programming using Turbo C++. I hope above information helped, again any suggestions
are welcome.

Turbo C graphics Programming


Harsha
To start with graphics programming, Turbo C is a good choice. Even though DOS has its own limitations, it
is having a large number of useful functions and is easy to program. To implement graphics algorithms, To give
graphical display of statistics, To view signals from any source, we can use C graphics. Here is a article to start
programming with Turbo C. 'Run and Learn' is our method. We have used source codes throughout the explanations.
Just execute them to understand what is happening.

Visit Downloads page for free source codes of graphics programs.

Turbo C has a good collection of graphics libraries. If you know the basics of C, you can easily
learn graphics programming. To start programming, let us write a small program that displays a circle on
the screen.

/* simple.c
example 1.0
*/
#include<graphics.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm;

initgraph(&gd, &gm, "c:\\turboc3\\bgi " );


circle(200,100,150);

getch();
closegraph();
}

To run this program, you need graphics.h header file, graphics.lib library file and Graphics driver (BGI
file) in the program folder. These files are part of Turbo C package. In all our programs we used 640x480
VGA monitor. So all the programs are according to that specification. You need to make necessary
changes to your programs according to your screen resolution. For VGA monitor, graphics driver used is
EGAVGA.BGI.

Here, initgraph() function initializes the graphics mode and clears the screen. We will study the
difference between text mode and graphics mode in detail latter.

InitGraph:

Initializes the graphics system.

Declaration:
void far initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Remarks: To start the graphics system, you must first call initgraph.

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then

S-ar putea să vă placă și