Vector2i

package
v0.0.0-...-f3deeb4 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package Vector2i provides a 2D vector using integer coordinates.

Index

Constants

This section is empty.

Variables

View Source
var (
	Zero  = XY{0, 0}                         // Zero vector, a vector with all components set to 0.
	One   = XY{1, 1}                         // One vector, a vector with all components set to 1.
	MinXY = XY{math.MinInt32, math.MinInt32} // Min vector, a vector with all components equal to [math.MinInt32]. Can be used as a negative integer equivalent of [Vector2.Inf].
	MaxXY = XY{math.MaxInt32, math.MaxInt32} // Max vector, a vector with all components equal to [math.MaxInt32]. Can be used as a positive integer equivalent of [Vector2.Inf].
	Left  = XY{-1, 0}                        // Left vector, a unit vector pointing left (-1, 0).
	Right = XY{1, 0}                         // Right vector, a unit vector pointing right (1, 0).
	Up    = XY{0, -1}                        // Up vector, a unit vector pointing up (0, -1).
	Down  = XY{0, 1}                         // Down vector, a unit vector pointing down (0, 1).
)

Functions

func AsArray

func AsArray(vec XY) [3]int32

func Aspect

func Aspect(v XY) Float.X

Aspect returns the aspect ratio of this vector, the ratio of x to y.

func Distance

func Distance(a, b XY) Float.X

Distance returns the distance between this vector and to.

func DistanceSquared

func DistanceSquared(a, b XY) int

DistanceSquared returns the squared distance between this vector and to.

This method runs faster than distance_to, so prefer it if you need to compare vectors or need the squared distance for some formula.

func Index

func Index[I Int.Any](v XY, i I) int

func Length

func Length(vec XY) Float.X

Length returns the length (magnitude) of this vector.

func LengthSquared

func LengthSquared(vec XY) Float.X

LengthSquared returns the squared length (squared magnitude) of this vector.

Types

type Axis

type Axis int
const (
	X Axis = iota // Enumerated value for the X axis. Returned by [MaxAxis] and [MinAxis].
	Y Axis = iota // Enumerated value for the Y axis. Returned by [MaxAxis] and [MinAxis].
)

func MaxAxis

func MaxAxis(vec XY) Axis

MaxAxis returns the axis of the vector's highest value. See Axis constants. If all components are equal, this method returns X.

func MinAxis

func MinAxis(vec XY) Axis

MinAxis returns the axis of the vector's lowest value. See Axis constants. If all components are equal, this method returns Y.

type XY

type XY = struct {
	X int32
	Y int32
}

A 2-element structure that can be used to represent 2D grid coordinates or any other pair of integers.

It uses integer coordinates and is therefore preferable to Vector2 when exact precision is required. Note that the values are limited to 32 bits, and unlike Vector2 this cannot be configured with an engine build option. Use int, int64 or PackedInt64Array if 64-bit values are needed.

func Abs

func Abs(v XY) XY

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

func Add

func Add(a, b XY) XY

func AddX

func AddX[X Float.Any | Int.Any](a XY, b X) XY

func Clamp

func Clamp(vec, min, max XY) XY

Clamp returns a new vector with all components clamped between the components of min and max, by running clamp on each component.

func Clampi

func Clampi[X Int.Any](vec XY, min, max X) XY

Clampi returns a new vector with all components clamped between the components of min and max.

func Div

func Div(a, b XY) XY

func DivX

func DivX[X Float.Any | Int.Any](a XY, b X) XY

func Max

func Max(a, b XY) XY

Max returns the component-wise maximum of this and with.

func Maxi

func Maxi[X Int.Any](a XY, b X) XY

Max returns the component-wise maximum of this and with.

func Min

func Min(a, b XY) XY

Min returns the component-wise minimum of this and with.

func Mini

func Mini[X Int.Any](a XY, b X) XY

Mini returns the component-wise minimum of this and with.

func Mod

func Mod(a, b XY) XY

func ModX

func ModX[X Float.Any | Int.Any](a XY, b X) XY

func Mul

func Mul(a, b XY) XY

func MulX

func MulX[X Float.Any | Int.Any](a XY, b X) XY

func Neg

func Neg(v XY) XY

func New

func New[X Int.Any | Float.Any](x, y X) XY

New constructs a new Vector2i from the given x and y.

func Sign

func Sign(v XY) XY

Sign returns a new vector with each component set to 1 if it's positive, -1 if it's negative, and 0 if it's zero. The result is identical to calling [Signi] on each component.

func Snapped

func Snapped(v, step XY) XY

Snapped returns a new vector with each component snapped to the closest multiple of the corresponding component in step.

func Snappedi

func Snappedi[X Int.Any](v XY, step X) XY

Snappedi returns a new vector with each component snapped to the closest multiple of the corresponding component in step.

func Sub

func Sub(a, b XY) XY

func SubX

func SubX[X Float.Any | Int.Any](a XY, b X) XY

Jump to

Keyboard shortcuts

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