From 386eb28f113f2eb7161d561c2f8111a7625ef5a7 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 18 Jul 2018 20:05:06 +0200 Subject: [PATCH] Added Matrix3x3F32 and Vector3F32. Added shorthand for creating a PointF32. --- matrix3x3f32.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ pointf32.go | 5 ++++ vector3f32.go | 11 ++++++++ 3 files changed, 83 insertions(+) create mode 100644 matrix3x3f32.go create mode 100644 vector3f32.go diff --git a/matrix3x3f32.go b/matrix3x3f32.go new file mode 100644 index 0000000..91fd22e --- /dev/null +++ b/matrix3x3f32.go @@ -0,0 +1,67 @@ +package geom + +// Matrix3x3F32 is an matrix with 3 vectors, i, j and k (floating point, 32 bits). +type Matrix3x3F32 struct { + I Vector3F32 + J Vector3F32 + K Vector3F32 +} + +// Mx3x3F32 is a shorthand function to create a matrix. +func Mx3x3F32(i, j, k Vector3F32) Matrix3x3F32 { + return Matrix3x3F32{i, j, k} +} + +// ScaleV scales the matrix m by vector v. +func (m Matrix3x3F32) ScaleV(v Vector3F32) Matrix3x3F32 { + return Matrix3x3F32{ + Vec3F32(v.X*m.I.X, v.X*m.I.Y, v.X*m.I.Z), + Vec3F32(v.Y*m.I.X, v.Y*m.I.Y, v.Y*m.I.Z), + Vec3F32(v.Z*m.I.X, v.Z*m.I.Y, v.Z*m.I.Z), + } +} + +// MulV multiplies the matrix m with vector v. +func (m Matrix3x3F32) MulV(v Vector3F32) Vector3F32 { + return Vec3F32( + v.X*m.I.X+v.Y*m.J.X+v.Z*m.K.X, + v.X*m.I.Y+v.Y*m.J.Y+v.Z*m.K.Y, + v.X*m.I.Z+v.Y*m.J.Z+v.Z*m.K.Z) +} + +// MulM multiplies the matrix m with matrix n. +func (m Matrix3x3F32) MulM(n Matrix3x3F32) Matrix3x3F32 { + return Matrix3x3F32{ + Vec3F32( + n.I.X*m.I.X+n.I.Y*m.J.X+n.I.Z*m.K.X, + n.I.X*m.I.Y+n.I.Y*m.J.Y+n.I.Z*m.K.Y, + n.I.X*m.I.Z+n.I.Y*m.J.Z+n.I.Z*m.K.Z), + Vec3F32( + n.J.X*m.I.X+n.J.Y*m.J.X+n.J.Z*m.K.X, + n.J.X*m.I.Y+n.J.Y*m.J.Y+n.J.Z*m.K.Y, + n.J.X*m.I.Z+n.J.Y*m.J.Z+n.J.Z*m.K.Z), + Vec3F32( + n.K.X*m.I.X+n.K.Y*m.J.X+n.K.Z*m.K.X, + n.K.X*m.I.Y+n.K.Y*m.J.Y+n.K.Z*m.K.Y, + n.K.X*m.I.Z+n.K.Y*m.J.Z+n.K.Z*m.K.Z), + } +} + +// Det calculates the determinant of the matrix m. +func (m Matrix3x3F32) Det() float32 { + return m.I.X*m.J.Y*m.K.Z + + m.J.X*m.K.Y*m.I.Z + + m.K.X*m.I.Y*m.J.Z - + m.K.X*m.J.Y*m.I.Z - + m.J.X*m.I.Y*m.K.Z - + m.I.X*m.K.Y*m.J.Z +} + +// Adj calculates the adjugate matrix of matrix m. +func (m Matrix3x3F32) Adj() Matrix3x3F32 { + return Mx3x3F32( + Vec3F32(m.J.Y*m.K.Z-m.K.Y*m.J.Z, m.K.Y*m.I.Z-m.I.Y*m.K.Z, m.I.Y*m.J.Z-m.J.Y*m.I.Z), + Vec3F32(m.K.X*m.J.Z-m.J.X*m.K.Z, m.I.X*m.K.Z-m.K.X*m.I.Z, m.J.X*m.I.Z-m.I.X*m.J.Z), + Vec3F32(m.J.X*m.K.Y-m.K.X*m.J.Y, m.K.X*m.I.Y-m.I.X*m.K.Y, m.I.X*m.J.Y-m.J.X*m.I.Y), + ) +} diff --git a/pointf32.go b/pointf32.go index 5bb2096..3fd0a87 100644 --- a/pointf32.go +++ b/pointf32.go @@ -4,3 +4,8 @@ package geom type PointF32 struct { X, Y float32 } + +// PtF32 is a shorthand function to create a point. +func PtF32(x, y float32) PointF32 { + return PointF32{x, y} +} diff --git a/vector3f32.go b/vector3f32.go new file mode 100644 index 0000000..cbe8717 --- /dev/null +++ b/vector3f32.go @@ -0,0 +1,11 @@ +package geom + +// Vector3F32 is a vector of size 3 (floating point, 32 bits). +type Vector3F32 struct { + X, Y, Z float32 +} + +// Vec3F32 is a shorthand function to create a vector. +func Vec3F32(x, y, z float32) Vector3F32 { + return Vector3F32{x, y, z} +}