Rotating Vectors C++

Posted on 2008-10-12
Last Modified: 2013-12-14
Hi all. I have a vector defined by:
vector < vector < vector < bool > > >

This vector defines an object in 3 dimensions. In other words, each true value represents a point and each false value represents a space.

I would basically like to "rotate" this vector on the x y and z axis separately. How would I do this?
Question by:momonja
  • 4
  • 2
  • 2
  • +1
LVL 53

Accepted Solution

Infinity08 earned 250 total points
ID: 22701017
Start by getting a 2D matrix rotated ... The 3D situation is equivalent, in that it comes down to rotating several 2D matrices (at the same time) instead of just one.

To rotate a 2D matrix, I assume we're talking about right-angled rotations, like :

        0 1 2 3
        4 5 6 7
        8 9 A B
        C D E F

becomes the following after a rotation over 90 degrees clockwise :

        C 8 4 0
        D 9 5 1
        E A 6 2
        F B 7 3

By looking at that example, it should be clear which positions move where when rotating, and implementing a matrix rotation shouldn't be too difficult.
LVL 16

Expert Comment

ID: 22716133

Hiya momonia

You can rotate vector coordinates using matrix transformation.
For linear displacement you should look at translation.
Oh, and you can use inverse transformations to remove hidden surfaces.
Apart from that and a bit of trig the the rest is fairly simple.
Have fun.


// code to multiply matrices (concatenation) 

void multiply_matrices(float a[4][4], float b[4][4])


  float tmp[4][4];

  int i, j; 

  for (j = 0; j < 4; j++)

    for (i = 0; i < 4; i++)

      tmp[i][j] = a[i][0] * b[0][j]

                 +a[i][1] * b[1][j]

                 +a[i][2] * b[2][j]

                 +a[i][3] * b[3][j]; 

  for (i = 0; i < 4; i++)

    for (j = 0; j < 4; j++)

      a[i][j] = tmp[i][j];


// multiply with: 

// x-rotation 

//    1        0        0        0

//    0        cos(x)   sin(x)   0

//    0        -sin(x)  cos(x)   0

//    0        0        0        1 


// y-rotation 

//    cos(y)   0        -sin(y)  0

//    0        1        0        0

//    sin(y)   0        cos(y)   0

//    0        0        0        1 


// z-rotation 

//    cos(z)   sin(z)   0        0

//    -sin(z)  cos(z)   0        0

//    0        0        1        0

//    0        0        0        1 


// Maths required to transform a vector using a matrix

// x0 etc are the original object space coords for the vector 

x = x0 * a[0][0] + y0 * a[1][0] + z0 * a[2][0] + a[3][0];

y = x0 * a[0][1] + y0 * a[1][1] + z0 * a[2][1] + a[3][1];

z = x0 * a[0][2] + y0 * a[1][2] + z0 * a[2][2] + a[3][2]; 

// You may need to look into homogenous coordinate systems... 

Open in new window

LVL 16

Expert Comment

ID: 22716361
Sorry to repeat for the sake of clarity...
void multiply_matrices(float a[4][4], float b[4][4])


  float tmp[4][4];

  int i, j; 

  for (j = 0; j < 4; j++)

    for (i = 0; i < 4; i++)

      tmp[i][j] = a[i][0] * b[0][j]

                + a[i][1] * b[1][j]

                + a[i][2] * b[2][j]

                + a[i][3] * b[3][j]; 

  for (i = 0; i < 4; i++)

    for (j = 0; j < 4; j++)

      a[i][j] = tmp[i][j];


Open in new window


Author Comment

ID: 22717928
t0t0: vectors, not arrays

is there any code that anyone can show me? I'm having trouble doing it myself.
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

LVL 53

Expert Comment

ID: 22718766
In order to help you better, we'll need to know what this will be used for ... Can you give a bit more background information ?

Can you also confirm that my interpretation (in my first post) is correct ?
LVL 16

Assisted Solution

t0t0 earned 250 total points
ID: 22719674

I've just reread your post - I thought maybe i'd misinterpreted your question.

Let me get this right... you want to rotate the position of a single point in space whose vector coordinates are (x, y, z) around either the x, y or z axis. Is that about the size of it?

Because if that's the case then you'll have to get to grips with matrix transformation.... unless you want to skip the formal stuff and invent a lazy way to do something that's been done day in and day out for the past century using the tools already suggested by myself and infinty08.

Let's say you've got a point somewhere in space. The point's position is denoted by it's x, y and z coordinates. Would you agree? In that case we can say p = (x, y, z).

There are three things you might want to do with a point in space...

Translation - moving a point in any plain leaving the other two coordinates of the point unchanged.

Rotation - rotating the point around either the x, y or z axis, where the coordinate around the axis of rotation remains unchaged (think about it) and the other two coordinates change in relation to their original positions.

The other operation you might want to perform on an (x, y, z) vector coordinate is scaling, but I'm not going to discuss that.

Simple translation
Let's say you want to translate a point (x, y, z). You'll be altering it's relative position without introducing other changes (such as scaling or rotation). We can denote transformed qhantities by a prime ( ' ) and if the displacement vector is t, then the translation can be expressed as:

        r' = r + t

where r is the position vector of the original point and r' is the position vector of the same point after translation. Applying this transformation on every vertex of the original point results i a new set of vertices that define the transformed point.

When expressed in the component form, the transformed coorinates are evaluated from:

                                                  /  1   0   0   0  \
                                                  |  0   1   0   0  |
        (x', y', z', 1) = (x, y, z, 1)   |  0   0   1   0  |
                                                  \  tx  ty  tz  1  /

where tx, ty and tz are the components of the translation vector t. And that's just simple displacement!

Now, suppose a point rotates about the z axis through a an angle g. The axis of rotation is fixed, only the x and y coordinates change. The angle of rotation is measured in a couterclockwise direction when the point is seen along the rotation axis, towards the origin. This is represented by:

                                                  /  cos(g)   sin(g)    0   0  \
                                                  |  -sin(g)   cos(g)   0   0  |
        (x', y', z', 1) = (x, y, z, 1)   |      0           0        1   0  |
                                                  \      0           0        0   1  /

Graphically, the rotation about the y axis is given by:

                                                  /  cos(g)   0    -sin(g)   0  \
                                                  |      0        1        0        0  |
        (x', y', z', 1) = (x, y, z, 1)   |  sin(g)    0    cos(g)    0  |
                                                  \      0       0         0        1  /

And rotation about the x axis is given by:

                                                  /  1         0            0        0  \
                                                  |   0     cos(g)   sin(g)     0  |
        (x', y', z', 1) = (x, y, z, 1)   |  0     -sin(g)    cos(g)   0  |
                                                  \  0         0            0        1  /

To display a point in 3D onto a 2D plane, say a computer screen, it has to be projected using parallel or perspective projection but that's another matter and one which i'll need to look up - something perhaps you might like to do....
LVL 18

Expert Comment

ID: 22723646
To transform these vectors,  you need coordinates.
In your case, a point coordinates are its indexes.
This is done in a four steps procedure:
  1. Convert point indexes to coordinates
  2. Set such point to false (it will be in another place...)
  3. Do the transformation by rotating, scaling or translating.
  4. Convert transformed coordinate back to index
As a matter of ease understanding, follows an example of single displacement (translation)
be  [v1][v2][v3]  the vector to translate
step 1:     x = v1; y = v2; z = v3;
step 2:     vector[v1][v2][v3] = false // it will be in another place...
step 3:     x = v1+dx;  y = v2+dy;  z=v3+dz;
step 4:    vector[x][y][z];
The same procedure applies to rotation and scaling.
LVL 18

Expert Comment

ID: 22723659
oooops, fix pack 1:
step 4:    vector[x][y][z] = true;
LVL 16

Expert Comment

ID: 22755613

Thank you for accepting my answer. Good luck with your vectors!

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now