vec2

package
v0.0.0-...-aed5d82 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 4 Imported by: 28

Documentation

Overview

Package vec2 contains a 2D float64 vector type T and functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Zero holds a zero vector.
	Zero = T{}

	// UnitX holds a vector with X set to one.
	UnitX = T{1, 0}
	// UnitY holds a vector with Y set to one.
	UnitY = T{0, 1}
	// UnitXY holds a vector with X and Y set to one.
	UnitXY = T{1, 1}

	// MinVal holds a vector with the smallest possible component values.
	MinVal = T{-math.MaxFloat64, -math.MaxFloat64}
	// MaxVal holds a vector with the highest possible component values.
	MaxVal = T{+math.MaxFloat64, +math.MaxFloat64}
)

Functions

func Angle

func Angle(a, b *T) float64

Angle returns the angle between two vectors.

func Distance

func Distance(a, b *T) float64

Distance between two vectors

func Dot

func Dot(a, b *T) float64

Dot returns the dot product of two vectors.

func IsLeftWinding

func IsLeftWinding(a, b *T) bool

IsLeftWinding returns if the angle from a to b is left winding.

func IsRightWinding

func IsRightWinding(a, b *T) bool

IsRightWinding returns if the angle from a to b is right winding.

func PointSegmentDistance

func PointSegmentDistance(p1 *T, x1 *T, x2 *T) float64

func SquareDistance

func SquareDistance(a, b *T) float64

Squared Distance between two vectors

Types

type Rect

type Rect struct {
	Min T
	Max T
}

Rect is a coordinate system aligned rectangle defined by a Min and Max vector.

func Joined

func Joined(a, b *Rect) (rect Rect)

Joined returns the minimal rectangle containing both a and b.

func NewRect

func NewRect(a, b *T) (rect Rect)

NewRect creates a Rect from two points.

func ParseRect

func ParseRect(s string) (r Rect, err error)

ParseRect parses a Rect from a string. See also String()

func (*Rect) Area

func (rect *Rect) Area() float64

Area calculates the area of the rectangle.

func (*Rect) Array

func (rect *Rect) Array() *[4]float64

func (*Rect) Center

func (rect *Rect) Center() T

func (*Rect) Contains

func (rect *Rect) Contains(other *Rect) bool

Contains returns if other Rect is contained within the rectangle.

func (*Rect) ContainsPoint

func (rect *Rect) ContainsPoint(p *T) bool

ContainsPoint returns if a point is contained within the rectangle.

func (*Rect) Extend

func (rect *Rect) Extend(p *T)

func (*Rect) Height

func (rect *Rect) Height() float64

func (*Rect) Intersects

func (rect *Rect) Intersects(other *Rect) bool

func (*Rect) Join

func (rect *Rect) Join(other *Rect)

Join enlarges this rectangle to contain also the given rectangle.

func (*Rect) Size

func (rect *Rect) Size() float64

func (*Rect) Slice

func (rect *Rect) Slice() []float64

Slice returns the elements of the vector as slice.

func (*Rect) String

func (rect *Rect) String() string

String formats Rect as string. See also ParseRect().

func (*Rect) Width

func (rect *Rect) Width() float64

type T

type T [2]float64

T represents a 2D vector.

func Add

func Add(a, b *T) T

Add returns the sum of two vectors.

func Cross

func Cross(a, b *T) T

Cross returns the cross product of two vectors.

func From

func From(other generic.T) T

From copies a T from a generic.T implementation.

func Interpolate

func Interpolate(a, b *T, t float64) T

Interpolate interpolates between a and b at t (0,1).

func Max

func Max(a, b *T) T

Max returns the component wise maximum of two vectors.

func Min

func Min(a, b *T) T

Min returns the component wise minimum of two vectors.

func Mul

func Mul(a, b *T) T

Mul returns the component wise product of two vectors.

func Parse

func Parse(s string) (r T, err error)

Parse parses T from a string. See also String()

func PointSegmentVerticalPoint

func PointSegmentVerticalPoint(p1 *T, x1 *T, x2 *T) *T

func Sub

func Sub(a, b *T) T

Sub returns the difference of two vectors.

func (*T) Add

func (vec *T) Add(v *T) *T

Add adds another vector to vec.

func (T) AlmostEqual

func (vec T) AlmostEqual(o T, tol float64) bool

AlmostEqual returns true if vec and o are equal allowing for numerical error tol.

func (T) Angle

func (vec T) Angle() float64

Angle returns the counter-clockwise angle of the vector from the x axis.

func (*T) Clamp

func (vec *T) Clamp(min, max *T) *T

Clamp clamps the vector's components to be in the range of min to max.

func (*T) Clamp01

func (vec *T) Clamp01() *T

Clamp01 clamps the vector's components to be in the range of 0 to 1.

func (T) Clamped

func (vec T) Clamped(min, max *T) T

Clamped returns a copy of the vector with the components clamped to be in the range of min to max.

func (T) Clamped01

func (vec T) Clamped01() T

Clamped01 returns a copy of the vector with the components clamped to be in the range of 0 to 1.

func (*T) Cols

func (vec *T) Cols() int

Cols returns the number of columns of the vector.

func (*T) Get

func (vec *T) Get(col, row int) float64

Get returns one element of the vector.

func (*T) Invert

func (vec *T) Invert() *T

Invert inverts the vector.

func (T) Inverted

func (vec T) Inverted() T

Inverted returns an inverted copy of the vector.

func (*T) IsZero

func (vec *T) IsZero() bool

IsZero checks if all elements of the vector are zero.

func (T) Length

func (vec T) Length() float64

Length returns the length of the vector. See also LengthSqr and Normalize.

func (T) LengthSqr

func (vec T) LengthSqr() float64

LengthSqr returns the squared length of the vector. See also Length and Normalize.

func (T) Lerp

func (v T) Lerp(other *T, t float64) T

func (*T) Mul

func (vec *T) Mul(v *T) *T

Mul multiplies the components of the vector with the respective components of v.

func (*T) Normalize

func (vec *T) Normalize() *T

Normalize normalizes the vector to unit length.

func (T) Normalized

func (vec T) Normalized() T

Normalized returns a unit length normalized copy of the vector.

func (*T) Rotate

func (vec *T) Rotate(angle float64) *T

Rotate rotates the vector counter-clockwise by angle.

func (*T) Rotate90DegLeft

func (vec *T) Rotate90DegLeft() *T

Rotate90DegLeft rotates the vector 90 degrees left (counter-clockwise).

func (*T) Rotate90DegRight

func (vec *T) Rotate90DegRight() *T

Rotate90DegRight rotates the vector 90 degrees right (clockwise).

func (*T) RotateAroundPoint

func (vec *T) RotateAroundPoint(point *T, angle float64) *T

RotateAroundPoint rotates the vector counter-clockwise around a point.

func (T) Rotated

func (vec T) Rotated(angle float64) T

Rotated returns a counter-clockwise rotated copy of the vector.

func (*T) Rows

func (vec *T) Rows() int

Rows returns the number of rows of the vector.

func (*T) Scale

func (vec *T) Scale(f float64) *T

Scale multiplies all element of the vector by f and returns vec.

func (T) Scaled

func (vec T) Scaled(f float64) T

Scaled returns a copy of vec with all elements multiplies by f.

func (*T) SetMax

func (vec *T) SetMax(c T)

func (*T) SetMin

func (vec *T) SetMin(c T)

func (*T) Size

func (vec *T) Size() int

Size returns the number elements of the vector.

func (*T) Slice

func (vec *T) Slice() []float64

Slice returns the elements of the vector as slice.

func (*T) String

func (vec *T) String() string

String formats T as string. See also Parse().

func (*T) Sub

func (vec *T) Sub(v *T) *T

Sub subtracts another vector from vec.

Jump to

Keyboard shortcuts

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