Skip to content

Mearleo/VectorJS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 

Repository files navigation

VectorJS

What is this?

VectorJS is a free to use Vector math system for your JavaScript programs.
It has fancy syntax that is easy to learn.


Basics

Here is how you create a vector:

const vec = V(1, 2); // The first value x is 1 and y is 2

A Vector can have from 0 dimensions up to 4 dimensions:

const vec_0 = V();
const vec_4 = V(1, 2, 3, 4);

You can chain actions but note that it calculates from left to right.
This is important but go to the section Operations first to understand better.

const vec = V(0, 1) ["+"] (0, 1) ["*"] (1, 2);
vec ["+="] (3) ["*="] (2, true);
// vec is now (3, 8);

To set and get the dimensions, just write down the dimension:

const vec = V(5, 3, 4);
vec.x += 1; // x = 6
console.log(vec.y); // console: 3

As you will see later, multiple inputs will count as a Vector:

(vec) // variables that are Vectors
(V(4, 1, 3)) // new Vectors
(0, 3) // Numbers only in the brackets
({x: 6, y: 4, z: 0, w: 3}) // Objects



Operations

Simply write your operation into a string in square brackets (+, -, *, /):

const vec_a = V(1, 2);
const vec_b = V(3, 4);
const vec_c = vec_a ["+"] (vec_b); // vec_c is now (4, 6)

To change a vectors value, use +=, -=, *=, /=:

const vec = V(1, 2);
vec ["+="] (2, 2); // vec_c is now (3, 4)

IMPORTANT
If you want to change all values, the input has to be a number and after that, write a boolean down!

const vec = V(1, 2);
vec ["+="] (2, true); // vec_c is now (3, 4)



Additional Functions

Dimensions:

const vec = V(1, 2, 3, 4);

// get dimesions
vec ["dm"] (); // (returns 4)

// set dimensions
vec ["dm"] (2); // returns vector (1, 2)

vec ["dm"] (4) // vec is now (1, 2, 0, 0)

vec ["dm"] (0) // vec is now ()

// the second value indicates the number that will be created
vec ["dm"] (3, 1) // vec is now (1, 1, 1)

That means that you elegantly can create Vectors:

const vec = V() ["dm"] (3, 1); // vec is now (1, 1, 1)

Distances:

Normal:

const vec_a = V(1, 0);
const vec_b = V(0, 0);

const dist = vec_a ["dist"] (vec_b); // dist is 1

With different dimensions:

const vec_a = V(1, 1);
const vec_b = V(1); // gets treated as (1, 0)

const dist = vec_a ["dist"] (vec_b); // dist is 1

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published