Write a program in C to draw concentric circle on screen using graphics.h header file
In this program, we will draw four circle on screen having centre at mid of the screen and radius 30, 50, 70 and 90 pixels. We will use outtextxy and circle functions of graphics.h header file. Below is the detailed descriptions of graphics functions used in this program.
Function | Description |
---|---|
initgraph | It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. |
getmaxx | It returns the maximum X coordinate in current graphics mode and driver. |
getmaxy | It returns the maximum Y coordinate in current graphics mode and driver. |
outtextxy | It displays a string at a particular point (x,y) on screen. |
circle | It draws a circle with radius r and centre at (x, y). |
setcolor | It changes the current drawing colour. Default colour is white. Each color is assigned a number, like BLACK is 0 and RED is 4. Here we are using colour constants defined inside graphics.h header file. |
closegraph | It unloads the graphics drivers and sets the screen back to text mode. |
C program to draw concentric circles using graphics
In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). It is the first step you need to do during graphics programming. Setting graphics driver as DETECT, means instructing the compiler to auto detect graphics driver. Here we are using getmaxx and getmaxy function to find the centre coordinate of the screen and setcolor function to change he colour of drawing.
#include<stdio.h> #include<graphics.h> #include<conio.h> int main(){ int gd = DETECT,gm; int x ,y; initgraph(&gd, &gm, "C:\\TC\\BGI"); /* Initialize center of circle with center of screen */ x = getmaxx()/2; y = getmaxy()/2; outtextxy(240, 50, "Concentric Circles"); /* Draw circles on screen */ setcolor(RED); circle(x, y, 30); setcolor(GREEN); circle(x, y, 50); setcolor(YELLOW); circle(x, y, 70); setcolor(BLUE); circle(x, y, 90); getch(); closegraph(); return 0; }
Program Output