DEV Community

AryanKoundal
AryanKoundal

Posted on

Computer Graphics | Program to Draw Graphics Objects using built-in C++ functions

By the end of this blog you will be able to draw Pixels, Lines, Circles, Rectangles and Ellipses. To get started with Computer Graphics using C++, you have to import the graphics library.



// Graphics Library for C++
#include <graphics.h>


Enter fullscreen mode Exit fullscreen mode

After this. you can start the methods/functions available in this graphics library.
Inside main() function before using the functions for all those objects you have to initialize the Graphics mode on your computer. This is necessary step, so that you can change your computer display mode to generate image.



        // gm is Graphics mode, a computer display mode to
    // generate image
    // DETECT is a macro defined in "graphics.h" header file
    int gd = DETECT, gm;

    // initgraph initializes the graphics system by loading a
    // graphics driver from disk, path can be empty
    initgraph(&gd, &gm, "");


Enter fullscreen mode Exit fullscreen mode

This completes your setup for using any of the methods listed in graphics library. Let’s draw a pixel first.



        // putpixel(x,y,color): plots a pixel at location (x, y)
    // of specified color
    putpixel(50, 100, YELLOW);


Enter fullscreen mode Exit fullscreen mode

This will create a new window on your screen on which a pixel a drawn. In addition to it, you can also print a string on the screen using,



// outtextxy(x,y,"string"):  displays the text or
    // string at a specified point (x, y) on the screen
    outtextxy(35, 55, "PIXEL");


Enter fullscreen mode Exit fullscreen mode

Image description

For drawing a line along a string “LINE” above it, you’ll need,



        // line(x1, y1, x2, y2):  draws a line from a point(x1,y1) to point(x2,y2)
    line(120, 90, 170, 170);
    outtextxy(130, 55, "LINE");


Enter fullscreen mode Exit fullscreen mode

Image description

For drawing a circle along a string “CIRCLE” above it, you’ll need,



        // circle(x,y,r): Draws a circle having center at x,y and having radius as r
    circle(240, 120, 40);
    outtextxy(215, 55, "CIRCLE");


Enter fullscreen mode Exit fullscreen mode

Image description

For drawing a rectangle along a string “RECTANGLE” above it, you’ll need,



        // rectangle(left, top, right, bottom): Draws a rectangle
    // having coordinates of left top and right bottom corner
    rectangle(300, 90, 400, 140);
    outtextxy(310, 55, "RECTANGLE");


Enter fullscreen mode Exit fullscreen mode

Image description

For drawing a ellipse along a string “ELLIPSE” above it, you’ll need,



        // ellipse(xCenter, yCenter, startAngle, endAngle, xRadius, yRadius):
    // Draws an ellipse for a given center, starting and ending angle
    // and horizontal and vertical radius.
    ellipse(500, 120, 0, 360, 70, 35);
    outtextxy(470, 55, "ELLIPSE");


Enter fullscreen mode Exit fullscreen mode

Image description

After these function calls, you will need two additional function calls. First one is getch() and another one is closegraph()



        // getch() is a nonstandard function and is present in
    // conio.h header file
    // it pauses the Output Console until a key is pressed
    getch();

    // closegraph function closes the graphics
    // mode and deallocates all memory allocated
    // by graphics system .
    closegraph();


Enter fullscreen mode Exit fullscreen mode

That’s all you need to draw these objects. The complete code is below,



#include <iostream>
// Graphics Library for C++
#include <graphics.h>
#include <conio.h>
#include <dos.h>
using namespace std;
int main()
{   
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    putpixel(50, 100, YELLOW);
    outtextxy(35, 55, "PIXEL");

    line(120, 90, 170, 170);
    outtextxy(130, 55, "LINE");

    circle(240, 120, 40);
    outtextxy(215, 55, "CIRCLE");

    rectangle(300, 90, 400, 140);
    outtextxy(310, 55, "RECTANGLE");

    ellipse(500, 120, 0, 360, 70, 35);
    outtextxy(470, 55, "ELLIPSE");
    getch();
    closegraph();

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

The output produced will be:

Image description

Top comments (4)

Collapse
 
sfleroy profile image
Leroy

Graphics is not a standard c++ feature. It's a windows only feature :) so this c++ only works on windows machines

Collapse
 
aryankoundal profile image
AryanKoundal

True, Graphics doesn't. Even in windows we manually have to add the code in the include section of the compiler plus a few steps more(link to setup - geeksforgeeks.org/include-graphics...) to get the code running.

Collapse
 
evasharma12 profile image
evasharma12

Very well explained🙌.

Collapse
 
oramthe69th profile image
Nikolas

How do you fill up the object and how would you output it?