maxkueng / victor Goto Github PK
View Code? Open in Web Editor NEWA JavaScript 2D vector class with methods for common vector operations
Home Page: http://victorjs.org/
License: MIT License
A JavaScript 2D vector class with methods for common vector operations
Home Page: http://victorjs.org/
License: MIT License
Maybe add a .fromAngleRad( angle ) such as:
Victor.fromAngleRad = function ( angle ) {
return new Victor(Math.cos( angle ), Math.sin( angle ));
}
// As per documentation, the following
var vec = new Victor(100, 0);
vec.rotateBy(Math.PI / 2);
vec.toString();
// => "x:6.123233995736766e-15, y:100"
x should be zero
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.
It would be nice to have a way of reflecting one vector across another vector.
http://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector
Documentation for divide says that the argument is scalar but the example says it's Victor
.
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)
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
All the rotate commands should probably specify either a clockwise or counterclockwise frame of reference.
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
Since Victor#toFixed() uses Number#toFixed() under the hood, it is turning the x and y coordinates of the vector into strings: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
Victor currently doesn't have any way of setting a vector to be the image of itself projected onto another vector. Similarly, there is no notion of rejecting a vector.
This would be useful for doing various sorts of 2D physics--calculating restitution vectors and the like. :)
Releveant reference:
http://en.wikipedia.org/wiki/Vector_projection
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.
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.
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?
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
//theoretically, console should print (0, 100)
var vec = new Victor(0, 100);
vec.rotate(Math.PI / 2);
console.log(vec);
//(-100, 0)
//theoretically, console should print (-100, 0)
var vec = new Victor(0, 100);
vec.rotateBy(Math.PI / 2);
console.log(vec);
//(0, -100), rotate by this.angle() + PI / 2
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.
params for both rotateBy and rotateByDegrees say to use radians, rotateByDegree should say
Number rotation Rotation in "degrees" ?
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
Shouldn't it be renamed substract ?
An option to set the magnitude of a vector would be welcome!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.