Home » C programming language

Graphics in C/C++: Draw Circle and Rectangle

In this tutorial, we are going to learn how to draw a circle and a rectangle in Graphics C/C++?
Submitted by Mahima Rao, on October 24, 2018

In today's advanced Advance Learning Tutorial, we will learn to draw Circle and rectangle in C/C++ Graphics.

First, let's start with the CIRCLE...

To make the circle, we have to maintain two points:

  1. Center of the circle
  2. A radius of the circle

To draw a circle in C programming, first include graphics.h header file in your program. C has given a function to draw a circle, whose prototype is this way...

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

Here, is the center point of the x and y circle.

For example, see the program:

#include <graphics.h>
#include <conio.h>
 
void main()
{
	int gd=DETECT,gm;
	initgraph(&gd,&gm,"");
	circle(200,200,100);
	getch();
	closegraph();
} 

The circle function does not return any value and whatever value you pass in the Circle function is in the pixel form. Similarly, if you want to draw another circle inside the circle then call the circle function twice. But be careful, the radius of the previous circle should be larger than the radius of the other and the center should be the same as the circle. The syntax for the same is given below.

    circle (200,200,10);
    circle (200,200,50); 

Now let's learn to draw RECTANGLE in C/C++ Graphics...

To draw a rectangle in C graphics, first, you have to initialize the graphics and also include the graphics.h file in your program.

Have a look at the Rectangle drawing function prototype below and then we will look forward to how it is used.

    void rectangle (int left, int top, int right, int bottom);

To create a rectangle, you have to pass the four parameters in this function. The two parameters represent the left and top upper left corner. Similarly, the right bottom parameter represents the lower right corner of the rectangle.

See the program for example:

#include <graphics.h>
#include <conio.h>

void main ()
{
	rectangle (200,200,300,300);
	getch ();
}

Rectangle function also does not return any value. If you pass the same value in the four parameters, you will see the rectangle at a single point in the output i.e you will just see a dot.

Whatever value you pass in the function parameter is a type of distance like I have passed 100 in the first parameter. This means that the compiler will give 100-pixel edge to the left to create the left side of the rectangle. Likewise, when we write 100 on top, it would draw a line from the top to 100 pixels.

If you have any confusion you post your question in the comment box. That's all for today. In the next tutorial, we will learn about some other interesting topics.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.