Using C, C++ Graphics, we can create basic shapes like line, circle, rectangle, ellipse, arc, bar, bar3d, and display text. To use graphics functions, we must include graphics.h header file.
Line function is used to draw a straight line, there are 4 parameters used in line function (x1,y1,x2,y2).
Example 1: Line function in C - Graphics. 450
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; int x1=100, y1=100, x2=200, y2=200; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); line(x1,y1,x2,y2); getch(); closegraph(); }
Circle function is used to draw a circle which required 3 parameters i.e x, y and radius.
Example 2: WAP to draw a circle with radius of 80 pixels. 388
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; int x=100, y=100, r=80 initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); circle(x, y, r); getch(); closegraph(); }
Example 3: WAP to draw a circle with radius of 80 pixels on the center of screen.
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; int x, y, r=80 initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); x = getmaxx()/2; y = getmaxy()/2; circle(x, y, r); outtextxy(x-100, y-50, "Circle on center of screen"); getch(); closegraph(); }
Rectangle function is used to draw a rectangle which required 4 parameters i.e x1(left), y1(top), x2(right) and y2(bottom).
Example 4: Rectangle function in C - Graphics. 357
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); rectangle(100,100,200,200); getch(); closegraph(); }
Ellipse function is used to draw a ellipse. Syntax: ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
Example 5: Ellipse function in C - Graphics. 263
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); ellipse(100, 100, 0, 360, 50, 25); getch(); closegraph(); }
Arc function is used to draw a arc. Syntax: arc(int x, int y, int stangle, int endangle, int radius);
Example 6: Arc function in C - Graphics. 320
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); arc(100, 100, 0, 135, 50); getch(); closegraph(); }
Bar function draws a rectangle and fill it with current fill pattern and color. Syntax: bar(int left, int top, int right, int bottom);
Example 7: Bar function in C - Graphics. 345
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); bar(100, 100, 200, 200); getch(); closegraph(); }
Bar3d function is used to draw a 2-dimensional rectangular filled in bar. Syntax:
bar3d(int left, int top, int right, int bottom, int depth, int topflag); left, top, right, bottom are the positions depth specifies the depth of bar in pixels topflag determines whether a 3 dimensional top is put on the bar or not (1 for yes, 0 for not )
Example 8: Bar3d function in C - Graphics. 291
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); bar3d(100, 100, 200, 200, 20, 1); getch(); closegraph(); }
Example 9: outtextxy function in C - Graphics. 289
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\turboc3\\bgi"); cleardevice(); outtextxy(100, 200, "My first C graphics program"); getch(); closegraph(); }
In C Graphics, there are 16 colors declared. We use colors to set background color, change color of shapes, fill color of shapes, change text color.
textcolor(GREEN); cprintf("Text in Green");
setbkcolor(GREEN); rectangle(100,100,200,200);
setcolor(GREEN); or setcolor(3); rectangle(100,100,200,200); outtextxy(110,110,"Text in Green")
S.No. | Color Name | Color Value |
---|---|---|
BLACK | 0 | |
BLUE | 1 | |
GREEN | 2 | |
CYAN | 3 | |
RED | 4 | |
MAGENTA | 5 | |
BROWN | 6 | |
LIGHTGRAY | 7 | |
DARKGRAY | 8 | |
LIGHTBLUE | 9 | |
LIGHTGREEN | 10 | |
LIGHTCYAN | 11 | |
LIGHTRED | 12 | |
LIGHTMAGENTA | 13 | |
YELLOW | 14 | |
WHITE | 15 |