Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 22717928
t0t0: vectors, not arrays

is there any code that anyone can show me? I'm having trouble doing it myself.
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

789 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