# Program to make “Pieslice” without using library function?

As we know in C/C++ there is "graphics.h" header file with the help of which we can do graphics programs in C. There is a function pieslice in that header file,its syntax is:

#include <graphics.h>

void pieslice(int x, int y, int stangle, int endangle, int radius);


[x,y are the center of the circle,stangle and endangle are the starting and end angles respectively]

Can we make a pieslice in C/C++ without using this in-built function.Please help. Tried making it with the help of the lines and arc function of the graphics.h but i am still not able to figure out how to connect the lines with the arc. Thank You in advance.

My code so far:

#include<stdio.h>
#include<graphics.h>
#include<math.h>

int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
int xc,yc,r,st_angle,ed_angle,StartX,StartY;
printf("Enter the centers of pieslice:\n");
scanf("%d %d",&xc,&yc);
scanf("%d",&r);
printf("Enter the starting angle:\n");
scanf("%d",&st_angle);
printf("Enter the end angle:\n");
scanf("%d",&ed_angle);

double startRadians = (double)st_angle * M_PI /180.0;
double endRadians = (double)ed_angle * M_PI /180.0;

{
startX = xc+ cos(startRadians) * r;
startY = yc+ sin(startRadians) * r;
}
{
endX = xc+ cos(endRadians) * r;
endY = yc+ sin(endRadians) * r;
}


• What do you already know about the pieslice shape that would help you draw it? Do you want to write a function that takes the same arguments as the existing pieslice function? To help people know what you're looking for, could you edit to explain what you are starting with? Do you want to construct this shape using arcs and straight lines provided by graphics.h or do you want to draw it one pixel at a time? – trichoplax Sep 29 '19 at 7:19
• I want to draw it using arcs and line provided by  graphics.h  . – Netranjit Borgohain Sep 29 '19 at 7:30
• FYI: graphics.h is not a part of C++. It's some external library. – Nicol Bolas Oct 31 '19 at 15:41

You can use the sine and cosine functions to do this. Normally they start on the x axis and rotate counter-clockwise. The start point can be found by calculating:

startX = x + cos(startRadians) * radius;


Likewise the ending coordinates are:

endX = x + cos(endRadians) * radius;


Note that sin() and cos() take their parameters in radians. Given that stangle and endangle are integers, I'm guessing they're probably in degrees so you'll need to convert them by doing something like:

double startRadians = (double)stangle * M_PI / 180.0;


etc.

If you want to draw the entire pie wedge, you'll need to iterate from the start angle to the end angle and calculate every n degrees along the curve.

• Thanks for the help – Netranjit Borgohain Sep 27 '19 at 4:32
• Please look into the codes i have written so far(updated my question). I know its not perfect,i am very new to coding. Specially usage of  graphics.h  – Netranjit Borgohain Oct 1 '19 at 5:01
• It looks almost right. I think you only want one loop that goes from startAngle to endAngle. And of course you'll need to actually draw some lines between the points you generate. – user1118321 Oct 1 '19 at 16:00
• Can you help me with that,please! I am clueless after this. – Netranjit Borgohain Oct 1 '19 at 16:08
• This site isn't for how to write code. I suggest you post it to the main StackOverflow site to get help with that. – user1118321 Oct 2 '19 at 1:08