Documentation ¶
Overview ¶
Package quat provides the quaternion numeric type and functions.
For a good treatment of uses and behaviors of quaternions, see the interactive videos by Ben Eater and Grant Sanderson here https://eater.net/quaternions.
Example (Rotate) ¶
Rotate a cube 120° around the diagonal vector [1, 1, 1].
package main import ( "fmt" "math" "gonum.org/v1/gonum/floats/scalar" "gonum.org/v1/gonum/num/quat" ) // point is a 3-dimensional point/vector. type point struct { x, y, z float64 } // raise raises the dimensionality of a point to a quaternion. func raise(p point) quat.Number { return quat.Number{Imag: p.x, Jmag: p.y, Kmag: p.z} } // rotate performs the quaternion rotation of p by the given quaternion // and scaling by the scale factor. func rotate(p point, by quat.Number, scale float64) point { // Ensure the modulus of by is correctly scaled. if len := quat.Abs(by); len != scale { by = quat.Scale(math.Sqrt(scale)/len, by) } // Perform the rotation/scaling. pp := quat.Mul(quat.Mul(by, raise(p)), quat.Conj(by)) // Extract the point. return point{x: pp.Imag, y: pp.Jmag, z: pp.Kmag} } // Rotate a cube 120° around the diagonal vector [1, 1, 1]. func main() { alpha := 2 * math.Pi / 3 q := raise(point{1, 1, 1}) scale := 1.0 q = quat.Scale(math.Sin(alpha/2)/quat.Abs(q), q) q.Real += math.Cos(alpha / 2) for i, p := range []point{ {x: 0, y: 0, z: 0}, {x: 0, y: 0, z: 1}, {x: 0, y: 1, z: 0}, {x: 0, y: 1, z: 1}, {x: 1, y: 0, z: 0}, {x: 1, y: 0, z: 1}, {x: 1, y: 1, z: 0}, {x: 1, y: 1, z: 1}, } { pp := rotate(p, q, scale) // Clean up floating point error for clarity. pp.x = scalar.Round(pp.x, 2) pp.y = scalar.Round(pp.y, 2) pp.z = scalar.Round(pp.z, 2) fmt.Printf("%d %+v -> %+v\n", i, p, pp) } }
Output: 0 {x:0 y:0 z:0} -> {x:0 y:0 z:0} 1 {x:0 y:0 z:1} -> {x:1 y:0 z:0} 2 {x:0 y:1 z:0} -> {x:0 y:0 z:1} 3 {x:0 y:1 z:1} -> {x:1 y:0 z:1} 4 {x:1 y:0 z:0} -> {x:0 y:1 z:0} 5 {x:1 y:0 z:1} -> {x:1 y:1 z:0} 6 {x:1 y:1 z:0} -> {x:0 y:1 z:1} 7 {x:1 y:1 z:1} -> {x:1 y:1 z:1}
Index ¶
- func Abs(q Number) float64
- func IsInf(q Number) bool
- func IsNaN(q Number) bool
- type Number
- func Acos(q Number) Number
- func Acosh(q Number) Number
- func Add(x, y Number) Number
- func Asin(q Number) Number
- func Asinh(q Number) Number
- func Atan(q Number) Number
- func Atanh(q Number) Number
- func Conj(q Number) Number
- func Cos(q Number) Number
- func Cosh(q Number) Number
- func Exp(q Number) Number
- func Inf() Number
- func Inv(q Number) Number
- func Log(q Number) Number
- func Mul(x, y Number) Number
- func NaN() Number
- func Parse(s string) (Number, error)
- func Pow(q, r Number) Number
- func Scale(f float64, q Number) Number
- func Sin(q Number) Number
- func Sinh(q Number) Number
- func Sqrt(q Number) Number
- func Sub(x, y Number) Number
- func Tan(q Number) Number
- func Tanh(q Number) Number
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Number ¶
type Number struct {
Real, Imag, Jmag, Kmag float64
}
Number is a float64 precision quaternion.
func Parse ¶
Parse converts the string s to a Number. The string may be parenthesized and has the format [±]N±Ni±Nj±Nk. The order of the components is not strict.
Click to show internal directories.
Click to hide internal directories.