Monday, January 9, 2017

C Program to draw a circle and a point

Graphics in C - Draw a Circle and a Point


In the previous article I introduced some graphics functions in C computer language. There are a few more functions, which I’ll explain through a simple program. Here is the program given below-

#include<graphics.h>
void main()
{
           int gd = DETECT,  gm,  maxx,  maxy,  midx,  midy;
           initgraph(&gd,  &gm, "c:\\tc\\bgi");

           /* set the color of graphics */
           setcolor(RED);

           /* get maximum screen coordinates */
           maxx = getmaxx();
           maxy = getmaxy();

           /* get mid-points of screen */
           midx = maxx/2;
           midy = maxy/2;

           /* draw a circle */
           circle(midx,  midy, 100);

           /* draw a point */
           putpixel(midx,  midy,  BLUE);

           getch();
           closegraph();
           restorecrtmode();
}


Compile and execute this program. The output would be a circle of red color and a blue point drawn in the center. This circle has been drawn in the center of the screen. The explanation of the program is given below-

In this program, four new integer type variables  ‘maxx’, ‘maxy’, ‘midx’, ‘midy’ have been declared.

Function setcolor(color) has been used to set the color of graphics.

Declaration:

void far setcolor(int color);

Syntax for calling this function is given below-

setcolor(color);

Function setcolor()  sets the current drawing color to ‘color’, which can range from 0 to the maximum number  your computer  supports. You can find this maximum number by the function getmaxcolor(). You can give the name of color in capital letters too as I have given ‘RED’ in the given program.

Function getmaxx()  returns maximum x screen coordinate(screen-relative) for the current graphics driver and mode.

Declaration:

Int far getmaxx(void);

Syntax for calling this function is given below-

getmaxx();

The value returned by this function has been assigned to variable ‘maxx’.

maxx = getmaxx();

Function getmaxy() returns the maximum y screen coordinate(screen-relative) for the current graphics driver and mode.

Declaration:

Int far getmaxy(void);

Syntax for calling this function is given below-

getmaxy();

The value returned by this function has been assigned to variable ‘maxy’.

maxy = getmaxy();

This way we get the maximum width and height of the screen in pixels.

Now we get the mid points of the screen by these statements-

midx = maxx/2;
midy = maxy/2;

Function circle() draws a circle.

Declaration:

void far circle(int x, int y, int radius);

In the circle() there are three arguments. First two arguments are the x and y coordinates of the center. Third argument is the radius of the circle in pixels.

Syntax for calling this function is given below-

circle(x, y, radius);

So in our program, statement circle(midx, midy, 100); draws a circle whose center is the midpoint of the screen, and radius is 100 pixels. Since we have set the color of graphics ‘RED’ by the setcolor() function, therefore outline of the circle would be of red color.

Function putpixel() plots a pixel or dot of a given color at a given point.

Declaration:

void far putpixel(int x, int y, int color).

Syntax for calling this function is given below-

putpixel(x, y, color);

So the statement  given below plots a dot of blue color in the center of the screen, hence in the center of the circle.

putpixel(midx, midy, BLUE);

Therefore, the explanation of this simple program is complete. For the explanation of remaining variables and functions, please refer my previous article C Program to draw a line.



No comments:

Post a Comment

Please give your comment