Computer Graphics | Program to Draw Graphics Objects using built-in C++ functions
Published: 2023-02-23 14:40:44

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.

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

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.

```c
		// 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, "");
```

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

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

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,

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iaa4zivkwkuf33ksdx3p.png)



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

```c
		// 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");
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82549r8fxm6osctm82p9.png)



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

```c
		// 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");
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epgmip2ocrwqh6pibley.png)


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

```c
		// 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");
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hlawqx7kfbqll4y1zui.png)


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

```c
		// 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");
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8bbvbjd15s2xtqf3cvm.png)



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

```c
		// 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();
```

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

```c
#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;
}
```

The output produced will be:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3p7bnibbtqm82qlyr0n.png)