The Vector2 class can be used for calculating math with basic (x, y) coordinates

Static methods

staticdistance(pt1:Vector2, pt2:Vector2):Float

Calculates the distance between two Vector2 points

Parameters:

pt1

A Vector2 instance

pt2

A second Vector2 instance

Returns:

The distance between each Vector2

@:value({ result : null })staticinterpolate(pt1:Vector2, pt2:Vector2, f:Float, ?result:Vector2):Vector2

Interpolates between two points, given a specified percentage value

Parameters:

pt1

A Vector2 instance

pt2

A second Vector2 instance

f

A percentage value to interpolate

result

(Optional) A Vector2 instance to use for the result

Returns:

A Vector2 instance holding the interpolated value

@:value({ result : null })staticpolar(len:Float, angle:Float, ?result:Vector2):Vector2

Converts a polar coordinate to into a cartesian Vector2 instance

Parameters:

len

The length of the polar value

angle

The angle of the polar value

result

(Optional) A Vector2 instance to store the result

Returns:

A Vector2 instance in cartesian coordinates

Constructor

@:value({ y : 0, x : 0 })new(x:Float = 0, y:Float = 0)

Creates a new Vector instance

Parameters:

x

(Optional) An initial x value (default is 0)

y

(Optional) An initial y value (default is 0)

Variables

read onlylength:Float

Gets the length of this vector from (0, 0) to (x, y)

read onlylengthSquared:Float

Gets the square of the length of this vector, which avoids use of Math.sqrt for faster performance

x:Float

The x coodinate for this vector

y:Float

The y coodinate for this vector

Methods

@:value({ result : null })add(v:Vector2, ?result:Vector2):Vector2

Adds the current vector to a second Vector2 instance and returns the result

Parameters:

v

A Vector2 instance to add

result

(Optional) A Vector2 instance to store the result

Returns:

A Vector2 instance that combines both vector values

clone():Vector2

Clones the current Vector2

Returns:

A new Vector2 instance with the same values as the current one

equals(toCompare:Vector2):Bool

Whether this Vector2 has the same values as another instance

Parameters:

toCompare

A Vector2 instance to compare against

Returns:

Whether the values of each vector are equal

normalize(thickness:Float):Void

Normalizes this vector between the current length and a set scale value

Parameters:

thickness

The scaling value. . For example, if the current vector is (0, 5) and you normalize it to 1, the normalized value will be (0, 1)

offset(dx:Float, dy:Float):Void

Offsets the current value of this vector

Parameters:

dx

An offset x value

dy

An offset y value

inlinesetTo(xa:Float, ya:Float):Void

Sets this Vector2 to new values

Parameters:

xa

An x value

ya

A y value

@:value({ result : null })subtract(v:Vector2, ?result:Vector2):Vector2

Subtracts the current vector from another Vector2 instance

Parameters:

v

A Vector2 instance to subtract from the current vector

result

(Optional) A Vector2 instance to store the result

Returns:

A Vector2 instance containing the subtracted values