Guest User

Untitled

a guest
May 16th, 2016
134
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  2. import org.apache.commons.math3.complex.Quaternion;
  3.  
  4. float ang = 0f;
  5. void setup() {
  6.   size(600, 600, P3D);
  7. }
  8.  
  9. void draw() {
  10.   fill(255);
  11.   //rect(0, 0, width, height);
  12.   translate(width/2, height/2);
  13.   Vector3D pt = new Vector3D(30, 120, 40);
  14.   Quaternion ptQ = new Quaternion(0, pt.getX(), pt.getY(), pt.getZ());
  15.   Vector3D axis = new Vector3D(50, 50, 50);
  16.   Quaternion q = getQuaternionFromAxisAngle(ang, axis);
  17.   Quaternion r = q.multiply(ptQ).multiply(q.getConjugate());
  18.  
  19.   stroke(255, 0, 0);
  20.   line(0, 0, 0, (float)r.getQ1(), (float)r.getQ2(), (float)r.getQ3());
  21.   ang += .1;
  22. }
  23.  
  24. Quaternion getQuaternionFromAxisAngle(float degAng, Vector3D vec) {
  25.   float radAng = radians(degAng);
  26.   // Here we calculate the sin( theta / 2) once for optimization
  27.   double factor = sin( radAng / 2.0 );
  28.  
  29.   // Calculate the x, y and z of the quaternion
  30.   double x = vec.getX() * factor;
  31.   double y = vec.getY() * factor;
  32.   double z = vec.getZ() * factor;
  33.  
  34.   // Calcualte the w value by cos( theta / 2 )
  35.   double w = cos( radAng / 2.0 );
  36.   return (new Quaternion(w, x, y, z)).normalize();
  37. }
RAW Paste Data