geom

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2022 License: BSD-3-Clause Imports: 3 Imported by: 1

README

geom a package for 3D Euclidean geometry

Overview

This package provides routines for managing 3D vectors and matrices and their various transformations.

API

The API provided by this package is avalble using go doc zappem.net/pub/math/geom. It can also be browsed on the go.dev website: package zappem.net/pub/math/geom.

License info

The geom package is distributed with the same BSD 3-clause license as that used by golang itself.

Reporting bugs

Use the github geom bug tracker.

Documentation

Overview

Package geom implements matrix math for 3D vectors and matrices.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNormalNotPossible     = errors.New("normal not possible")
	ErrNotValidMatrix        = errors.New("not a 3x3 matrix")
	ErrSingular              = errors.New("too close to singular")
	ErrNoNormalForZeroVector = errors.New("no non-collinear of zero vector")
	ErrIsIdentity            = errors.New("too close to identity")
)

ErrNormalNotPossible etc are errors returned by this package.

View Source
var I = M(1)

I is the identity matrix.

View Source
var ZeroM = M()

ZeroM is the zero matrix.

View Source
var ZeroV = V()

ZeroV is a zero vector.

Functions

func DefineZeroish

func DefineZeroish(z float64)

DefineZeroish sets the zeroEnough scale. It should be called with a positive value to have any effect. If called with zero or a negative number, it prevents the Zeroish() funcion from recognizing near zero. Zeroish defaults to treating 10^-7 as close enough to zero.

func RefZero

func RefZero() float64

RefZero returns the current zero-enough value in use by the geom package.

func Zeroish

func Zeroish(a float64) bool

Zeroish confirms that two numbers are close enough to zero. The tolerance is set via DefineZeroish.

Types

type Angle

type Angle float64

Angle represents an angle.

func Degrees

func Degrees(d float64) Angle

Degrees returns an angle of the specified degrees.

func Radians

func Radians(r float64) Angle

Radians returns an angle of the specified degrees.

func (Angle) C

func (a Angle) C() float64

C returns the cosine of an angle.

func (Angle) Deg

func (a Angle) Deg() float64

Deg returns an angle in degrees.

func (Angle) LikeCos

func (a Angle) LikeCos() Angle

LikeCos returns the angle that shares a cosine with a.

func (Angle) LikeSin

func (a Angle) LikeSin() Angle

LikeSin returns the angle that shares a sine with a.

func (Angle) LikeTan

func (a Angle) LikeTan() Angle

LikeTan returns an angle that is 180 degrees opposed to a. If a is non-positive, we add 180 deg, otherwise, we subtract 180 deg.

func (Angle) Rad

func (a Angle) Rad() float64

Rad returns an angle in radians.

func (Angle) S

func (a Angle) S() float64

S returns the sine of an angle.

func (Angle) String

func (a Angle) String() string

String displays an angle in degrees rounded to 2 decimal places.

func (Angle) T

func (a Angle) T() float64

T returns the tangent of an angle.

type Matrix

type Matrix []float64

Matrix is a square 3x3 matrix type.

func M

func M(v ...float64) Matrix

M defines a matrix. It takes a variable number of arguments, if the number of arguments is:

1 - create a matrix with this value used by the elements on the
    trace.

3 - create a matrix with these values used to fill the trace elements.

9 - each element is specified, left to right, top to bottom.

other - start filling at [0][0], walking rows left to right until
        out of elements.

func RX

func RX(a Angle) Matrix

RX returns a right-hand rotation matrix around the X axis of angle a.

func RY

func RY(a Angle) Matrix

RY returns a right-hand rotation matrix around the Y axis of angle a.

func RZ

func RZ(a Angle) Matrix

RZ returns a right-hand rotation matrix around the Z axis of angle a.

func (Matrix) Add

func (m Matrix) Add(n Matrix) Matrix

Add adds two matrices.

func (Matrix) AddS

func (m Matrix) AddS(n Matrix, s float64) Matrix

AddS adds two matrices scaling the n by s.

func (Matrix) Det

func (m Matrix) Det() float64

Det returns the determinant of a matrix.

func (Matrix) Eigen

func (m Matrix) Eigen() (s float64, v Vector, a Angle, err error)

Eigen decomposes a matrix into its determinant (s), its eigenvector (v) and an angle (a) which is the positive angle or rotation around v that the matrix represents.

func (Matrix) Equals

func (m Matrix) Equals(n Matrix) bool

Equals confirms two matrices are close enough to equal.

func (Matrix) Inv

func (m Matrix) Inv() (Matrix, error)

Inv returns the inverse of a matrix. We use the Gauss-Jordan method to find the inverse.

func (Matrix) Scale

func (m Matrix) Scale(s float64) Matrix

Scale multiplies a matrix by a scalar, s.

func (Matrix) Sub

func (m Matrix) Sub(n Matrix) Matrix

Sub computes the difference of two matrices.

func (Matrix) Transpose

func (m Matrix) Transpose() Matrix

Transpose returns the transpose of a matrix.

func (Matrix) X

func (m Matrix) X() Vector

X returns the X vector column of a matrix.

func (Matrix) XM

func (m Matrix) XM(n Matrix) Matrix

Multiply two matrices, allocating a new one.

func (Matrix) XV

func (m Matrix) XV(v Vector) Vector

Multiply a vector by a matrix.

func (Matrix) Y

func (m Matrix) Y() Vector

Y returns the Y vector column of a matrix.

func (Matrix) Z

func (m Matrix) Z() Vector

Z returns the Z vector column of a matrix.

type Vector

type Vector []float64

Vector is a column vector with 3 elements.

func V

func V(v ...float64) Vector

V defines a vector, explicitly specifying the elements in 0 to 2 order.

func X

func X(s float64) Vector

X returns an X vector of the specified length.

func Y

func Y(s float64) Vector

Y returns an Y vector of the specified length.

func Z

func Z(s float64) Vector

Z returns an Z vector of the specified length.

func (Vector) Add

func (v Vector) Add(u Vector) Vector

Add adds two vectors (u) and (v).

func (Vector) AddS

func (v Vector) AddS(u Vector, s float64) Vector

AddS adds a scaled (s) vector (u) to v and allocates a new result.

func (Vector) Cross

func (v Vector) Cross(u Vector) Vector

Cross returns the cross-product of two vectors. It returns a vector perpendicular to both u and v. It returns v x u using right hand rule.

func (Vector) Dot

func (v Vector) Dot(u Vector) float64

Dot returns the scalar dot product of two vectors.

func (Vector) Equals

func (v Vector) Equals(u Vector) bool

Equals confirms two vectors are close enough to equal.

func (Vector) NonCollinear

func (v Vector) NonCollinear() (u Vector, err error)

NonCollinear returns a vector that is not a scalar multiple of the input.

func (Vector) Normalize

func (v Vector) Normalize() (u Vector, err error)

Normalize attempts to scale v to return a unit vector parallel to v.

func (Vector) R

func (v Vector) R() float64

R returns the scalar length of a vector.

func (Vector) RV

func (v Vector) RV(a Angle) (m Matrix, err error)

RV returns a rotation matrix for an angle (a) around vector axis (v). The axis vector (v) need not be of unit length. This function uses the Rodrigues rotation formula.

func (Vector) Scale

func (v Vector) Scale(s float64) Vector

Scale maginfies a vector, v, by scale, s.

func (Vector) Sub

func (v Vector) Sub(u Vector) Vector

Sub subtracts vector (u) from (v).

Jump to

Keyboard shortcuts

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