2
$\begingroup$

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);
    printf("Enter the radius:\n");
    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;

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


$\endgroup$
3
  • $\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$ Commented Sep 29, 2019 at 7:19
  • $\begingroup$ I want to draw it using arcs and line provided by ` graphics.h ` . $\endgroup$ Commented Sep 29, 2019 at 7:30
  • $\begingroup$ FYI: graphics.h is not a part of C++. It's some external library. $\endgroup$ Commented Oct 31, 2019 at 15:41

1 Answer 1

1
$\begingroup$

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;

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.

$\endgroup$
4
  • $\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$ Commented Oct 1, 2019 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$ Commented Oct 1, 2019 at 16:00
  • $\begingroup$ Can you help me with that,please! I am clueless after this. $\endgroup$ Commented Oct 1, 2019 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$ Commented Oct 2, 2019 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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