You are on page 1of 7

Installation Steps To Configure For Graphics

So far we have been using C language for simple console output only. Most of us are unaware
that using C++, low level graphics program can also be made. This means we can incorporate
shapes, colors and designer fonts in our program. This article deals with the steps to enable
the DevC++ compiler to generate graphics .
Configuring DevC++
 Step 1: Download the DevC++ version 5.11 from here.
 Step 2: Download the Graphics header files, and etc stuff needed from the given
dropbox link.
 Step 3: Extract the contents of the rar file.
 Step 4: Go to the location where DevC++ is installed. For me its D drive. Go inside the
MinGW64 folder. Copy the graphics.h and winbgim.h in the include folder and D:\Dev-
Cpp\MinGW64\x86_64-w64-mingw32\include folder.
 Step 5:Copy the libbgi.a file into lib folder and in D:\Dev-Cpp\MinGW64\x86_64-w64-
mingw32\lib folder.
 Step 6: Copy the ConsoleAppGraphics.template, ConsoleApp_cpp_graph.txt files and paste
them inside the template folder of the devc++ installer location.
Now we are done with configuring of the DevC++ to support graphics programming. We shall
write our very first graphics program now.
Running the first graphics program
1. Open DevC++. Click file ->New ->Project.
2. Make sure you get the Console Graphics option. However, we are not going to click on it.
3. Choose Empty Project option and Give a project name and make sure the selected
language is C++.
4. Copy the following code to the editor window
Example 1
#include<graphics.h>

int main( ){

initwindow( 700 , 700 , "MY First Program");

circle(200, 200, 150);

getch();

return 0;

Example 2
Display a galaxy of circles in C/C++ is as below.

#include<graphics.h>

int main( ){

initwindow( 400 , 400 , "Graphics using Dev-C++");

setcolor(YELLOW);

for(int i=5; i<=150; i+=5) {

circle(200, 200, i);

getch();

return 0;

}
Example 3 // C++ Implementation for drawing line
#include <graphics.h>

// driver code
int main()
{
// gm is Graphics mode which is a computer display
// mode that generates image using pixels.
// 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
initgraph(&gd, &gm, "");

// line for x1, y1, x2, y2


line(150, 150, 450, 150);

// line for x1, y1, x2, y2


line(150, 200, 450, 200);

// line for x1, y1, x2, y2


line(150, 250, 450, 250);

getch();

// closegraph function closes the graphics


// mode and deallocates all memory allocated
// by graphics system .
closegraph();
}
Example 5 // C++ program to draw an Ellipse
// rotating over a Circle using graphics

#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

// Ellipse drawing function


void drawEllipse(int xc, int yc, int a, int b,
float alpha, int color)
{
float t = 3.14 / 180;
alpha = 360 - alpha;
setcolor(color);
int theta;

// Filling each pixel corresponding


// to every angle from 0 to 360
for (int i = 0; i < 360; i += 1) {
theta = i;
int x = a * cos(t * theta) * cos(t * alpha)
+ b * sin(t * theta) * sin(t * alpha);

int y = b * sin(t * theta) * cos(t * alpha)


- a * cos(t * theta) * sin(t * alpha);

putpixel(xc + x, yc - y, color);
}
}

// Function to calculate the position


// of ellipse after each rotation
void slidePattern(int xc, int yc, int r, int a, int b,
int alpha, float p, int color)
{
setcolor(color);
float t = 3.14 / 180;
float t1, t2, d;
float angle = (p * alpha);

// Calculation for center of Ellipse


t1 = cos(t * fmod(angle, 360));
t2 = sin(t * fmod(angle, 360));
t1 *= t1;
t2 *= t2;
t1 = t1 / (a * a);
t2 = t2 / (b * b);
d = sqrt(t1 + t2);
d = 1 / d;

int draw_x = xc + (r + d) * cos(t * alpha);


int draw_y = yc - (r + d) * sin(t * alpha);
int draw_ang = angle + alpha;

drawEllipse(draw_x, draw_y, a,
b, draw_ang, color);
}

// Function to increment the angle


// of rotation
void ellipseovercircle(int xc, int yc,
int r, int a, int b)
{
float theta = 0;
double h, p1;

// Calculating the ratio of


// perimeters of Ellipse and Circle
h = (a * a) + (b * b);
h /= 2;
p1 = sqrt(h);
p1 /= r;
p1 = 1 / (p1);

// by decreasing theta we can


// move Ellipse clockwise
for (;; theta -= 1) {

// Draw Ellipse at new location


// using White color
slidePattern(xc, yc, r, a, b,
theta, p1, WHITE);

circle(xc, yc, r); // Drawing Circle


delay(25); // Introducing delay

// Erase the existing Ellipse


slidePattern(xc, yc, r, a, b,
theta, p1, BLACK);
}
}

// Driver code
int main()
{
// Initialize graphics function
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

// maximum X-coordinate for the window


int maxx = getmaxx();

// maximum Y-coordinate for the window


int maxy = getmaxy();

// Start drawing from the mid of the screen


ellipseovercircle(maxx / 2, maxy / 2,
100, 40, 28);

closegraph();
return 0;
}
Example 6
to create circles that will keep on growing till the assigned number
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

int main()

int gd=DETECT, gm; int i;

//clrscr();

initgraph (&gd,&gm,"c:\\turboc3\\bgi");

for(i=0;i<100;i++)

setcolor(i);

//coordinates of center from x axis, y axis, radius

circle(200,200,50+i*2);

delay(30);

//cleardevice(); //try with and without cleardevice();

getch();

closegraph();

You might also like