v

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 2 Imported by: 21

README

v

2d vector package for go

https://pkg.go.dev/github.com/setanarut/v

Documentation

Overview

v is a 2d vector package

Index

Constants

This section is empty.

Variables

View Source
var (
	// One Vec{1, 1} vector is a vector with all components set to 1.
	One = Vec{1, 1}
	// Left unit vector. Vec{-1, 0} Represents the direction of left.
	Left = Vec{-1, 0}
	// Right unit vector. Vec{1, 0} Represents the direction of right.
	Right = Vec{1, 0}
	// Up unit vector. Vec{0, -1} Y is down in 2D, so this vector points -Y.
	Up = Vec{0, -1}
	// Down unit vector. Vec{0, 1} Y is down in 2D, so this vector points +Y.
	Down = Vec{0, 1}
)

Functions

This section is empty.

Types

type Vec

type Vec struct {
	X, Y float64
}

func FromAngle

func FromAngle(angle float64) Vec

FromAngle makes a new 2D unit vector from an angle

func (Vec) Abs

func (v Vec) Abs() Vec

Abs returns the absolute value of vector.

func (Vec) AbsX

func (v Vec) AbsX() float64

AbsX returns the absolute X value of vector.

func (Vec) AbsY

func (v Vec) AbsY() float64

AbsY returns the absolute Y value of vector.

func (Vec) Add

func (v Vec) Add(a Vec) Vec

Add returns this + a

func (Vec) Angle

func (v Vec) Angle() float64

Angle returns the angular direction v is pointing in (in radians).

func (Vec) AngleTo

func (v Vec) AngleTo(other Vec) float64

AngleTo returns the angle to the given vector, in radians.

func (Vec) Ceil

func (v Vec) Ceil() Vec

Ceil returns vector with all components rounded up (towards positive infinity).

func (Vec) Cross

func (v Vec) Cross(other Vec) float64

Cross calculates the 2D vector cross product analog. The cross product of 2D vectors results in a 3D vector with only a z component. This function returns the magnitude of the z value.

func (Vec) Dist

func (v Vec) Dist(other Vec) float64

Dist returns distance between v and other.

func (Vec) DistSq

func (v Vec) DistSq(other Vec) float64

DistSq returns the squared distance between this and other.

Faster than v.Dist() when you only need to compare distances.

func (Vec) Div

func (v Vec) Div(a Vec) Vec

Div divides this vector by a.

func (Vec) DivS added in v1.1.0

func (v Vec) DivS(s float64) Vec

DivS divides this vector by scalar value s.

func (Vec) Dot

func (v Vec) Dot(other Vec) float64

Dot returns dot product

func (Vec) Equals

func (v Vec) Equals(other Vec) bool

Equals checks if two vectors are equal. (Be careful when comparing floating point numbers!)

func (Vec) EqualsPr

func (v Vec) EqualsPr(other Vec, allowedDelta float64) bool

EqualsP returns they are practically equal with each other within a delta tolerance.

func (Vec) Floor

func (v Vec) Floor() Vec

Floor returns vector with all components rounded down (towards negative infinity).

func (Vec) IsZero

func (v Vec) IsZero() bool

IsZero returns true if vector is zero vector

func (Vec) Lerp

func (v Vec) Lerp(other Vec, t float64) Vec

Lerp linearly interpolates between this and other vector.

func (Vec) Limit

func (v Vec) Limit(max float64) Vec

Limits a vector's magnitude to a maximum value.

func (Vec) Mag

func (v Vec) Mag() float64

Mag returns the magnitude (length) of the vector.

func (Vec) MagSq

func (v Vec) MagSq() float64

MagSq returns the magnitude (length) of the vector, squared.

This method is often used to improve performance since, unlike Mag(), it does not require a Sqrt() operation.

func (Vec) Mul

func (v Vec) Mul(a Vec) Vec

Mul returns this * a

func (Vec) Neg

func (v Vec) Neg() Vec

Neg negates a vector.

func (Vec) NegX

func (v Vec) NegX() Vec

NegY negates X.

func (Vec) NegY

func (v Vec) NegY() Vec

NegY negates Y.

func (Vec) Project

func (v Vec) Project(other Vec) Vec

Returns the vector projection onto other.

func (Vec) Reflect added in v1.1.2

func (v Vec) Reflect(normal Vec) Vec

Reflect returns the reflection of the vector v over the given normal. normal should be a normalized (unit) vector.

func (Vec) Rotate

func (v Vec) Rotate(angle float64) Vec

Rotate a vector by an angle in radians

func (Vec) Round

func (v Vec) Round() Vec

Round returns the nearest integer Vector, rounding half away from zero.

func (Vec) Scale

func (v Vec) Scale(s float64) Vec

Scale scales vector

func (Vec) Slerp

func (v Vec) Slerp(to Vec, weight float64) Vec

Slerp performs spherical linear interpolation between two vectors with given weight value in [0,1] range, returning interpolated vector

func (Vec) String

func (v Vec) String() string

String returns string representation of this vector.

func (Vec) Sub

func (v Vec) Sub(a Vec) Vec

Sub returns this - a

func (Vec) Unit

func (v Vec) Unit() Vec

Unit returns a normalized copy of this vector (unit vector).

Jump to

Keyboard shortcuts

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