Graphics in C++


  • Graphics provide a visual way to see objects in action. Turbo C++ graphics functions fall into two categories:
  • those that work in the text mode
  • those that work in the graphics mode
  • The text mode graphic functions are concerned with placing text in certain areas of the screen.
  • The graphics-mode functions allow you to draw dots, lines, and shapes (like circles, rectangles, and ellipse etc.), add color to lines and areas, and perform many other graphics-related activities

 Text - Mode Graphics Functions

  • The text mode graphics functions need no preparation before use. You can start using the functions without any prior initialization.
  • window ( ) : This function takes four integer arguments that determine the left, top, right, bottom coordinates of the window. Text written by certain functions will appear inside the window, starting at the top left corner.
  • Text too long for the width is automatically wrapped at the right edge. Text that goes beyond the bottom of the window causes the contents of the window to scroll upward. Syntax: window (int left, int top, int right, int bottom);
  • This function does not draw a border around the window, it only confines text to a rectangular area. So, you cannot see the window unless you start typing in, the default coordinates for this function are the entire screen.
  • cputs( ) This function writes a string of text to a window. Syntax: cputs (string);
  • clrscr( ) This function erases the text window. When a window is not initialized, this function can be used to erase the entire screen. Syntax: clrscr( );
  • gotoxy( ) This library function positions the cursor within a text window.
  • Since text is usually written starting at the cursor position, this allows us to position text where you want it. Syntax: gotoxy (int x, int y);
  • putch( ) This library function displays a single character at the cursor position. Syntax: putch(char ch);
  • Its parameter is the character to be displayed.

Graphics - Mode Graphics Functions

  • In graphics mode the basic element is a pixel. Pixel is an abbreviation for picture element.
  • The number of pixels (resolution) on the screen depends on the hardware.
  • Each pixel displays a single dot on the screen.
  • Any pixel can be suppressed or illuminated.
  • Graphics-mode programs use a completely different set of functions from text-mode programs to put images on the screen.
  • A graphics program must also perform some preliminary work to set the appropriate graphics mode.

Setting up Graphics Mode

  • detectgraph() : This function checks the system and returns two integer parameters: a value representing the system's graphics driver and a value for the recommended graphics mode if an adapter is installed.
  • If a negative driver value is returned, it indicates the absence of a graphics adapter.
  • The mode value is the highest resolution possible with that adapter. Syntax: detectgraph(int *driver, int *mode);
  • Example: Illustrates detectgraph Function # include
    # include
    main()
    {
    int g_dr, g_mode;
    detectgraph(&g_dr,&g_mode);
    if (g_dr < 0) cout << "No graphics Adapter\n";
    }

  • The function detectgraph can be called directly or indirectly. The function given above makes a direct call to the function. The constant DETECT makes an indirect call to the function. The above program could be rewritten as
  • initgraph (): This library function must be executed before any other graphics mode functions can be used.
  • This requires the GRAPHICS.H header file. Syntax: initgraph(int *driver, int *mode, char *path);
  • where *driver is the graphics driver, given by an integer.
  • *mode is the graphics mode, given by an integer.
  • *path is a string indicating where the driver is available
  • note:The Driver is a file extention BGI.Each type of hardware requires a different driver.A null string ("") indicates sriver is in the current directory.

Position Functions

  • getmaxx() and getmaxy() These functions return the maximum pixels in each row (number of columns) and each column (number of rows) of the screen. The values depend on the resolution of the screen. On a 640 X 200 resolution screen, getmaxx() returns 639 and getmaxy() returns 199. Syntax:
  • int getmaxx();
  • int getmaxy();
  • getx() and gety():The functions getx() and gety() return the current x and y coordinates. Syntax:
  • int getx();
  • int gety();

Drawing Shapes

  • The functions described below helps us to draw different shapes like line, circle, etc. on the screen.
  • line() This function is used to draw a line, it requires the starting coordinates and the ending coordinates of the line. Syntax: line(start_col, start_row, end_col, end_row)
  • Circle() This function requires three parameters - the x and the y values of the center and the radius to draws a circle. Syntax: circle(center_col, center_row, radius);
  • Arc().This function draws a circular arc using that specified center, start and end angles and radius. Syntax: arc(column, row, st_angle, end_angle, radius);
  • ellipse() This function draws an ellipse or an elliptical arc using the specified center, start and end angles and horizontal and vertical radius. Syntax: ellipse(cen_col, cen_row, st_angle, end_angle, hor_rad, ver_rad);

Miscellaneous Functions

  • setcolor() This function sets the current drawing color. Syntax: setcolor(int color);
  • Setlinestyle() This sets the line width and style for drawing subsequent lines. Syntax: setlinestyle(linestyle, upattern, thickness); where linestyle could be any one of the values in
  • Values of Linestyle for Setlinestyle function

    Constant Value Draws a
    Solid_Line 0 solid line
    Dotted_Line 1 dotted line
    Center_Line 2 center line
    DASHED_LINE 3 line of hyphenes
    Userbit_Line 4 user defined style
  • upattern
    Should contain a sequence of on and off bits representing a pattern for drawing the line. If linestyle is not USERBIT_LINE the value of upattern is ignored.
  • Thickness
    Could be NORM_WIDTH, value 1 (normal width) or THICK_WIDTH, value 3 (three times as thick).

Filling Enclosed Regions

  • Following functions are useful for filling the shapes which have been just discussed.
  • floodfill() This function fills a bounded region with colour.
    Syntax: floodfill(column, row, border);
  • setfillstyle() This function sets the fill pattern and color.
    Syntax: setfillstyle(pattern, color);
  • Values for Patterns of setfillstyle function

    Constant Pattern Value
    EMPTY_FILL Background Colour 0
    SOLID_FILL Foreground Colour 1
    LINE_FILL Horizontal Lines 2
    LTSLASH_FILL /// Pattern(Thin Lines) 3
    SLASH_FILL /// Pattern(Thick Lines) 4
    BKSLASH_FILL \\\ Pattern(Thin Lines) 5
    LTBKSLASH_FILL \\\ Pattern(Thick Lines) 6
    HATCH_FILL Light cross Hatch 7
    XHATCH_FILL Heavy cross Hatch 8
    INTERLEAVE_FILL Interleaving Lines 9
    WIDE_DOT_FILL Sparse Dots 10
    CLOSE_DOT_FILL Densely Packed Dots 11
    USER_FILL User Defined Pattern 12

Cursor Control

  • moveto() This function moves the cursor to the specified location in the graphics screen.
    Syntax: moveto(column, row);
  • Text Output:
  • outtext( )and outtextxy() Both these functions are used to display text on the screen in the graphics mode. The function outtext() displays the text at current cursor position while the outtextxy() function positions the cursor and displays the given text.
    Syntax: outtext(text_string);
  • Settextstylel() This function is used to specify the font, direction and character size to be used for subsequent text output in graphics mode.
    Syntax: settextstyle(font, direction, char_size);
    Where font can be any of the values from Table. All these fonts are from special files, with extension .CHR, during run time.

  • Valid Font Values

    Constant Value Displays
    DEFAULT_FONT 0 Letters in default font
    TRIPLEX_FONT 1 Stroked triplex letters
    SMALL_FONT 2 Stroked small letters
    SANS_SERIF_FONT 3 Stroked sans serif letters
    GOTHIC_FONT 4 Stroked gothic letters
  • direction can be any of the values from the table.

  • Values for Direction

    Constant Value Displays text
    HORIZ_DIR 0 Horizontally, left aligned
    VERT_DIR 1 Vertically, right aligned
  • char_size
    magnifies all stroked letters specified no of times (unless it is 0) i.e. if char_size is 2, magnification is 2 times. Maximum magnification is 10.
  • getpixel() and putpixel()
    The getpixel() function returns an integer to represent the color of the pixel at the co-ordinates specified. Syntax: getpixel(column, row);
  • Clearing Graphics Screen
    cleardevice(): This function clears the graphics screen and positions the cursor at 0, 0.
    Syntax cleardevice();
  • Ending Graphics Mode
    closegraph()
    This function must be used when no more graphics routines are to be used.
  • It releases the memory reserved by graphics system and restores the screen to the video mode that was active before initgraph()
    Syntax closegraph();

some graphic functions

Here are some graphics programs

Exception Handling<< Previous
Next >> Stream

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus




Footer1