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 start with Computer Graphics using C++, you must import the graphics library.
// 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 a necessary step to change your computer display mode to generate images.
// 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 any of the methods listed in the 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);
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");
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");
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");
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");
For drawing an 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");
After these function calls, you will need two additional function calls. First, one is fetch() and another one is a close graph()
// 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,
#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: