Created
September 18, 2020 16:53
-
-
Save theAlgorithmist/8d7efda7e3b9cd82e5f9ad4d6291a606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TSMT$Quaternion } from './Quaternion'; | |
import { Q } from '../definitions/Q'; | |
export class QCalculations | |
{ | |
protected static readonly Q1: TSMT$Quaternion = new TSMT$Quaternion(); | |
protected static readonly Q2: TSMT$Quaternion = new TSMT$Quaternion(); | |
constructor() | |
{ | |
// empty | |
} | |
/** | |
* Add two quaternions | |
* | |
* @param q1 4-tuple representing first input quaternion | |
* | |
* @param q2 4=tuple representing second input quaternion | |
*/ | |
public static add(q1: Q, q2: Q): Q | |
{ | |
QCalculations.Q1.fromArray(q1.w, q1.i, q1.j, q1.k); | |
QCalculations.Q2.fromArray(q2.w, q2.i, q2.j, q2.k); | |
QCalculations.Q1.add(QCalculations.Q2); | |
const values: Array<number> = QCalculations.Q1.toArray(); | |
return new Q(values[0], values[1], values[2], values[3]); | |
} | |
/** | |
* Subtract two quaternions | |
* | |
* @param q1 4-tuple representing first input quaternion | |
* | |
* @param q2 4=tuple representing second input quaternion | |
*/ | |
public static subtract(q1: Q, q2: Q): Q | |
{ | |
QCalculations.Q1.fromArray(q1.w, q1.i, q1.j, q1.k); | |
QCalculations.Q2.fromArray(q2.w, q2.i, q2.j, q2.k); | |
QCalculations.Q1.subtract(QCalculations.Q2); | |
const values: Array<number> = QCalculations.Q1.toArray(); | |
return new Q(values[0], values[1], values[2], values[3]); | |
} | |
/** | |
* Mutiply two quaternions | |
* | |
* @param q1 4-tuple representing first input quaternion | |
* | |
* @param q2 4=tuple representing second input quaternion | |
*/ | |
public static multiply(q1: Q, q2: Q): Q | |
{ | |
QCalculations.Q1.fromArray(q1.w, q1.i, q1.j, q1.k); | |
QCalculations.Q2.fromArray(q2.w, q2.i, q2.j, q2.k); | |
QCalculations.Q1.multiply(QCalculations.Q2); | |
const values: Array<number> = QCalculations.Q1.toArray(); | |
return new Q(values[0], values[1], values[2], values[3]); | |
} | |
/** | |
* Divide two quaternions | |
* | |
* @param q1 4-tuple representing first input quaternion | |
* | |
* @param q2 4=tuple representing second input quaternion | |
*/ | |
public static divide(q1: Q, q2: Q): Q | |
{ | |
QCalculations.Q1.fromArray(q1.w, q1.i, q1.j, q1.k); | |
QCalculations.Q2.fromArray(q2.w, q2.i, q2.j, q2.k); | |
QCalculations.Q1.divide(QCalculations.Q2); | |
const values: Array<number> = QCalculations.Q1.toArray(); | |
return new Q(values[0], values[1], values[2], values[3]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment