×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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

Draw a circle in C programming

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); 

Draw a RECTANGLE in C programming

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.