v

package module
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 2 Imported by: 9 modules, 38 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc Coverage Status

v

2d vector package for Go

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
}

Vec is a 2D vector

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) AddX added in v1.2.9

func (v Vec) AddX(n float64) Vec

AddX adds n to v.X

func (Vec) AddY added in v1.2.9

func (v Vec) AddY(n float64) Vec

AddY adds n to v.Y

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

Limit limits vector's magnitude to max.

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) Max added in v1.2.9

func (v Vec) Max(other Vec) Vec

Max returns the component-wise maximum of two vectors.

func (Vec) Min added in v1.2.9

func (v Vec) Min(other Vec) Vec

Min returns the component-wise minimum of two vectors.

func (Vec) Mul

func (v Vec) Mul(a Vec) Vec

Mul returns this * a

func (Vec) Neg

func (v Vec) Neg() Vec

Neg negates this vector.

func (Vec) NegX

func (v Vec) NegX() Vec

NegX negates X element.

func (Vec) NegY

func (v Vec) NegY() Vec

NegY negates Y element.

func (Vec) Perp added in v1.2.6

func (v Vec) Perp() Vec

Perp returns the perpendicular vector rotated 90 degrees counter-clockwise.

func (Vec) Project

func (v Vec) Project(other Vec) Vec

Project returns the vector projection of v 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) SetMag added in v1.2.5

func (v Vec) SetMag(m float64) Vec

SetMag sets the magnitude (length) of the 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).

- Vectors with squared length < 1e-8 are considered zero and left unchanged

- Vectors with squared length within 1e-8 of 1.0 are considered already normalized

Jump to

Keyboard shortcuts

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