Vector3i

package
v0.0.0-...-e95b224 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package Vector3 provides a 3D vector using integer coordinates.

Index

Constants

This section is empty.

Variables

View Source
var (
	Zero   = XYZ{0, 0, 0}                                     // Zero vector, a vector with all components set to 0.
	One    = XYZ{1, 1, 1}                                     // One vector, a vector with all components set to 1.
	MinXYZ = XYZ{math.MinInt32, math.MinInt32, math.MinInt32} // Min vector, a vector with all components equal to [math.MinInt32]. Can be used as a negative integer equivalent of Vector3.INF.
	MaxXYZ = XYZ{math.MaxInt32, math.MaxInt32, math.MaxInt32} // Max vector, a vector with all components equal to [math.MaxInt32]. Can be used as a positive integer equivalent of Vector3.INF.
	Left   = XYZ{-1, 0, 0}                                    // Left unit vector. Represents the local direction of left, and the global direction of west.
	Right  = XYZ{1, 0, 0}                                     // Right unit vector. Represents the local direction of right, and the global direction of east.
	Up     = XYZ{0, -1, 0}                                    // Up unit vector.
	Down   = XYZ{0, 1, 0}                                     // Down unit vector.

	// Forward unit vector. Represents the local direction of forward, and the global direction
	// of north. Keep in mind that the forward direction for lights, cameras, etc is different
	// from 3D assets like characters, which face towards the camera by convention. Use [ModelFront]
	// and similar constants when working in 3D asset space.
	Forward = XYZ{0, 0, -1}

	Back = XYZ{0, 0, 1} // Back unit vector. Represents the local direction of back, and the global direction of south.
)

Functions

func AsArray

func AsArray(vec XYZ) [3]int32

func Distance

func Distance(v, to XYZ) Float.X

Distance returns the distance between this vector and to.

func DistanceSquared

func DistanceSquared(v, to XYZ) int

DistanceSquared returns the squared distance between this vector and to.

This method runs faster than [Vector3.DistanceTo], 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 XYZ, i I) int

func Length

func Length(v XYZ) Float.X

Length the length (magnitude) of this vector.

func LengthSquared

func LengthSquared(v XYZ) int

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

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

Types

type Axis

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

func MaxAxis

func MaxAxis(v XYZ) 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(v XYZ) Axis

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

type XYZ

type XYZ = struct {
	X int32 // The vector's X component.
	Y int32 // The vector's Y component.
	Z int32 // The vector's Z component.
}

XYZ is a 3-element structure that can be used to represent 3D grid coordinates or any other triplet of integers.

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

func Abs

func Abs(v XYZ) XYZ

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

func Add

func Add(a, b XYZ) XYZ

func AddX

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

func Clamp

func Clamp(v, min, max XYZ) XYZ

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

func Clampi

func Clampi[X Int.Any](v XYZ, min, max X) XYZ

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

func Div

func Div(a, b XYZ) XYZ

func DivX

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

func Max

func Max(a, b XYZ) XYZ

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

func Maxi

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

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

func Min

func Min(a, b XYZ) XYZ

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

func Mini

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

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

func Mod

func Mod(a, b XYZ) XYZ

func ModX

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

func Mul

func Mul(a, b XYZ) XYZ

func MulX

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

func Neg

func Neg(v XYZ) XYZ

func New

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

New returns a XYZ with the given components.

func Sign

func Sign(v XYZ) XYZ

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 XYZ) XYZ

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 XYZ, step X) XYZ

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 XYZ) XYZ

func SubX

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

Jump to

Keyboard shortcuts

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