0

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
Reply
2
Views
9 Years
Discussion Span
Last Post by VernonDozier
0

I think you are going to have to define "rotate around an axis" and what exactly you are trying to accomplish with that. x, y, and z are coordinates? The word "vector" threw me until I noticed that it is a struct and that you are using the word "vector" in math terminology rather than computer science terminology. If I rotate the vector (1, 2, 3) around the x-axis, what should the results be?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.