matrix

package
v0.0.0-...-1b60a0e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package matrix represents a matrix of a given dimension. It provides commonly used matrix operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckInBounds

func CheckInBounds(m *Matrix, row, col uint) error

CheckInBounds returns an error if either the row or column values are out of bounds of the passed Matrix.

func Cofactor

func Cofactor(m *Matrix, row, col uint) (float64, error)

Cofactor returns the cofactor of the submatrix. If the passed row or col are not in bounds of the passed Matrix, then an error is returned.

func Determinant

func Determinant(m *Matrix) (float64, error)

Determinant calculate and returns the determinant of the passed Matrix. If the passed Matrix is not a square matrix, then an error is returned.

func IsInvertible

func IsInvertible(m *Matrix) bool

IsInvertible returns true if the passed Matrix is invertible. The passed Matrix is invertible if it's determinant is equal to 0.

func MatrixToPoint

func MatrixToPoint(m *Matrix) (*point.Point, error)

MatrixToPoint returns a Point representation of the passed Matrix. An error is returned if the passed Matrix is not of a 3x1 or 4x1 dimension.

func MatrixToVector

func MatrixToVector(m *Matrix) (*vector.Vector, error)

MatrixToVector returns a Point representation of the passed Matrix. An error is returned if the passed Matrix is not of a 3x1 or 4x1 dimension.

func Minor

func Minor(m *Matrix, row, col uint) (float64, error)

Minor returns the determinant of the submatrix. If the passed row or col are not in bounds of the passed Matrix, then an error is returned.

Types

type Matrix

type Matrix struct {
	// contains filtered or unexported fields
}

Matrix represents an n-dimensional grid of floating point numbers.

func Inverse

func Inverse(m *Matrix) (*Matrix, error)

Inverse returns the inverse of the passed Matrix.

func Multiply

func Multiply(m1, m2 *Matrix) (*Matrix, error)

Multiply returns a new Matrix that is the result of multiplying the passed matrices. If the column length in m1 is not equal to the row length in m2, an error is returned.

func Multiply4x4

func Multiply4x4(m1, m2 *Matrix) *Matrix

Multiply4x4 returns a new Matrix that is the result of multiplying the passed 4x4 matrices. The passed matrices are assumed to be of 4x4 order.

func NewIdentityMatrix

func NewIdentityMatrix(size uint) *Matrix

NewIdentityMatrix returns a new identity Matrix having row and column length equal to the passed size.

func NewMatrix

func NewMatrix(rows, cols uint) *Matrix

NewMatrix returns a new Matrix having the passed row and column lengths.

func NewScalingMatrix

func NewScalingMatrix(x, y, z float64) *Matrix

NewScalingMatrix returns a new 4x4 scaling Matrix.

The scaling Matrix returned has the form:

| x 0 0 0 |
| 0 y 0 0 |
| 0 0 z 0 |
| 0 0 0 1 |

func NewShearingMatrix

func NewShearingMatrix(xy, xz, yx, yz, zx, zy float64) *Matrix

NewShearingMatrix returns a new 4x4 shearing Matrix that can be used for a shear transformation of a Point.

The shearing Matrix returned has the form:

| 1  xy xz 0 |
| yx 1  yz 0 |
| zx zy 1  0 |
| 0  0  0  1 |

func NewTranslationMatrix

func NewTranslationMatrix(x, y, z float64) *Matrix

NewTranslationMatrix returns a new 4x4 translation Matrix.

The translation Matrix returned has the form:

| 1 0 0 x |
| 0 1 0 y |
| 0 0 1 z |
| 0 0 0 1 |

func NewXRotationMatrix

func NewXRotationMatrix(radians float64) *Matrix

NewXRotationMatrix returns a new 4x4 rotation Matrix that can be used to rotate a Point or Vector around the X axis by the passed number of radians.

The rotation Matrix returned has the form:

| 1 0      0       0 |
| 0 cos(r) -sin(r) 0 |
| 0 sin(r) cos(r)  0 |
| 0 0      0       1 |

func NewYRotationMatrix

func NewYRotationMatrix(radians float64) *Matrix

NewYRotationMatrix returns a new 4x4 rotation Matrix that can be used to rotate a Point or Vector around the Y axis by the passed number of radians.

The rotation Matrix returned has the form:

| cos(r)  0 sin(r) 0 |
| 0       1 0      0 |
| -sin(r) 0 cos(r) 0 |
| 0       0 0      1 |

func NewZRotationMatrix

func NewZRotationMatrix(radians float64) *Matrix

NewZRotationMatrix returns a new 4x4 rotation Matrix that can be used to rotate a Point or Vector around the z-axis by the passed number of radians.

The rotation Matrix returned has the form:

| cos(r) -sin(r) 0 0 |
| sin(r) cos(r)  0 0 |
| 0      0       1 0 |
| 0      0       0 1 |

func PointToMatrix

func PointToMatrix(pt *point.Point) *Matrix

PointToMatrix returns a 4x1 Matrix that represents the passed Point. The returned Matrix is known as a 'column vector' in linear algebra.

func Submatrix

func Submatrix(m *Matrix, row, col uint) (*Matrix, error)

Submatrix returns a new Matrix that is the result of removing the passed row and column index from the passed Matrix. If the passed row or col are not in bounds of the passed Matrix, then an error is returned.

func Transpose

func Transpose(m Matrix) *Matrix

Transpose returns a new Matrix that is the result of transposing the passed Matrix. Transposing a Matrix turns the nth row into the nth column in the resulting Matrix.

func VectorToMatrix

func VectorToMatrix(vec *vector.Vector) *Matrix

VectorToMatrix returns a 4x1 Matrix that represents the passed Vector. The returned Matrix is known as a 'column vector' in linear algebra.

func ViewTransform

func ViewTransform(from, to point.Point, up vector.Vector) *Matrix

ViewTransform returns a transformation matrix that can be used to transform a camera view in a scene.

The from parameter specifies where the eye is at in the scene. The to parameter specifies the point in the scene at which to look at. The up parameter specifies which direction is up.

func (*Matrix) Equals

func (m *Matrix) Equals(m1 *Matrix) bool

Equals returns true if this Matrix has identical rows, columns, and elements as the passed Matrix.

func (*Matrix) GetCols

func (m *Matrix) GetCols() uint

GetCols returns the number of columns that the Matrix has.

func (*Matrix) GetRows

func (m *Matrix) GetRows() uint

GetRows returns the number of rows that the Matrix has.

func (*Matrix) GetValue

func (m *Matrix) GetValue(row, col uint) (float64, error)

GetValue sets the passed value at the passed row and column in the Matrix.

func (*Matrix) RotateX

func (m *Matrix) RotateX(radians float64) *Matrix

RotateX rotates this Matrix around the x-axis by the passed number of radians. Rotation happens clockwise when looking down the positive x-axis towards the negative x-axis.

func (*Matrix) RotateY

func (m *Matrix) RotateY(radians float64) *Matrix

RotateY rotates this Matrix around the y-axis by the passed number of radians. Rotation happens clockwise when looking down the positive y-axis towards the negative y-axis.

func (*Matrix) RotateZ

func (m *Matrix) RotateZ(radians float64) *Matrix

RotateZ rotates this Matrix around the z-axis by the passed number of radians. Rotation happens clockwise when looking down the positive z-axis towards the negative z-axis.

func (*Matrix) Scale

func (m *Matrix) Scale(x, y, z float64) *Matrix

Scale scales this Matrix by the passed x, y, and z values.

func (*Matrix) SetValue

func (m *Matrix) SetValue(row, col uint, val float64) error

SetValue sets the passed value at the passed row and column in the Matrix.

func (*Matrix) Shear

func (m *Matrix) Shear(xy, xz, yx, yz, zx, zy float64) *Matrix

Shear shears or skews this Matrix in on coordinate relative to another coordinate. For example, the parameter xy represents how much to shear x relative to y.

func (*Matrix) Translate

func (m *Matrix) Translate(x, y, z float64) *Matrix

Translate translates this Matrix by the passed x, y, and z values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL