繁体   English   中英

如何通过在for循环中旋转方向来获取采样点?

[英]How to get sampling points by rotating a direction in a for loop?

我需要循环一定数量的采样点。 那些采样点是表示方向的归一化矢量。 他们应该在代码中计算它们。 从前向矢量1, 0 ,我想围绕原点旋转,以便我想出给定数量的方向。

for(int i = 0; i < number_of_sampling_points; ++i)
{
    // get direction vector based on i and number_of_sampling_points
    // ...
}

例如,具有number_of_sampling_points4循环我想要得到的值对内部1, 00, 1-1, 00, -1 订单无关紧要。

使用trig:

double x = std::cos(pi * 2 * (double)i / (double)number_of_sampling_points);
double y = std::sin(pi * 2 * (double)i / (double)number_of_sampling_points);

尝试这个:

const double PI = 3.14159265358979323846;
const int number_of_sampling_points = 4;
for (int i = 0; i < number_of_sampling_points; ++i)
{
    const double a = PI * 2 * (1.0 - i) / number_of_sampling_points;

    double x = sin(a);
    double y = cos(a);

    cout << "(" << x << " , " << y << ")" << endl;
}

输出(四舍五入):

(1 , 0)
(0 , 1)
(-1 , 0)
(0 , -1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM