クオータニオンを JavaScript で!!
クオータニオンを JavaScript で!!
コンピュータ・グラフィックを計算すときに、回転は付きものだ。
勉強しているうちに「クオータニオンで回転」が出てきた。
まず、それを学んだ結果を記録する
クオータニオンとは
クオータニオンの演算について
クオータニオンの一般系
クオータニオンのノルム
クオータニオンの積の逆元
class Quaternion { constructor (x = 0, y = 0, z = 0, w = 0) { this.x = x; this.y = y; this.z = z; this.w = w; } add ({x, y, z, w}) { this.x += x; this.y += y; this.z += z; this.w += w; return this; } multi ({x, y, z, w}) { let {x: x_, y: y_, z: z_, w: w_} = this; this.x = w_*x + x_*w + y_*z + z_*y; this.y = w_*y - x_*z + y_*w + z_*x; this.z = w_*z + x_*y - y_*x + z_*w; this.w = w_*w - x_*x - y_*y - z_*z; return this; } norm () { return Math.sqrt (this.x**2 + this.y**2 + this.z**2 + this.z**2); } inverse () { let n = this.x**2 + this.y**2 + this.z**2 + this.z**2; return new Quaternion (this.x / -n, this.y / -n, this.z / -n, this.w / n); } }