Electrofriends

Turbo C graphics programming

by Ranjith | November 20th, 2008.

Circle, arc, pieslice are declared as follows:

Declaration:

*  void far arc(int x, int y, int stangle, int endangle, int radius);
*  void far circle(int x, int y, int radius);
*  void far pieslice(int x, int y, int stangle, int endangle, int radius);

Remarks:

* arc draws a circular arc in the current drawing color.
* circle draws a circle in the current drawing color.
* pieslice draws a pie slice in the current drawing color, then fills it using
the current fill pattern and fill color.

Arguments:

* (x,y): Center point of arc, circlew, or pie slice
* stangle: Start angle in degrees
* endangle: End angle in degrees
* radius: Radius of arc, circle, and pieslice

Here, stangle and endangle are in degrees starting from the +ve x-axis in the polar coordinate system in the anti-clockwise direction. if stangle is 0, endangle is 360, it will draw a full circle. Refer this figure for clear idea: For the details of current color, fill color and fill patterns, refer the sections Lines and Colors.

Angle calculations

Angle calculations

Another basic shape that we come across is a rectangle. To draw a border, use rectangle with the coordinates of outline, to draw a square use rectangle with same height and width. drawpoly() and fillpoly() are two functions useful to draw any polygons. To use these functions, store coordinates of the shape in an array and pass the address of array as an argument to the function. By looking at the output of the previous program, you can understand what drawpoly is. fillpoly is similar except that it fills in the shape with current fill color.

Declaration:

*   void far rectangle(int left, int top, int right, int bottom);
*   void far drawpoly(int numpoints, int far *polypoints);
*   void far fillpoly(int numpoints, int far *polypoints);

Remarks:

*  Rectangle draws a rectangle in the current line style, thickness, and drawing color.
*  Drawpoly draws a polygon using the current line style and color.
*  Fillpoly draws the outline of a polygon using the current line style and color, then fills the polygon using the current fill pattern and fill color.

Arguments:

*  (left,top) is the upper left corner of the rectangle, and (right,bottom) is its lower right corner.
*  numpoints:  Specifies number of points
*  Polypoints: Points to a sequence of (numpoints x 2) integers. Each pair of integers gives the x and y coordinates of a point on the polygon.

To draw a closed polygon with N points, numpoints should be N+1 and the array polypoints[] should contain 2(N+1) integers with first 2 integers equal to last 2 integers.

Let us study more about shapes latter. Here is some idea about colors. There are 16 colors declared in graphics.h as listed bellow.

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

To use these colors, use functions setcolor(), setbkcolor() and setfillstyle(). setcolor() function sets the current drawing color. If we use setcolor(RED); and draw any shape, line or text after that, the drawing will be in red color. You can either use color as defined above or number like setcolor(4);. setbkcolor() sets background color for drawing. Setfillstyle sets fill pattern and fill colors. After calling setfillstyle, if we use functions like floodfill, fillpoly, bar etc, shpes will be filled with fill color and pattern set using setfillstyle. These

function declarations are as follows.
Declaration:

*  void far setfillstyle(int pattern, int color);
*  void far setcolor(int color);
*  void far setbkcolor(int color);

Remarks:

* setfillstyle sets the current fill pattern and fill color.
* setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
* setbkcolor sets the background to the color specified by color.

The parameter pattern in setfillstyle is as follows:

Parameter pattern in setfillstyle

Parameter pattern in setfillstyle

Here is an example program with colors, pixels, bar, cleardevice etc. stdlib.h is used for random number generation. We have a function random(no), it returns a random number between 0 an no. The effect is by drawing random radius, random color circles with same center and random pixels. kbhit() function(defined in conio.h) returns a nonzero value when a key is pressed in the keyboard. So, the loop will continue until a key is pressed.

/*
random.c
some graphics effects using random numbers.
example 1.2
by HarshaPerla, http://eharsha.tk
*/

#include “graphics.h”
#include “conio.h”
#include “stdlib.h”

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

initgraph(&gd, &gm, “”);
setcolor(3);
setfillstyle(SOLID_FILL,RED);
bar(50, 50, 590, 430);

setfillstyle(1, 14);
bar(100, 100, 540, 380);

while(!kbhit())
{
putpixel(random(439)+101,  random(279)+101,random(16));
setcolor(random(16));
circle(320,240,random(100));
}
getch();
closegraph();
}

Pages: 1 2

Share and Enjoy:
  • Digg
  • Technorati
  • StumbleUpon
  • TwitThis
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Mixx
  • Yahoo! Buzz
  • LinkedIn
  • MySpace
  • FriendFeed
  • NewsVine
  • Netvibes
Similar Posts:

23 Responses to Turbo C graphics programming

  1. gokul

    please send image compression program using C.

  2. shabeeb ahamed

    when i run the followed program, i have got error msg.

    program
    /* simple.c
    example 1.0
    */
    #include
    #include

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

    initgraph(&gd, &gm, “c:\\turboc3\\bgi ” );
    circle(200,100,150);

    getch();
    closegraph();
    }

    error
    1.undefined symbol _closegraph
    2.undefined symbol _circle
    3.undefined symbol _initgraph.

    and also i cant run any graphic program successfully. these types of errors are showing.

    plz help me to solve this problem.

  3. shabeeb ahamed

    sry, idnt know about image compression program.

  4. bhuvan

    i wanna program which detects circle which is actually read from paint

  5. chaitanya kuamar

    shabeeb ahamed ,just you try this one once.

    #include
    #include

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

    initgraph(&gd, &gm, “” );
    circle(200,100,150);

    getch();
    closegraph();
    }

  6. shahan

    please tell me about the turbo c graphics text explicitly

  7. Sim

    dis was pretty useful… but i have this project to implement the dijkstra’s algorithm for directed graphs and we were required to draw the graph in c. i have this problem in making the arrows for the directed edges as well as placing the edges witout criss crossing

  8. Saranya

    Can any one tell me how to draw rounded rectangle and sphere?

  9. rachit

    i need a program to draw all d figures present in car digital display… like indicators, petrol sign,etc.. can u help me in dis..

  10. Ronnel S. Lim

    Can anyone of you that could help me for the fulfilment of my requirement? I need the program that output like this..

    describing figure
    *it is any polygon figure that contain with string on it.
    *it is combination color with shadow…

    I highly expected your kindness……

  11. vennila.p

    how to correct the error -undefined symbol initgraph(),closegraph()?

  12. mj

    hi!! can i have a sample program for SWITCH CASE..thankS!!!

  13. mj

    you can send it to my email..michaeljordanl@yahoo.com!! thanks..

  14. megha

    I need coding of visual bubble sort in C using graphics

  15. Naresh Kumar

    Can any please give me a program of graph with respect to x and y

    in which we can take the input of x and y and the graph is plotted

    please send me as soon as possible

    nareshkumar326@hotmail.com

    thanking u in anticipation.

  16. maze

    can you help us…?????
    what is the reason why when i run the program that have a graphics…
    the message is only “watch”

  17. maze

    is there a connection in the monitor why turbo c that have graphic cant run….?????

    please badly needed……

    just send 2 my mail

    mazieamolo@yahoo.com

    tnx

  18. nandu

    need some other graphical codes like marquee

  19. Ranjeet Singh

    those who are receiving the message
    linking error
    1.undefined symbol _closegraph
    2.undefined symbol _circle
    3.undefined symbol _initgraph.

    they are needed change some turobo c ide settings as
    1 goto “option” menu of the menu bar
    2 next to linker sub menu
    3 and then to Libraries
    3 put a [X] on graphics library

    and the program would work perfectly fine

  20. shray

    could any 1 tell me how to get to know wat address is to be specified in initgraph declaration i.e. like”c:\\tc\\bgi” my compiler cant include graphics.h. wher as i have graphics .h installed.

  21. vinay

    @shray
    just try this
    in the bgi folder in tc ,copy a file named egavga.bgi
    and paste it in the bin folder ( from where we start c++)
    then u no longer need to put any path in the initgraph function.

  22. aswathy sarath

    pleasa include syntax of each functions

  23. Aakash

    hi 2 all…
    i have some doubt…

    m working in graphical mode…

    in my program i need to clear screen again and again in graphical mode only thats y i cant use closegraph() again and again

    and if i use clrscr() then the page become white..

    so can any1 tell me that how can i clear my screen….

    most probably there is some function for this can any1 tell me that function name or can help me in any way…

    m very thankful 2 u;;

    with regardsss

Leave a Reply

Copyright©2009 www.electrofriends.com All Rights Reserved. Powered by Dhyeya