Draw a Circle in C++ Using Graphics.h

C++ graphics

Introduction to C++ graphics

Graphics in C++ is defined to create a graphic model like creating different shapes and adding colors to it. It tin be done in the C++ console by importing graphics.h library to GCC compiler. We tin draw the circle, line, eclipse, and other geometric shapes besides. The application of Object-oriented Programming is a principal technique to be used here. C++ does non accept any built-in functions to perform drawing as they accept low-level programs to utilise; instead, we tin apply API to practise graphics.

Syntax

The formal syntax is given equally:

# include<graphics.h>
{
Initgraph();
}

Few Graphics attributes are:

setcolor(color), setbkcolor(color), setlinestyle(fashion, blueprint,thickness).

How do graphics piece of work in C++?

The graphics are a two-dimensional concept; to implement this, we need implementation and few functions in C++ programming. So, a window or canvas is the main feature to bear witness the output. Since we need a good framework to develop a skilful feature to depict, here in this article, I have used DevC++ IDE for that nosotros need a certain package in addition to work with Graphics, to download this, we can refer WinBGIm to install the graphics library.

To work with DevC++, we demand to download graphics.h and libbgi. a file. The next step is to go to the project and select project options followed by the parameter tab and paste the following in a linker tab: lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32.

Many GUI programming is stopped in C++ because they don't have the default graphics library.

To work with graphics, nosotros need a few essentials before inbound the code.

1. Co-ordinates: This specifies how points are placed in a window; the initial origin of the screening signal is assumed as (0,0). This co-ordinated arrangement depicts how and where to perform a draw choice is specified. The Graphics screen has 640 X 480 pixels.

2. Basics of Colour: The default color elements are red, green, and blue; the output of all these colors confines to the pixels of the screen. To set a colour, we tin apply setcolor (number); The number specifies the color code; for example, a number 14 is given for yellow. Shading and coloring add extra furnishings to the image.

Few functions brand the code more attractive, which works well in graphics manner.

  1. Glimmer: Information technology helps to glimmer a character on the screen window.
  2. GOTOXY: It helps to move a cursor to whatsoever position on the screen.
  3. Delay: Information technology suspends a few sections. For example, to motion the next car. It waits for a while.
  4. Position functions like getmaxx(), getx() and gety().

Ok, let's go with the working steps in graphics code.

  1. The offset footstep is to include a header file GRAPHICS.H with a graphic function, and graphic.lib has built-in library functions.
  2. Next is to include a function initgraph () which starts the graphic mode and comes with two variables gd, a graphic driver and gm, graphic mode. followed by this, we tin employ a directory path.
  3. Closegraph () – This function shifts the screen back to text mode. To end the program, this function is used; it flushes the memory used before for the graphics.
  4. clear() – It returns the cursor position to (0,0).
  5. circumvolve () – Creates a circle with a given radius.
  6. line () – Creates a line with starting and ending points.

For example, to draw a uncomplicated line or a circumvolve, the following parameters are added.

  • lineto(x,y): it moves from the current position to the user-divers position.
  • circle (x, y, radius): To depict a whole circle, we need a eye radius.
  • rectangle (x1, y1, x2, y2): where x1, y1 is the upper left side and the lower correct is x2, y2.

Examples of C++ graphics

Here I have given a sample plan on how to work on the graphics mode and development process in devC++.

Case #one

To draw a triangle in C++ using graphics

Code:

#include <graphics.h>
#include <iostream>
int main()
{
int gd = Notice, gm;
initgraph(&gd, &gm, "");
line(140, 140, 350, 100);
line(140, 140, 200, 200);
line(350, 140, 200, 200);
getch();
closegraph();
}

Explanation

The in a higher place simple Code draws a line of x1, y1, x2, y2 points on a screen. Gd, gm is a graph mode for a part initgraph. The generated graphics window of the above code is shown equally:

Output:

C++ graphics output 1

Example #2

Creating a Domicile Page with Rectangle shapes and text

Code:

#include<iostream.h>
#include<conio.h>
#include<graphic.h>
#include<math.h>
void main()
{clrscr();
int g=0,a;
initgraph(&m,&d,"");
setbkcolor(14);
setcolor(six);
settextstyle(2,0,4);
outtextxy(180,130,"G");
setcolor(5);
settextstyle(2,0,4);
outtextxy(120,120,"O");
setcolor(6);
settextstyle(2,0,four);
outtextxy(300,120,"O");
setcolor(5);
settextstyle(two,0,4);
outtextxy(250,130,"G");
setcolor(ii);
settextstyle(ii,0,4);
outtextxy(360,160,"L");
setcolor(3);
settextstyle(2,0,4);
outtextxy(310,130,"E");
setcolor(ix);
settextstyle(ii,0,4);
setcolor(8);
settextstyle(2,0,4);
outtextxy(110,250,"surf");
settextstyle(two,0,4);
outtextxy(350,320,"Get AHEAD");
setcolor(6);
rectangle(130,210,450,210);
rectangle(90,310,170,340);
rectangle(360,320,510,320);
getch();
}

Explanation

The above code draws a rectangle shape along with text in a dissimilar color.

Output:

C++ graphics output 2

Example #3

Code:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);
void main()
{
int gd,gm=Discover;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
rectangle(60,60,90,ninety);
flood (50,fifty,8,0);
getch();
}
void flood(int a,int b, int fcol, int col)
{
if(getpixel(a,b)==col)
{
filibuster(xv);
putpixel(a,b,fcol);
flood(a+1,b,fcol,col);
flood (a-i,b,fcol,col);
inundation (a,b+1,fcol,col);
flood (a,b-1,fcol,col);
}
}

Explanation

The above code flooded a shape with the text color.

Output:

output 3

Example #4

Code:

#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void ellipsedr(int e1, int e2, int a1, int b1,
bladder alp, int color)
{
float tt = iii.14 / 180;
alp= 360 - alp;
setcolor(color);
int tetaa;
for (int j = 0; j < 360; j += 1) {
tetaa = j;
int x1 = a1 * cos(t1 * tetaa) * cos(t1 * al)
+ b1 * sin(t1 * tetaa) * sin(tt * alp);
int y1 = b1 * sin(t1 * tetaa) * cos(tt * alp)
- a * cos(tt * tetaa) * sin(tt * alp);
putpixel(e1 + x1, e2 - y1, color);
}
}
void view(int e1, int e2, int rr, int a1, int b1,
int alp, float pp, int colour)
{
setcolor(color);
float tt = 3.xiv / 180;
float ta, tb, d;
float angle = (pp * alp);
ta = cos(t * fmod(bending, 360));
tb = sin(t * fmod(angle, 360));
ta*= ta;
tb *= tb;
ta = ta / (a1 * a1);
tb = tb / (b1 * b1);
d = sqrt(ta + tb);
d = one / d;
int gox = e1 + (rr + d) * cos(tt * alp);
int goy = e2 - (rr + d) * sin(tt * alp);
int goang = angle + alp;
ellipsedr(gox, goy, a,
b, draw_ang, color);
}
void elipsecirc(int ninety, int yc,
int rr, int a1, int b1)
{
float tetaa = 0;
double hei, pi1;
hei = (a1 * a1) + (b1 * b1);
hei /= ii;
pi1 = sqrt(hei);
pi1 /= rr;
pi1 = 1 / (pi1);
for (;; tetaa -= 1) {
view(e1, e2, rr, a1, b1,tetaa, pi1, WHITE);
circle(xcir, ycir, rr);
delay(25);
view(e1, e2, rr, a1, b1,tetaa, pi1, Black);
}
}
int primary()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int mx = getmaxx();
int my = getmaxy();
elipsecirc(mx / ii, my / 2,
90, xxx, 26);
closegraph();
return 0;
}

Explanation

The above code displays an ellipse over the circumvolve past setting x and y coordinates.

Output:

output 4

Conclusion

In this commodity, we take described how graphics work in C++ programming. We have presented simple and full general functions used in graphics to practise programming. We accept also discussed the design and case process to sympathise the concept.

Recommended Articles

This is a guide to C++ graphics. Hither we hash out how graphics work in C++ programming and Examples along with the codes and outputs. You can besides look at the following article to larn more –

  1. C++ accented value
  2. C++ thread( )
  3. C++ sort()
  4. C++ reserve()

gallodwellied.blogspot.com

Source: https://www.educba.com/c-plus-plus-graphics/

0 Response to "Draw a Circle in C++ Using Graphics.h"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel