Hi,

I have generated random unit vector. I would like to rotate it around the axes X,Y,Z in a random way. I thought that I generate 3 random numbers, e.g. 0,1,2.

And if the random number is 0, then rotate the vector around X,
And if the random number is 1, then rotate the vector around Y,
And if the random number is 2, then rotate the vector around Z.

What's wrong with this code? Maybe the "switch-case" part is wrong.... I don't know...
Any ideas?

struct vector {
float x,y,z;
};

struct rotvector {
float x,y,z;
};

int i;

vector vec[N];
rotvector rvec[N];

// Initialize the random number generator
// srand(time(0));
for (i = 0; i < N; i++) {

float phi   = uniform(0.0f, M_2PIf);
float costheta = uniform(-1.0f, 1.0f);

float x = 1.0f * sqrtf(1-costheta*costheta) * cosf(phi);
float y = 1.0f * sqrtf(1-costheta*costheta) * sinf(phi);
float z = 1.0f * costheta;

// store a vector
vec[i].x = x;
vec[i].y = y;
vec[i].z = z;

rvec[i].x = x;
rvec[i].y = y;
rvec[i].z = z;

// ROTATION <<<<<<<<<<<

float fCos, fSin;
fCos = cosf(epsilon);
fSin = sinf(epsilon);

float RotMatrixX[3][3] = {1.0f, 0.0f, 0.0f, 0.0f, fCos, -fSin, 0.0f, fSin, fCos};
float RotMatrixY[3][3] = {fCos, 0.0f, fSin, 0.0f, 1.0f, 0.0f, -fSin, 0.0f, fCos};
float RotMatrixZ[3][3] = {fCos, -fSin, 0.0f, fSin, fCos, 0.0f, 0.0f, 0.0f, 1.0f};

// ROT AROUND 'X'
float RotateX()
{
for (int i = 0; i < 3; i++)
{
double RvX = 0;
for (int k = 0; k < 3; k++)
RvX += RotMatrixX[i][k] * vec[k];
rvec[i] = RvX;
}

return RvX;
}

// ROT AROUND 'Y'
float RotateY()
{
for (int i = 0; i < 3; i++)
{
double RvY = 0;
for (int k = 0; k < 3; k++)
RvY += RotMatrixY[i][k] * vec[k];
rvec[i] = RvY;
}

return RvY;
}

// ROT AROUND 'Z'
float RotateZ()
{
for (int i = 0; i < 3; i++)
{
double RvZ = 0;
for (int k = 0; k < 3; k++)
RvZ += RotMatrixZ[i][k] * vec[k];
rvec[i] = RvZ;
}

return RvZ;
}
2
Contributors
1