Skip to content

Instantly share code, notes, and snippets.

@micahcatlin
Last active December 18, 2015 10:19
Show Gist options
  • Select an option

  • Save micahcatlin/5768206 to your computer and use it in GitHub Desktop.

Select an option

Save micahcatlin/5768206 to your computer and use it in GitHub Desktop.
Coffeescript code block which produces different output on firefox and chrome
# http://jsfiddle.net/YwqsV/
fromAngleAxis = (angle, axis, dest) ->
axis = normalize axis
halfAngle = angle / 2
s = Math.sin halfAngle
dest[0] = axis[0] * s
dest[1] = axis[1] * s
dest[2] = axis[2] * s
dest[3] = Math.cos halfAngle
return dest
magnitudeSquared = (u) ->
sum = 0
for i in [0..3]
sum += u[i] * u[i]
return sum
magnitude = (u) ->
return Math.sqrt(magnitudeSquared(u))
normalize = (u) ->
mag = magnitude(u)
return u if mag == 0
length = 1 / mag
for i in [0..3]
u[i] *= length
return u
mult = (u, v, dest) ->
# [x, y, z] are the imaginary parts and w is the real part.
ux = u[0]
uy = u[1]
uz = u[2]
uw = u[3]
vx = v[0]
vy = v[1]
vz = v[2]
vw = v[3]
dest[0] = ux * vw + uw * vx + uy * vz - uz * vy
dest[1] = uy * vw + uw * vy + uz * vx - ux * vz
dest[2] = uz * vw + uw * vz + ux * vy - uy * vx
dest[3] = uw * vw - ux * vx - uy * vy - uz * vz
return dest
q1 = []
q2 = []
q3 = []
fromAngleAxis(20, [0, 0, 1], q1)
fromAngleAxis(20, [0, 0, 1], q2)
mult(q1, q2, q3)
console.log q3[0], q3[1], q3[2], q3[3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment