Skip to content


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.

 

 


13 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. kin says

    thanxs 4 ts information…it helped me alot

  2. vinay says

    thanx man….

  3. Edna Encia says

    Thanx for sharing every knowledge its give to student wide understanding in thier cited topics……thanx….

  4. K.R.Nekhelesh says

    This information was very useful. but can you proceed with more functions….?

  5. Faizal says

    Thanks for tutorial

  6. hari krishna says

    thanx for the info……
    i could use graphics in c because of you only..

  7. prateek says

    good website

  8. Afaash says

    Thanks a lot. It has helped me greatly

  9. nitu says

    this was of gr8 help .thax a lot

  10. gelbert05 says

    tnx people, the info here help me…

  11. Mumtaz Ahmad says

    this is highly informative site to learn graphic in c++ i got a lot of information by this and i will also suggest to my friends to visit this site.

Continuing the Discussion

  1. Creating standalone graphics programs for Turbo C++ linked to this post on July 20, 2007

    [...] have already writtern some Turbo c++ graphics articles in two part series (1 & 2)this article will cover it up as creating stand alone for the programs we have written in previous [...]

  2. Learning Turbo C++ linked to this post on January 5, 2008

    [...] C++ graphics programming Turbo C++ graphics functions Turbo C++ Sound programming Turbo C++ standalone graphics programs  Turbo explorer [...]