gpt4 book ai didi

c++ - 将圆分成n等份得到每个分割点的坐标

转载 作者:行者123 更新时间:2023-11-30 03:35:47 24 4
gpt4 key购买 nike

我已阅读 this我尝试用 C++ 实现它,但输出却大不相同。我不知道出了什么问题。我使用的代码:

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;

}

输出

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

正确的输出

(150,0)

(46,142)

(-121,88)

(-121,-88)

(46,-142)

最佳答案

将度数转换为弧度的问题

float degree = (angle * 180 / M_PI);

正确的换算公式是

float radian = (angle * M_PI / 180);

另外如评论中所述,使用好名称以避免混淆。

关于c++ - 将圆分成n等份得到每个分割点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40989768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com