Giter VIP home page Giter VIP logo

vector2d.js's Introduction

Vector2d.js - An object oriented vector library for javascript.

Including Vector2d.js

Vector2d can be loaded through Require.js (AMD), CommonJS or using script tags.

Require.js

define(['vector2d'], function(Vector2d) { ... });

Common js

var Vector2d = require('vector2d').Vector2d;

Script tag

<script src="vector2d.js"></script>

Usage

A new Vector2d instance can be instantiated with or without the new operator:

var v1 = new Vector2d();
var v2 = Vector2d();

Initial vector values can also be passed to the constructor function as a list of x y values, or as an array:

var v1 = Vector2d(4, 5);
var v2 = Vector2d([1, 1]);

The vectors can now be manipulated and queried using method calls.

v1.subtract(v2);

// v1 == [3, 4]
// v2 == [1, 1]

Vector operations are destructive

Meaning, the calling vector gets set to the resultant of the operation.

Using .clone() makes a copy of the vector to ensure operations are non-destructive.

var result = v1.clone().subtract(v2);

// result == [3, 4]
// v1 == [4, 5]
// v2 == [1, 1]

All methods which accept a vector parameter can also accept a 2 value array instead.

v1.add([3, 3]);
v1.multiply([2, 2]);

Method chaining

Methods which return a vector can be chained together.

v1.add(v2).scale(2).rotate(0.5);

vector2d.js's People

Contributors

rejacobson avatar

Watchers

 avatar James Cloos avatar

Forkers

mtlt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.