Question

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.

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. The order doesn't matter.

Was it helpful?

Solution

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)

OTHER TIPS

Use 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top