Vector2i

A 2D vector using integer coordinates.

Description

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 toVector2 when exact precision is required. Note that the values are limited to 32 bits, and unlikeVector2 this cannot be configured with an engine build option. Useint orPackedInt64Array if 64-bit values are needed.

Note: In a boolean context, a Vector2i will evaluate tofalse if it's equal toVector2i(0,0). Otherwise, a Vector2i will always evaluate totrue.

Tutorials

Properties

int

x

0

int

y

0

Constructors

Vector2i

Vector2i()

Vector2i

Vector2i(from:Vector2i)

Vector2i

Vector2i(from:Vector2)

Vector2i

Vector2i(x:int, y:int)

Methods

Vector2i

abs()const

float

aspect()const

Vector2i

clamp(min:Vector2i, max:Vector2i)const

Vector2i

clampi(min:int, max:int)const

int

distance_squared_to(to:Vector2i)const

float

distance_to(to:Vector2i)const

float

length()const

int

length_squared()const

Vector2i

max(with:Vector2i)const

int

max_axis_index()const

Vector2i

maxi(with:int)const

Vector2i

min(with:Vector2i)const

int

min_axis_index()const

Vector2i

mini(with:int)const

Vector2i

sign()const

Vector2i

snapped(step:Vector2i)const

Vector2i

snappedi(step:int)const

Operators

bool

operator !=(right:Vector2i)

Vector2i

operator %(right:Vector2i)

Vector2i

operator %(right:int)

Vector2i

operator *(right:Vector2i)

Vector2

operator *(right:float)

Vector2i

operator *(right:int)

Vector2i

operator +(right:Vector2i)

Vector2i

operator -(right:Vector2i)

Vector2i

operator /(right:Vector2i)

Vector2

operator /(right:float)

Vector2i

operator /(right:int)

bool

operator <(right:Vector2i)

bool

operator <=(right:Vector2i)

bool

operator ==(right:Vector2i)

bool

operator >(right:Vector2i)

bool

operator >=(right:Vector2i)

int

operator [](index:int)

Vector2i

operator unary+()

Vector2i

operator unary-()


Enumerations

enumAxis:🔗

AxisAXIS_X =0

Enumerated value for the X axis. Returned bymax_axis_index() andmin_axis_index().

AxisAXIS_Y =1

Enumerated value for the Y axis. Returned bymax_axis_index() andmin_axis_index().


Constants

ZERO =Vector2i(0,0)🔗

Zero vector, a vector with all components set to0.

ONE =Vector2i(1,1)🔗

One vector, a vector with all components set to1.

MIN =Vector2i(-2147483648,-2147483648)🔗

Min vector, a vector with all components equal toINT32_MIN. Can be used as a negative integer equivalent ofVector2.INF.

MAX =Vector2i(2147483647,2147483647)🔗

Max vector, a vector with all components equal toINT32_MAX. Can be used as an integer equivalent ofVector2.INF.

LEFT =Vector2i(-1,0)🔗

Left unit vector. Represents the direction of left.

RIGHT =Vector2i(1,0)🔗

Right unit vector. Represents the direction of right.

UP =Vector2i(0,-1)🔗

Up unit vector. Y is down in 2D, so this vector points -Y.

DOWN =Vector2i(0,1)🔗

Down unit vector. Y is down in 2D, so this vector points +Y.


Property Descriptions

intx =0🔗

The vector's X component. Also accessible by using the index position[0].


inty =0🔗

The vector's Y component. Also accessible by using the index position[1].


Constructor Descriptions

Vector2iVector2i()🔗

Constructs a default-initializedVector2i with all components set to0.


Vector2iVector2i(from:Vector2i)

Constructs aVector2i as a copy of the givenVector2i.


Vector2iVector2i(from:Vector2)

Constructs a newVector2i from the givenVector2 by truncating components' fractional parts (rounding towards zero). For a different behavior consider passing the result ofVector2.ceil(),Vector2.floor() orVector2.round() to this constructor instead.


Vector2iVector2i(x:int, y:int)

Constructs a newVector2i from the givenx andy.


Method Descriptions

Vector2iabs()const🔗

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


floataspect()const🔗

Returns the aspect ratio of this vector, the ratio ofx toy.


Vector2iclamp(min:Vector2i, max:Vector2i)const🔗

Returns a new vector with all components clamped between the components ofmin andmax, by running@GlobalScope.clamp() on each component.


Vector2iclampi(min:int, max:int)const🔗

Returns a new vector with all components clamped betweenmin andmax, by running@GlobalScope.clamp() on each component.


intdistance_squared_to(to:Vector2i)const🔗

Returns the squared distance between this vector andto.

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


floatdistance_to(to:Vector2i)const🔗

Returns the distance between this vector andto.


floatlength()const🔗

Returns the length (magnitude) of this vector.


intlength_squared()const🔗

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

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


Vector2imax(with:Vector2i)const🔗

Returns the component-wise maximum of this andwith, equivalent toVector2i(maxi(x,with.x),maxi(y,with.y)).


intmax_axis_index()const🔗

Returns the axis of the vector's highest value. SeeAXIS_* constants. If all components are equal, this method returnsAXIS_X.


Vector2imaxi(with:int)const🔗

Returns the component-wise maximum of this andwith, equivalent toVector2i(maxi(x,with),maxi(y,with)).


Vector2imin(with:Vector2i)const🔗

Returns the component-wise minimum of this andwith, equivalent toVector2i(mini(x,with.x),mini(y,with.y)).


intmin_axis_index()const🔗

Returns the axis of the vector's lowest value. SeeAXIS_* constants. If all components are equal, this method returnsAXIS_Y.


Vector2imini(with:int)const🔗

Returns the component-wise minimum of this andwith, equivalent toVector2i(mini(x,with),mini(y,with)).


Vector2isign()const🔗

Returns a new vector with each component set to1 if it's positive,-1 if it's negative, and0 if it's zero. The result is identical to calling@GlobalScope.sign() on each component.


Vector2isnapped(step:Vector2i)const🔗

Returns a new vector with each component snapped to the closest multiple of the corresponding component instep.


Vector2isnappedi(step:int)const🔗

Returns a new vector with each component snapped to the closest multiple ofstep.


Operator Descriptions

booloperator !=(right:Vector2i)🔗

Returnstrue if the vectors are not equal.


Vector2ioperator %(right:Vector2i)🔗

Gets the remainder of each component of theVector2i with the components of the givenVector2i. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using@GlobalScope.posmod() instead if you want to handle negative numbers.

print(Vector2i(10,-20)%Vector2i(7,8))# Prints (3, -4)

Vector2ioperator %(right:int)🔗

Gets the remainder of each component of theVector2i with the givenint. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using@GlobalScope.posmod() instead if you want to handle negative numbers.

print(Vector2i(10,-20)%7)# Prints (3, -6)

Vector2ioperator *(right:Vector2i)🔗

Multiplies each component of theVector2i by the components of the givenVector2i.

print(Vector2i(10,20)*Vector2i(3,4))# Prints (30, 80)

Vector2operator *(right:float)🔗

Multiplies each component of theVector2i by the givenfloat. Returns aVector2.

print(Vector2i(10,15)*0.9)# Prints (9.0, 13.5)

Vector2ioperator *(right:int)🔗

Multiplies each component of theVector2i by the givenint.


Vector2ioperator +(right:Vector2i)🔗

Adds each component of theVector2i by the components of the givenVector2i.

print(Vector2i(10,20)+Vector2i(3,4))# Prints (13, 24)

Vector2ioperator -(right:Vector2i)🔗

Subtracts each component of theVector2i by the components of the givenVector2i.

print(Vector2i(10,20)-Vector2i(3,4))# Prints (7, 16)

Vector2ioperator /(right:Vector2i)🔗

Divides each component of theVector2i by the components of the givenVector2i.

print(Vector2i(10,20)/Vector2i(2,5))# Prints (5, 4)

Vector2operator /(right:float)🔗

Divides each component of theVector2i by the givenfloat. Returns aVector2.

print(Vector2i(10,20)/2.9)# Prints (5.0, 10.0)

Vector2ioperator /(right:int)🔗

Divides each component of theVector2i by the givenint.


booloperator <(right:Vector2i)🔗

Compares twoVector2i vectors by first checking if the X value of the left vector is less than the X value of theright vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.


booloperator <=(right:Vector2i)🔗

Compares twoVector2i vectors by first checking if the X value of the left vector is less than or equal to the X value of theright vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.


booloperator ==(right:Vector2i)🔗

Returnstrue if the vectors are equal.


booloperator >(right:Vector2i)🔗

Compares twoVector2i vectors by first checking if the X value of the left vector is greater than the X value of theright vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.


booloperator >=(right:Vector2i)🔗

Compares twoVector2i vectors by first checking if the X value of the left vector is greater than or equal to the X value of theright vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.


intoperator [](index:int)🔗

Access vector components using theirindex.v[0] is equivalent tov.x, andv[1] is equivalent tov.y.


Vector2ioperator unary+()🔗

Returns the same value as if the+ was not there. Unary+ does nothing, but sometimes it can make your code more readable.


Vector2ioperator unary-()🔗

Returns the negative value of theVector2i. This is the same as writingVector2i(-v.x,-v.y). This operation flips the direction of the vector while keeping the same magnitude.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.