[英]How to get sampling points by rotating a direction in a for loop?
I need to loop over a given number of sampling points. 我需要循环一定数量的采样点。 Those sampling points are normalized vectors representing directions. 那些采样点是表示方向的归一化矢量。 They should be calculate them in code. 他们应该在代码中计算它们。 Starting with a forward vector 1, 0
, I want to rotate around the origin so that I came up with the given number of directions. 从前向矢量1, 0
,我想围绕原点旋转,以便我想出给定数量的方向。
for(int i = 0; i < number_of_sampling_points; ++i)
{
// get direction vector based on i and number_of_sampling_points
// ...
}
For example, with number_of_sampling_points
is 4
inside the loop I want to get the value pairs 1, 0
, 0, 1
, -1, 0
, 0, -1
. 例如,具有number_of_sampling_points
是4
循环我想要得到的值对内部1, 0
, 0, 1
, -1, 0
, 0, -1
。 The order doesn't matter. 订单无关紧要。
Use trig: 使用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);
Try this: 尝试这个:
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;
}
Output (rounded): 输出(四舍五入):
(1 , 0)
(0 , 1)
(-1 , 0)
(0 , -1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.