1

I have read this and I tried to implement it in C++, but the output is quite different. I have no idea what is wrong. The code I used:

double cordinate_print()
{
    int x, y;
    int number_of_chunks = 5;
    double angle=0;
    double x_p[5] ; // number of chunks 
    double y_p[5]; // number of chunks 
    //double x0, y0 = radious;
    double rad = 150;

    for (int i = 0; i < number_of_chunks; i++)
    {
        angle = i * (360 / number_of_chunks);
        float degree = (angle * 180 / M_PI);
        x_p[i] = 0 + rad * cos(degree);
        y_p[i] = 0 + rad * sin(degree);
        //printf("x-> %d y-> %d \n", x_p[i], y_p[i]);
        cout << "x -> " << x_p[i] << "y -> " << y_p[i] << "\n";
    }

    //printing x and y values

    printf("\n \n");

    return 0;

}

Output

x -> 150 y -> 0
x -> -139.034 y -> -56.2983
x -> 107.74 y -> 104.365
x -> -60.559 y -> -137.232
x -> 4.77208 y -> 149.924

The correct output

(150,0)

(46,142)

(-121,88)

(-121,-88)

(46,-142)
Community
  • 1
  • 1
Alan
  • 121
  • 1
  • 9
  • Calling a variable "degrees" when it's actually an angle in radians is misleading to humans. The compiler doesn't care, though. Don't see the problem directly, but the points do appear to be on the circle - it's just that the angles are wrong. – MSalters Dec 06 '16 at 07:16

2 Answers2

1

Issue with the conversion of degree into radian

float degree = (angle * 180 / M_PI);

The correct conversion formula is

float radian = (angle * M_PI / 180);

Also as mentioned in the comment use the good name to avoid any confusion.

Hemant Gangwar
  • 2,172
  • 15
  • 27
1

Since your default angles are in degrees, you need to convert them to radians first before using sin() and cos(), then multiplying it to the radius.

double cordinate_print()
{
    int number_of_chunks = 5;
    double degrees = 0;                         // <-- correction
    double x_p[5]; // number of chunks 
    double y_p[5]; // number of chunks 
    double radius = 150;                        // <-- correction

    for (int i = 0; i < number_of_chunks; i++)
    {
        degrees = i * (360 / number_of_chunks); // <-- correction
        float radian = (degrees * (M_PI / 180));  // <-- correction
        x_p[i] = radius * cos(radian);          // <-- correction
        y_p[i] = radius * sin(radian);          // <-- correction

        cout << "x -> " << x_p[i] << "y -> " << y_p[i] << "\n";
    }

    //printing x and y values
    printf("\n \n");

    return 0;
}
Rowen Chumacera
  • 529
  • 3
  • 10