Added Matrix3x3F32 and Vector3F32.

Added shorthand for creating a PointF32.
This commit is contained in:
Sander Schobers 2018-07-18 20:05:06 +02:00
parent bdfa821989
commit 386eb28f11
3 changed files with 83 additions and 0 deletions

67
matrix3x3f32.go Normal file
View File

@ -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),
)
}

View File

@ -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}
}

11
vector3f32.go Normal file
View File

@ -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}
}