quaternion-java/fr/alnotz/Quaternion.java

136 lines
4.6 KiB
Java

package fr.alnotz;
public class Quaternion implements IsQuaternion{
private double s;
private double i;
private double j;
private double k;
public Quaternion(){
this.s = 0;
this.i = 0;
this.j = 0;
this.k = 0;
}
public Quaternion(double s, double i, double j, double k){
this.s = s;
this.i = i;
this.j = j;
this.k = k;
}
public static Quaternion plus(IsQuaternion quaternion0, IsQuaternion quaternion1){
return new Quaternion(quaternion0.getS() + quaternion1.getS(),
quaternion0.getI() + quaternion1.getI(),
quaternion0.getJ() + quaternion1.getJ(),
quaternion0.getK() + quaternion1.getK());
}
public static Quaternion times(IsQuaternion quaternion0, IsQuaternion quaternion1){
/*
* (a0 + b0i + c0j + d0k)(a1 + b1i + c1j + d1k)
* (a0a1 - b0b1 - c0c1 - d0d1) +
* (a0b1 + b0a1 + c0d1 - d0c1)i +
* (a0c1 - b0d1 + c0a1 + d0b1)j +
* (a0d1 + b0c1 - c0b1 + d0a1)k
*/
double newS = quaternion0.getS() * quaternion1.getS() -
quaternion1.getI() * quaternion1.getI() -
quaternion0.getJ() * quaternion1.getJ() -
quaternion0.getK() * quaternion1.getK();
double newI = quaternion0.getS() * quaternion1.getI() +
quaternion0.getI() * quaternion1.getK() +
quaternion0.getJ() * quaternion1.getS() -
quaternion0.getK() * quaternion1.getJ();
double newJ = quaternion0.getS() * quaternion1.getJ() -
quaternion0.getI() * quaternion1.getK() +
quaternion0.getJ() * quaternion1.getS() +
quaternion0.getK() * quaternion1.getI();
double newK = quaternion0.getS() * quaternion1.getK() +
quaternion0.getI() * quaternion1.getJ() -
quaternion0.getJ() * quaternion1.getI() +
quaternion0.getK() * quaternion1.getS();
return new Quaternion(newS, newI, newJ, newK);
}
public static Quaternion conjugate(IsQuaternion quaternion) {
return new Quaternion(quaternion.getS(),
-1 * quaternion.getI(),
-1 * quaternion.getJ(),
-1 * quaternion.getK());
}
@Override
public double getS() {
return this.s;
}
@Override
public double getI() {
return this.i;
}
@Override
public double getJ() {
return this.j;
}
@Override
public double getK() {
return this.k;
}
@Override
public void setS(double s) {
this.s = s;
}
@Override
public void setI(double i) {
this.i = i;
}
@Override
public void setJ(double j) {
this.j = j;
}
@Override
public void setK(double k) {
this.k = k;
}
@Override
public Quaternion plus(IsQuaternion quaternion) {
return new Quaternion(this.s + quaternion.getS(),
this.i + quaternion.getI(),
this.j + quaternion.getJ(),
this.k + quaternion.getK());
}
@Override
public Quaternion times(IsQuaternion quaternion) {
/*
* (a0 + b0i + c0j + d0k)(a1 + b1i + c1j + d1k)
* (a0a1 - b0b1 - c0c1 - d0d1) +
* (a0b1 + b0a1 + c0d1 - d0c1)i +
* (a0c1 - b0d1 + c0a1 + d0b1)j +
* (a0d1 + b0c1 - c0b1 + d0a1)k
*/
double newS = this.s * quaternion.getS() -
this.i * quaternion.getI() -
this.j * quaternion.getJ() -
this.k * quaternion.getK();
double newI = this.s * quaternion.getI() +
this.i * quaternion.getK() +
this.j * quaternion.getS() -
this.k * quaternion.getJ();
double newJ = this.s * quaternion.getJ() -
this.i * quaternion.getK() +
this.j * quaternion.getS() +
this.k * quaternion.getI();
double newK = this.s * quaternion.getK() +
this.i * quaternion.getJ() -
this.j * quaternion.getI() +
this.k * quaternion.getS();
return new Quaternion(newS, newI, newJ, newK);
}
@Override
public Quaternion conjugate() {
return new Quaternion(this.s, -1 * this.i, -1 * this.j, -1 * this.k);
}
@Override
public boolean equals(IsQuaternion quaternion) {
return false;
}
@Override
public String toString(){
return String.format("%+f %+fi %+fj %+fk\n", this.s, this.i, this.j, this.k);
}
}