# Vec2.js

A generic library useful when you need to work with points/vectors in 2d space.

## Use

` var a = 10 10 // new keyword b = ; // call the constructor directly console; // 90`

**Stuff to Note**: most of the Vec2's methods take a `returnNew`

as the last parameter. If passed a truthy value, a new vector will be returned to you. Otherwise the operation will be applied to `this`

and `this`

will be returned.

Also, since `Infinity`

and `NaN`

are so insidious, this library will throw as soon as it detects either of these so you can take action to fix your data/algorithm.

## Supported operations

**change**([fn])

Add an observer `fn`

that will be called whenever this vector changes. Calling this method without a function causes it to notify observers.

`fn`

signature: `function(vec, prev) {}`

- where `prev`

is a clone of the vector before the last operation.

this function returns the passed `fn`

*returns*: `Vec2`

**ignore**([fn])

Pass a `fn`

to remove it from the observers list. Calling this function without a `fn`

will remove all observers.

*returns*: `Vec2`

**set**(x, y [, notify]) or **set**(vec2 [, notify])

Sets the `x`

and `y`

coordinates of this vector. If `false`

is passed for `notify`

, none of the observers will be called.

*returns*: `Vec2`

**zero**()

Sets the `x`

and `y`

of this vector to `0`

*returns*: `Vec2`

**clone**()

Returns a clone of this vector.

*Note*: this does not clone observers

*returns*: `Vec2`

**negate**([returnNew])

Negate the `x`

and `y`

coords of this vector. If `returnNew`

is truthy, a new vector with the negated coordinates will be returned.

*returns*: `Vec2`

**add**(x, y [, returnNew]) or **add**(array, [, returnNew]) or **add**(vec2 [, returnNew])

Add the `x`

and `y`

to this vector's coordinates.

If `returnNew`

is truthy, return a new vector containing the resulting coordinates. Otherwise apply them to this vector and return it.

*returns*: `Vec2`

**subtract**(x, y [, returnNew]) or **subtract**(array, [, returnNew]) or **subtract**(vec2 [, returnNew])

*returns*: `Vec2`

**multiply**(scalar [, returnNew]) or **multiply**(x, y [, returnNew]) or **multiply**(array, [, returnNew]) or **multiply**(vec2 [, returnNew])

Multiply this vectors components with the incoming, returning a clone if `returnNew`

is truthy.

*returns*: `Vec2`

**divide**(scalar [, returnNew]) or **divide**(x, y [, returnNew]) or **divide**(array, [, returnNew]) or **divide**(vec2 [, returnNew])

Divide this vectors components by the incoming, returning a clone if `returnNew`

is truthy.

*note*: this method will throw if you attempt to divide by zero or pass values that cause NaNs

*returns*: `Vec2`

**rotate**(radians [, inverse [, returnNew]])

Rotate this vector's cordinates around `(0,0)`

. If `returnNew`

is specified, a new `Vec2`

will be created and populated with the result and returned. Otherwise the result is applied to this vector and `this`

is returned.

`inverse`

- inverts the direction of the rotation

`returnNew`

- causes the result to be applied to a new `Vec2`

, otherwise the result is applied to `this`

*returns*: `Vec2`

**length**()

Returns the length of this vector from `(0,0)`

*returns*: `double`

**lengthSquared**()

Returns the length of this vector prior to the `Math.sqrt`

call.

This is usefull when you don't need to know the actual distance, but need a normalized value to compare with another `Vec2#lengthSquared`

or similar.

*returns*: `double`

**distance**(vec2)

*returns*: the distance between this vector and the incoming

**nearest**(array)

*returns*: closest vector in array to this vector.

**normalize**([returnNew])

Normalizes this vector. If `returnNew`

is truthy, a new vector populated with the normalized coordinates will be returned.

*returns*: `Vec2`

**equal**(vec2) or **equal**(x, y) or **equal**(array)

returns true if the incoming coordinates are the same as this vector's

*returns*: `boolean`

**abs**([returnNew])

Return a `Vec2`

that contains the absolute value of each of this vector's parts.

If `returnNew`

is truthy, create a new `Vec2`

and return it. Otherwise apply the absolute values to to `this`

.

*returns*: `Vec2`

**min**(vec)

Return a `Vec2`

consisting of the smallest values from this vector and the incoming

When returnNew is truthy, a new `Vec2`

will be returned otherwise the minimum values in either this or `vec`

will be applied to this vector.

*returns*: `Vec2`

**max**(vec)

Return a `Vec2`

consisting of the largest values from this vector and the incoming

When returnNew is truthy, a new `Vec2`

will be returned otherwise the maximum values in either `this`

or `vec`

will be applied to this vector.

*returns*: `Vec2`

**clamp**(low, high [, returnNew])

Clamp the coordinates of this vector to the high/low of the incoming vec2s. If `returnNew`

apply the result to the new vector and return. Otherwise apply to this vector.

*returns*: `Vec2`

**lerp**(vec, amount [, returnNew])

Perform linear interpolation between this vector and the incoming.

`amount`

- the percentage along the path to place the vector

`returnNew`

- if `truthy`

, apply the result to a new vector and return it, otherwise return `this`

*returns*: `Vec2`

**skew**([returnNew])

Returns a vector set with the `(-y,x)`

coordinates of this vector. If `returnNew`

a new vector is created and the operation is applied to the new vector.

*returns*: `Vec2`

**dot**()

*returns*: `double`

**perpDot**()

*returns*: `double`

**angleTo**(vec)

returns the angle from this vector to the incoming.

*returns*: `double`

**isPointOnLine**(start, end)

where `start`

and `end`

are vec2-like (e.g. `start.x`

and `start.y`

)

*returns*: `boolean`

**toArray**()

*returns*: `[x, y]`

**fromArray**(array)

Applies the `[0]`

to `this.x`

and `[1]`

to `this.y`

*returns*: `Vec2`

**toJSON**()

*returns*: `{ x: ..., y: ...}`

**toString**()

*returns*: `'(x, y)'`

## Install

### Browser

### Node

install with npm

```
npm install vec2
```

and then require it!

```
var Vec2 = require('vec2');
```

## License

MIT (see LICENSE.txt)