Uses of C++ graphics

Uses of C++ graphics
Techiio-author
Written by Sagar RabidasJanuary 28, 2022
6 min read
C++
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss C++ graphics.

C++ graphics

Graphics in C++ is defined to create a graphic model like creating different shapes and adding colors to them. It can be done in the C++ console by importing graphics.h library to GCC compiler. We can draw a circle, lines, eclipse, and other geometric shapes too. The application of Object-oriented Programming is a primary technique to be used here. C++ does not have any built-in functions to perform drawing as they have low-level programs to use; instead, we can use API to do graphics.

Syntax

The formal syntax is given as:

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

Few Graphics attributes are:

setcolor(color), setbkcolor(color), setlinestyle(style, pattern,thickness).

Examples of C++ graphics

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

Example #1

To draw a triangle in C++ using graphics

Code:

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

Example #2

Creating a Home 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(&g,&d,"");
setbkcolor(14);
setcolor(6);
settextstyle(2,0,4);
outtextxy(180,130,"G");
setcolor(5);
settextstyle(2,0,4);
outtextxy(120,120,"O");
setcolor(6);
settextstyle(2,0,4);
outtextxy(300,120,"O");
setcolor(5);
settextstyle(2,0,4);
outtextxy(250,130,"G");
setcolor(2);
settextstyle(2,0,4);
outtextxy(360,160,"L");
setcolor(3);
settextstyle(2,0,4);
outtextxy(310,130,"E");
setcolor(9);
settextstyle(2,0,4);
setcolor(8);
settextstyle(2,0,4);
outtextxy(110,250,"surf");
settextstyle(2,0,4);
outtextxy(350,320,"Go AHEAD");
setcolor(6);
rectangle(130,210,450,210);
rectangle(90,310,170,340);
rectangle(360,320,510,320);
getch();
}

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=DETECT;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
rectangle(60,60,90,90);
flood (50,50,8,0);
getch();
}
void flood(int a,int b, int fcol, int col)
{
if(getpixel(a,b)==col)
{
delay(15);
putpixel(a,b,fcol);
flood(a+1,b,fcol,col);
flood (a-1,b,fcol,col);
flood (a,b+1,fcol,col);
flood (a,b-1,fcol,col);
}
}

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,
float alp, int color)
{
float tt = 3.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 color)
{
setcolor(color);
float tt = 3.14 / 180;
float ta, tb, d;
float angle = (pp * alp);
ta = cos(t * fmod(angle, 360));
tb = sin(t * fmod(angle, 360));
ta*= ta;
tb *= tb;
ta = ta / (a1 * a1);
tb = tb / (b1 * b1);
d = sqrt(ta + tb);
d = 1 / 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 xc, int yc,
int rr, int a1, int b1)
{
float tetaa = 0;
double hei, pi1;
hei = (a1 * a1) + (b1 * b1);
hei /= 2;
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 main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int mx = getmaxx();
int my = getmaxy();
elipsecirc(mx / 2, my / 2,
90, 30, 26);
closegraph();
return 0;
}

C
C++
C++ graphics
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance