2

A quaternion is a number of the form:

a + bi + cj + dk

Right? Good, but...

  1. How do I interpret this number in C language?

  2. If for example I want to rotate a cube What multiplied by the quaternion? A vector?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Javier Ramírez
  • 1,001
  • 2
  • 12
  • 23
  • To represent a quarternion, you either need a struct with 4 members, or a length-4 array. As for the rest of your questions, that's essentially maths, and so is off-topic for SO... – Oliver Charlesworth Mar 13 '13 at 00:51
  • Possibly duplicate of http://stackoverflow.com/questions/508370/quaternion-libraries-in-c-c? – rhughes Mar 13 '13 at 01:32

2 Answers2

6

For your first question, I think you mean "how do I represent", not "interpret".

The simplest way is to use a struct:

typedef struct quaternion_t {
  double x,y,z,w;
} quaternion_t;

Note that a usual practice, as used above, is also to use x, y, z, and w for the component names (but your naming is perfectly acceptable, as long as you know which one is which). The use of double or single precision floats for components depends on your needs: accuracy or space.

Simple operations are then easy to implement:

void conjugate(quaternion_t *q){
    q->x = -q->x;
    q->y = -q->y;
    q->z = -q->z;
}

double product(quaternion_t *q1, quaternion_t *q2){
    return q1->x * q2->x + q1->y * q2->y + q1->z * q2->z + q1->w * q2->w;
}

double norm(quaternion_t *q){
    double p = product(q,q);
    return sqrt(p);
}

// etc

For your second question, I suggest that you look for a good tutorial on that topic. Meanwhile, the wikipedia pages:

provide a good introduction.

didierc
  • 14,572
  • 3
  • 32
  • 52
4

Whether you code this in C or C++, the math will be exactly the same.

See here for some details:

quaternion libraries in C/C++ http://en.wikipedia.org/wiki/Quaternion http://irrlicht.sourceforge.net/docu/classirr_1_1core_1_1quaternion.html https://irrlicht.svn.sourceforge.net/svnroot/irrlicht/trunk/include/quaternion.h

Community
  • 1
  • 1
rhughes
  • 9,257
  • 11
  • 59
  • 87