How to draw an arc in C++

Hi guys, today we will see how to draw an arc in the C++ language.

Before, we start, let me tell you that whenever we want to create any graphic in C++, we have to add a header file called <graphics.h>

C++ program to draw an arc

Now, let us see how can we draw an arc in C++.

Syntax of arc() is:

void arc(int x, int y, int start_angle, int end_angle, int radius);

where,

(x,y) = center of arc
start_angle= starting angle of the arc
end_angle= ending angle of the arc
radius= radius of the arc

Whenever we want to create an arc, we need 5 things i.e. x,y coordinates, starting angle, ending angle, and radius.

Let me first show the code and then I will explain.

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
  clrscr();
  int gd=DETECT,gm;
  int x,y,sangle,eangle,radius;

  cout<<"enter x,y co-ordinates\nenter starting angle\nenter ending angle\nenter radius\t";
  cin>>x>>y>>sangle>>eangle>>radius;

  initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

  arc(x,y,sangle,eangle,radius);

  getch();
  closegraph();
}

So, in this code, we used a variable gm, it is graphic mode which generates the image. DETECT is a macro function. They both are used to draw any graphic.
Latterly, we asked the user to enter the coordinates of the arc, the starting and ending angle of the arc and the radius.
initgraph initializes the graphic system.
Then we called the arc() to display the arc according to the inputs entered by the user.
At last, we called the closegraph(), which will return us to normal mode.

Input:

enter x,y co-ordinates
enter starting angle
enter ending angle
enter radius        
200 200
0
90
100

So, the output will be:

arc

We can even create a circle using the arc(). Let me show an example,
input:

enter x,y co-ordinates
enter starting angle
enter ending angle
enter radius        
200 200
0
360
100

The output for the complete circle will be:

circle

This is because a circle is nothing but an arc from 0 to 360 degrees.

Leave a Reply

Your email address will not be published. Required fields are marked *