# Rotation of a point around axis using quaternions

I have a simple processing program to rotate a point around arbitrary axis but it stops after a bit. I'm trying to get full rotation of the point around the axis.

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math3.complex.Quaternion;

float ang = 0f;
void setup() {
size(600, 600, P3D);
}

void draw() {
fill(255);
//rect(0, 0, width, height);
translate(width/2, height/2);
Vector3D pt = new Vector3D(30, 145, 40);
Quaternion ptQ = new Quaternion(0, pt.getX(), pt.getY(), pt.getZ());
Vector3D axis = new Vector3D(50, 50, 50);
Quaternion q = getQuaternionFromAxisAngle(ang, axis);
Quaternion r = q.multiply(ptQ).multiply(q.getConjugate());

stroke(255, 0, 0);
line(0, 0, 0, (float)r.getQ1(), (float)r.getQ2(), (float)r.getQ3());
ang += .1;
println(r.getQ1() + " " + r.getQ2() + " " + r.getQ3());
}

Quaternion getQuaternionFromAxisAngle(float degAng, Vector3D vec) {

float radAng = radians(degAng/2.0);
// Here we calculate the sin( theta / 2) once for optimization
double factor = sin( radAng);

// Calculate the x, y and z of the quaternion
double x = vec.getX() * factor;
double y = vec.getY() * factor;
double z = vec.getZ() * factor;

// Calcualte the w value by cos( theta / 2 )
double w = cos( radAng);
return (new Quaternion(w, x, y, z)).normalize();
}

• it does rotate but it is slow when rotation traverses to half – John Smith May 17 '16 at 6:44
• Is there a reason why you are importing apache's Vector3D and Quaternion instead of using Unity's own Quaternion and Vector3? – Sheikz May 17 '16 at 14:28
• @Sheikz This appears to be a Processing question, not a Unity question. – The Guy with The Hat May 17 '16 at 15:11
• yeah, this isn't unity question, I was looking for conceptual correctness for a simple code piece like this. – John Smith May 17 '16 at 17:29
• Do I have to convert result quaternion to something else before plotting ? – John Smith May 18 '16 at 0:06