Giter Club home page Giter Club logo

victor's Introduction

Victor

Build Status

A Javascript 2D Vector Maths Library, built using the UMD standards, so you can use it as a global object or with any module loader. It works in both Node.js and the browser.

Check out the website for documentation.

Installation

Node.js / Browserify

npm install victor --save
var Victor = require('victor');
var vec = new Victor(42, 1337);

Bower

bower install victor --save

Global object

Include the pre-built script.

<script src="./build/victor.js"></script>
<script>
var vec = new Victor(42, 1337);
</script>

Build & test

npm run build
npm test

Contributors

Ordered by date of first contribution. Auto-generated on Mon, 31 Aug 2015 13:08:12 GMT.

License

MIT

victor's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

victor's Issues

Vector projection?

Vector projection would be a very useful feature.

It could work like so:

var a = new Victor(100, 150);
var b = new Victor(300, 80);

var projection = a.projectTo(b); // returns a Victor object

Victor JS Fails Silently (Needs better error warnings)

It's easy to do a lot of things by accident IE: Using a scalar in ".multiply()" method, or taking the dot product of a scalar instead of another vector.

When this happens, these functions usually return NaN, and continue on without issue, except that you now have a NaN floating about that you have to figure out where it came from. It would be nice if there was at least some console warnings letting you know that you screwed something up instead of trying to work your way backwards.

Scalar multiply/divide

I know you can just v.multiply(new Victor(n, n)) where n is the scalar you want, but this is kind of a hack, and a dedicated scalar multiplication method would be wonderful.

.fromAngle[Rad|Deg]( angle ) ?

Maybe add a .fromAngleRad( angle ) such as:

Victor.fromAngleRad = function ( angle ) {
    return new Victor(Math.cos( angle ), Math.sin( angle ));
}

Immutable version

It was very annoying for me that methods like add or divide modified the vector. My temporary solution is my immutable-vector2d library. Do you have any ideas as how to merge my project into this one?

Angle between two vectors?

Could we get a method for angle between two vectors?`

Something like:

var v1 = new Victor(10, 10);
var v2 = new Victor(20, 20);
var angle = v1.angle(v2);

This would be inherently useful for game programming.

rotateDeg

Not sure what is wrong, but if I do the following :

var v = new Victor(0,1);
var y = v.clone().rotateDeg(180);
console.log(y);

I was expecting x:0 y:-1, instead I get
x: -1.2246467991473532e-16
y: -1

Rotation is broken

These are all expected to return the same vector back but rotate them 45 degrees instead.

(new Victor(1,1)).rotateBy(0)
(new Victor(0,1)).rotateBy(0)
(new Victor(0,-1)).rotateBy(0)
(new Victor(-1,-1)).rotateBy(0)

Adding a makeOrthogonal method

I've got a need for finding an orthogonal (perpendicular) vector of a certain magnitude in one of my projects.

I have a function that does the job, but I'd prefer to make it a part of Victor.js. Here's the function:

  /**
   * Returns a new vector thats orthogonal/perpendicular to the given
   * vector.
   * Mathematical rules:
   * - the dot product of two orthogonal vectors is zero.
   * - the magnitude of a 2d vector is the square root of the sum of
   *   its components.
   * - ^ two equations. components = the two unknowns.
   */
  getOrthogonal(vector, magnitude) {
    magnitude = magnitude || 1
    if (vector.x === 0) {
      return new Victor(magnitude, 0)
    }
    if (vector.y === 0) {
      return new Victor(0, magnitude)
    }
    const j = Math.sqrt((Math.pow(magnitude, 2) /
                         (1 + (Math.pow(vector.y, 2) /
                               Math.pow(vector.x, 2)))))
    const i = -(vector.y / vector.x) * j
    return new Victor(i, j)
  }

I was wondering if you would accept a merge request for a method that would work as follows:

var vec1 = new Victor(100, 200);
var vec2 = vec1.makeOrthogonal(10);
// vec 2 is a vector with a magnitude of 10 that's orthogonal to vec1 

Publish builded version to npm

Hi!

I have issue, written in name of topic. I haven't commonjs in my project, but victor is great, but i cannot use it in my project, because npm module contain commonjs code.

I can prepare PR for fix this question, if you agree.

Missing clamp method

The current implementation of limit assumes that a multiplier should affect both components of the vector. Other libraries would just clamp the values to a given min, max range. Have you thought about adding a clamp method?

Cheers

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.