Class: Vector2

RS.Math.Vector2

Class representing a 2D Vector with components x and y.

new RS.Math.Vector2 (initial)

Creates a new Vector2. Accepts any arguments that RS.Math.Vector2#set accepts.

Name Type Description
initial RS.Math.Vector2 | Array | Object | Number optional

initial value.

Example
let v = new RS.Math.Vector2();
v = new RS.Math.Vector2(1,2);
v = new RS.Math.Vector2([0.2,-0.3]);
v = new RS.Math.Vector2({x: 0.1, y: 0.53});

Members

x Number

The X component of the vector

y Number

The Y component of the vector

Methods

add (rhs)RS.Math.Vector2

Adds another vector to this vector.

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector

Returns:
Type Description
RS.Math.Vector2 this
Example
const vec = new RS.Math.Vector2(1,2);
vec.add(new RS.Math.Vector2(7,-3));
// vec is now {x:8,y:-1}

clone ()RS.Math.Vector2

returns a copy of this vector.

Returns:
Type Description
RS.Math.Vector2

distance (rhs)Number

Returns the distance between the point specified by this vector and another

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector.

Returns:
Type Description
Number the distance

divide (rhs)RS.Math.Vector2

Divides another vector into this vector per-component.

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector

Returns:
Type Description
RS.Math.Vector2 this
Example
const vec = new RS.Math.Vector2(1,2);
vec.divide(new RS.Math.Vector2(7,-3));
// vec is now {x:0.1428,y:-0.6667}

dot (rhs)Number

Returns the dot product between this vector and another.

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector.

Returns:
Type Description
Number the dot product

equal (rhs, tolerance)Boolean

Returns whether this vector is equal to another vector.

Name Type Description
rhs RS.Math.Vector2

the vector to compare to

tolerance Number optional

if provided then this level of tolerance is used.

Returns:
Type Description
Boolean true if equal, false if not
Example
let vec = new RS.Math.Vector2(0.01,-0.05);
vec.equal(vec);
// returns true
vec.equal(new RS.Math.Vector2(0.02,-0.05));
// returns false
vec.equal(new RS.Math.Vector2(0.02,-0.05),0.1);
// returns true due to tolerance

equal_with_tolerance (rhs, tolerance)Boolean

Returns whether this vector is equal to another vector within a tolerance.

Name Type Description
rhs RS.Math.Vector2

the vector to compare to

tolerance Number optional

if provided then this level of tolerance is used, otherwise tolerance is 10e-5

Returns:
Type Description
Boolean true if equal, false if not
Example
let vec = new RS.Math.Vector2(0.01,-0.05);
vec.equal_with_tolerance(vec);
// returns true
vec.equal_with_tolerance(new RS.Math.Vector2(0.02,-0.05));
// returns false
vec.equal_with_tolerance(new RS.Math.Vector2(0.01001,-0.05));
// returns true due to default tolerance
vec.equal_with_tolerance(new RS.Math.Vector2(0.02,-0.05),0.1);
// returns true due to tolerance

is_colinear (rhs)Boolean

Returns whether this vector and another are colinear (point in the same direction.

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector

Returns:
Type Description
Boolean true if colinear, false if not
Example
const vec = new RS.Math.Vector2(1,2);
vec.is_colinear(new RS.Math.Vector2(2,4));
// returns true
vec.is_colinear(new RS.Math.Vector2(1,0));
// returns false
vec.is_colinear(new RS.Math.Vector2(-2,-4));
// returns false since they point in opposite directions

is_null_vector (tolerance)Boolean

Returns whether this vector is the null vector

Name Type Description
tolerance Number optional

if provided then this level of tolerance is used.

Returns:
Type Description
Boolean true if null, false if not
Example
let vec = new RS.Math.Vector2(0.01,-0.05);
vec.is_null_vector();
// returns false
vec.is_null_vector(0.1);
// returns true due to tolerance
vec = new RS.Math.Vector2();
vec.is_null_vector();
// returns true

length ()Number

Returns the length of this vector.

Returns:
Type Description
Number the length

multiply (rhs)RS.Math.Vector2

Multiplies another vector with this vector per-component.

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector

Returns:
Type Description
RS.Math.Vector2 this
Example
const vec = new RS.Math.Vector2(1,2);
vec.multiply(new RS.Math.Vector2(7,-3);
// vec is now {x:7,y:-6}

normalize ()RS.Math.Vector2

Normalizes this vector.

Returns:
Type Description
RS.Math.Vector2 this

scale (scale)RS.Math.Vector2

Uniformly scales this vector.

Name Type Description
scale Number

the scaling factor

Returns:
Type Description
RS.Math.Vector2 this
Example
const vec = new RS.Math.Vector2(1,2);
vec.scale(2);
// vec is now {x:2,y:4}

set (source)

Sets this vector. The source may be of the following types:

  • RS.Math.Vector2
  • an Array with 2 or more members
  • an Object.
  • individual arguments for x and y

In the case of an object being supplied it should have the members x and y. Parsing failures on x or y will set them to 0.

Name Type Description
source RS.Math.Vector2 | Array | Object | Number

the object to set from or a set of numbers.

Example
const v = new RS.Math.Vector2();
v.set(1,2);
v.set([0.2,-0.3]);
v.set({x: 0.1, y: 0.53});

subtract (rhs)RS.Math.Vector2

Subtracts another vector from this vector.

Name Type Description
rhs RS.Math.Vector4 | RS.Math.Vector3 | RS.Math.Vector2

the other vector

Returns:
Type Description
RS.Math.Vector2 this
Example
const vec = new RS.Math.Vector2(1,2);
vec.subtract(new RS.Math.Vector2(7,-3));
// vec is now {x:-6,y:5}

toString ()String

Returns a string describing this Object.

Returns:
Type Description
String A String describing this Object.