vector

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// EPSILON define small comparisons
	EPSILON = float64(0.00001)
	// Integer Zero vector, a vector with all components set to 0.
	ZERO = New2D[int](0, 0)
	// Integer One vector, a vector with all components set to 1.
	ONE = New2D[int](1, 1)
	// Left unit vector. Represents the direction of left.
	LEFT = New2D[int](-1, 0)
	// Right unit vector. Represents the direction of right.
	RIGHT = New2D[int](1, 0)
	// Up unit vector. Y is down in 2D, so this vector points -Y.
	UP = New2D[int](0, -1)
	// Down unit vector. Y is down in 2D, so this vector points +Y.
	DOWN = New2D[int](0, 1)
)

Functions

This section is empty.

Types

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

type Vector2D

type Vector2D[T Number] struct {
	// The vector's X component
	X T `json:"x"`
	// The vector's Y component
	Y T `json:"y"`
}

Vector2D is a 2D vector using numeric coordinates as generics. A 2-element structure that can be used to represent 2D coordinates or any other pair of numeric values.

func New2D

func New2D[T Number](x, y T) *Vector2D[T]

New2D constructs a new Vector2 from the given x and y.

func (*Vector2D[T]) Abs added in v0.0.7

func (v *Vector2D[T]) Abs() *Vector2D[T]

ABS returns a new vector with all components in absolute values (i.e. positive).`

func (*Vector2D[T]) Add

func (v *Vector2D[T]) Add(to *Vector2D[T]) *Vector2D[T]

Add one vector to another

func (*Vector2D[T]) Clone added in v0.0.4

func (v *Vector2D[T]) Clone() *Vector2D[T]

Clone returns a deep-copy of the current vector.

func (*Vector2D[T]) Cross

func (v *Vector2D[T]) Cross(w *Vector2D[T]) T

Returns the 2D analog of the cross product for this vector and with. This is the signed area of the parallelogram formed by the two vectors. If the second vector is clockwise from the / first vector, then the cross product is the positive area. If counter-clockwise, the cross product is the negative area. Note: Cross product is not defined in 2D mathematically. This method embeds the 2D vectors in the XY plane of 3D space and uses their cross product's Z component as the analog.

func (*Vector2D[T]) DirectionTo added in v0.0.3

func (v *Vector2D[T]) DirectionTo(to *Vector2D[T]) *Vector2D[T]

DirectionTo returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).normalized().

func (*Vector2D[T]) DistanceSquaredTo

func (v *Vector2D[T]) DistanceSquaredTo(w *Vector2D[T]) T

Returns the squared distance between this vector and w.

func (*Vector2D[T]) DistanceTo

func (v *Vector2D[T]) DistanceTo(w *Vector2D[T]) T

Returns the distance between this vector and w.

func (*Vector2D[T]) Dot

func (v *Vector2D[T]) Dot(w *Vector2D[T]) T

Returns the dot product of this vector and with. This can be used to compare the angle between two vectors. For example, this can be used to determine whether an enemy is facing the player. The dot product will be 0 for a straight angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees. When using unit (normalized) vectors, the result will always be between -1.0 (180 degree angle) when the vectors are facing opposite directions, and 1.0 (0 degree angle) when the vectors are aligned. Note: a.dot(b) is equivalent to b.dot(a).

func (*Vector2D[T]) LERP

func (v *Vector2D[T]) LERP(to *Vector2D[T], weight float64) *Vector2D[T]

Returns the result of the linear interpolation between this vector and to by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation. interpolation = A * (1 - t) + B * t = A + (B - A) * t

func (*Vector2D[T]) Length

func (v *Vector2D[T]) Length() T

Lenght returns the length (magnitude) of this vector.

func (*Vector2D[T]) LengthSquared

func (v *Vector2D[T]) LengthSquared() T

Returns the squared length (squared magnitude) of this vector.

func (*Vector2D[T]) MoveToward

func (v *Vector2D[T]) MoveToward(to *Vector2D[T], delta T) *Vector2D[T]

MoveToward returns a new vector moved toward to by the fixed delta amount. Will not go past the final value.

func (*Vector2D[T]) Normalized

func (v *Vector2D[T]) Normalized() *Vector2D[T]

Returns the result of scaling the vector to unit length. Equivalent to v / v.length(). Note: This function may return incorrect values if the input vector length is near zero.

func (*Vector2D[T]) Subtract

func (v *Vector2D[T]) Subtract(u *Vector2D[T]) *Vector2D[T]

Subtract one vector from another

Jump to

Keyboard shortcuts

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