Skip to content

Commit

Permalink
Added toString method
Browse files Browse the repository at this point in the history
  • Loading branch information
HexagonNico committed Oct 29, 2023
1 parent ef83fe5 commit 524490e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ case class Quaternion(w: Double, x: Double, y: Double, z: Double) extends Double
def multipliedBy(l: Double): Quaternion = this * l

/**
* Returns the product between this quaternion and the quaternion `w + xi + yj + zk` as defined by the [[https://en.wikipedia.org/wiki/Quaternion#Hamilton_product Hamilton product]].
* Returns the product between this quaternion and the quaternion `w + xi + yj + zk` as defined by the Hamilton product.
*
* @param w The real/scalar part of the second operand of the multiplication
* @param x The first component of the vector part of the second operand of the multiplication
Expand All @@ -157,7 +157,7 @@ case class Quaternion(w: Double, x: Double, y: Double, z: Double) extends Double
)

/**
* Returns the product between this quaternion and the quaternion `w + xi + yj + zk` as defined by the [[https://en.wikipedia.org/wiki/Quaternion#Hamilton_product Hamilton product]].
* Returns the product between this quaternion and the quaternion `w + xi + yj + zk` as defined by the Hamilton product.
*
* This method can be used in place of the '*' operator for better interoperability with Java.
*
Expand All @@ -170,15 +170,15 @@ case class Quaternion(w: Double, x: Double, y: Double, z: Double) extends Double
def multiply(w: Double, x: Double, y: Double, z: Double): Quaternion = this * (w, x, y, z)

/**
* Returns the product between this quaternion and the given one as defined by the [[https://en.wikipedia.org/wiki/Quaternion#Hamilton_product Hamilton product]].
* Returns the product between this quaternion and the given one as defined by the Hamilton product.
*
* @param q The second operand of the multiplication
* @return The product between this quaternion and the given one
*/
def *(q: Quaternion): Quaternion = this * (q.w, q.x, q.y, q.z)

/**
* Returns the product between this quaternion and the given one as defined by the [[https://en.wikipedia.org/wiki/Quaternion#Hamilton_product Hamilton product]].
* Returns the product between this quaternion and the given one as defined by the Hamilton product.
*
* This method can be used in place of the '*' operator for better interoperability with Java.
*
Expand Down Expand Up @@ -247,11 +247,9 @@ case class Quaternion(w: Double, x: Double, y: Double, z: Double) extends Double
def length: Double = math.sqrt(this.lengthSquared)

/**
* Returns the [[https://en.wikipedia.org/wiki/Quaternion#Unit_quaternion versor]] of this quaternion. That is, this quaternion divided by its norm.
* Returns that is, this quaternion divided by its norm or [[length]].
*
* @see [[length]]
*
* @return The versor of this quaternion
* @return A unit quaternion
*/
def normalized: Quaternion = this / this.length

Expand Down Expand Up @@ -307,18 +305,18 @@ case class Quaternion(w: Double, x: Double, y: Double, z: Double) extends Double
def divide(w: Double, x: Double, y: Double, z: Double): Quaternion = this / (w, x, y, z)

/**
* Returns the [[https://en.wikipedia.org/wiki/Quaternion#Exponential,_logarithm,_and_power_functions exponential]] of this quaternion.
* Returns the exponential of this quaternion.
*
* @return The exponential of this quaternion
*/
def exp: Quaternion = {
val v = Vec3d(this.x, this.y, this.z)
val length = v.length
Quaternion(math.exp(this.w), math.cos(length) + v / v.length * math.sin(length))
Quaternion(math.cos(length), v / v.length * math.sin(length)) * math.exp(this.w)
}

/**
* Returns the [[https://en.wikipedia.org/wiki/Quaternion#Exponential,_logarithm,_and_power_functions logarithm]] of this quaternion.
* Returns the logarithm of this quaternion.
*
* @return The logarithm of this quaternion
*/
Expand Down Expand Up @@ -398,6 +396,34 @@ case class Quaternion(w: Double, x: Double, y: Double, z: Double) extends Double
* @return True if the components of this quaternion equal the given ones, otherwise false
*/
def equals(w: Double, x: Double, y: Double, z: Double): Boolean = this == (w, x, y, z)

/**
* Returns a string representation of this quaternion in the form `w + xi + yj + zk`.
*
* @return A string representation of this quaternion
*/
override def toString: String = {
val s = new StringBuilder(if (this.w == 0.0) "" else this.w.toString)
if (this.x != 0.0) {
if (s.nonEmpty) {
s ++= (if (this.x >= 0.0) " + " else " - ")
}
s ++= this.x.abs + "i"
}
if (this.y != 0.0) {
if (s.nonEmpty) {
s ++= (if (this.y >= 0.0) " + " else " - ")
}
s ++= this.y.abs + "j"
}
if (this.z != 0.0) {
if (s.nonEmpty) {
s ++= (if (this.z >= 0.0) " + " else " - ")
}
s ++= this.z.abs + "k"
}
s.toString
}
}

object Quaternion {
Expand Down Expand Up @@ -489,4 +515,4 @@ object Quaternion {
*/
def /(q: Quaternion): Quaternion = l * q.reciprocal
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ package io.github.hexagonnico.vecmatlib.quaternion
import org.scalatest.funsuite.AnyFunSuite

class QuaternionSuite extends AnyFunSuite {

test("Test") {
val q = Quaternion(4.0, 3.0, 0.0, -4.0)
println(q)
}
}

0 comments on commit 524490e

Please sign in to comment.