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:


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

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

    for(int k=0; k<startRadians;k++)
        startX = xc+ cos(startRadians) * r;
        startY = yc+ sin(startRadians) * r;
    for(int l=o; l<endRadians; l++)
        endX = xc+ cos(endRadians) * r;
        endY = yc+ sin(endRadians) * r;

  • $\begingroup$ 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? $\endgroup$ – trichoplax Sep 29 '19 at 7:19
  • $\begingroup$ I want to draw it using arcs and line provided by ` graphics.h ` . $\endgroup$ – Netranjit Borgohain Sep 29 '19 at 7:30
  • $\begingroup$ FYI: graphics.h is not a part of C++. It's some external library. $\endgroup$ – 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;
startY = y + sin(startRadians) * radius;

Likewise the ending coordinates are:

endX = x + cos(endRadians) * radius;
endY = y + sin(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;


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.

| improve this answer | |
  • $\begingroup$ Thanks for the help $\endgroup$ – Netranjit Borgohain Sep 27 '19 at 4:32
  • $\begingroup$ 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 ` $\endgroup$ – Netranjit Borgohain Oct 1 '19 at 5:01
  • $\begingroup$ 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. $\endgroup$ – user1118321 Oct 1 '19 at 16:00
  • $\begingroup$ Can you help me with that,please! I am clueless after this. $\endgroup$ – Netranjit Borgohain Oct 1 '19 at 16:08
  • $\begingroup$ This site isn't for how to write code. I suggest you post it to the main StackOverflow site to get help with that. $\endgroup$ – user1118321 Oct 2 '19 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.