Turbo C++ graphics Part 2

January 22, 2007 · By mahesh 

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.

 

 

Comments

13 Responses to “Turbo C++ graphics Part 2”

  1. kin on March 29th, 2007 7:07 am

    thanxs 4 ts information…it helped me alot

  2. vinay on May 29th, 2007 1:52 pm

    thanx man….

  3. Edna Encia on July 6th, 2007 9:53 am

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

  4. K.R.Nekhelesh on July 7th, 2007 7:07 am

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

  5. Creating standalone graphics programs for Turbo C++ on July 20th, 2007 8:41 am

    [...] 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 [...]

  6. Faizal on July 25th, 2007 5:31 pm

    Thanks for tutorial

  7. hari krishna on September 9th, 2007 11:53 am

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

  8. prateek on November 24th, 2007 5:28 pm

    good website

  9. Learning Turbo C++ on January 5th, 2008 10:40 am

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

  10. Afaash on January 8th, 2008 5:01 pm

    Thanks a lot. It has helped me greatly

  11. nitu on January 12th, 2008 11:10 am

    this was of gr8 help .thax a lot

  12. gelbert05 on January 13th, 2008 5:36 pm

    tnx people, the info here help me…

  13. Mumtaz Ahmad on January 28th, 2008 5:46 am

    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.