veca

Veca is a simple library implementing 2-D and 3-D vectors.

Installation

From NodeJS

First, use npm install to get veca . Second, require veca like so:

const v = require ( ' veca ' )

You may now use veca's v() function.

From the Browser

Simply download the file v.js from this repository, and then include it using:

< script src = ' v.js ' > < / script >

You can also use RawGit's CDN:

< script src = " https://cdn.rawgit.com/emctague/veca/2cae9b9a/v.js " > < / script >

Usage

Creating a Vector

You can create both 2-Dimensional and 3-Dimensional vectors using the v function.

v ( 10 , 20 ) v ( 30 , 40 , 50 )

Converting a Vector to a String

The str method can be used to convert a vector to a simple, readable string:

v ( 30 , 20 , 49 ) . str ( )

The vector will also be implicitly converted to a string:

' The vector is: ' + v ( 5 , 15 )

Using the Values from a Vector

You can use the x , y , and z properties to interact directly with a vector's values.

const vector = v ( 5 , 3 , 9 ) vector . x vector . y vector . z vector . z = 10 vector . str ( )

Getting the Number of Dimensions in a Vector

The dimens method will return 2 or 3 , depending on whether or not the vector is 3-Dimensional.

v ( 3 , 6 ) . dimens ( ) v ( 3 , 6 , 9 ) . dimens ( )

Converting a Vector to an Array or Object

The arr method converts the vector to an array.

v ( 93 , 2 ) . arr ( )

Similarly, the obj method will convert the vector to a simple object.

v ( 20 , 39 , 42 ) . obj ( )

Basic Mathematical Operations

A number of basic operations can be used on a vector to manipulate its values. Each of these methods can be called with either another vector, in which case each value will be modified using the equivalent value in the other vector, or with a single number, which will be used to modify each of the vector values.

Division is done with the div method:

v ( 2 , 4 , 6 ) . div ( 2 ) v ( 2 , 4 , 6 ) . div ( v ( 2 , 4 , 3 ) )

Multiplication is done with the mult method:

v ( 2 , 4 , 6 ) . mult ( 2 ) v ( 2 , 4 , 6 ) . mult ( v ( 2 , 4 , 3 ) )

Addition is done with the add method:

v ( 2 , 4 , 6 ) . add ( 2 ) v ( 2 , 4 , 6 ) . add ( v ( 2 , 4 , 3 ) )

Subtraction is done with the sub method:

v ( 2 , 4 , 6 ) . sub ( 2 ) v ( 2 , 4 , 6 ) . div ( v ( 2 , 4 , 3 ) )

Powers are done with the pow method:

v ( 2 , 4 , 6 ) . pow ( 2 ) v ( 2 , 4 , 6 ) . div ( v ( 2 , 4 , 3 ) )

Modulo is done with the mod method:

v ( 2 , 4 , 6 ) . mod ( 3 ) v ( 2 , 4 , 6 ) . mod ( v ( 2 , 4 , 3 ) )

Dot Product

The dot product can be gotten using the dot method.

v ( 5 , 10 , 15 ) . dot ( v ( 3 , 6 , 9 ) )

Custom Iterative Methods

Custom iterative methods, which behave in the same way as the "Basic Mathematical Operations" specified above, can be created using the iter method: