42 lines
804 B
JavaScript
42 lines
804 B
JavaScript
/* VECTORS */
|
|
var vector = os.use('vector')
|
|
|
|
vector.random = function () {
|
|
var vec = [Math.random() - 0.5, Math.random() - 0.5];
|
|
return vector.norm(vec);
|
|
};
|
|
|
|
vector.direction = function (from, to) {
|
|
return vector.norm(to.sub(from));
|
|
};
|
|
|
|
vector.equal = function (v1, v2, tol) {
|
|
if (!tol) return v1.equal(v2);
|
|
|
|
var eql = true;
|
|
var c = v1.sub(v2);
|
|
|
|
c.forEach(function (x) {
|
|
if (!eql) return;
|
|
if (Math.abs(x) > tol) eql = false;
|
|
});
|
|
|
|
return eql;
|
|
};
|
|
|
|
vector.reflect = function (vec, plane) {
|
|
var p = vector.norm(plane);
|
|
return vec.sub(p.scale(2 * vector.dot(vec, p)));
|
|
};
|
|
|
|
vector.reflect_point = function (vec, point) {
|
|
return point.add(vec.sub(point).scale(-1));
|
|
};
|
|
|
|
vector.v2one = [1,1];
|
|
vector.v3one = [1,1,1];
|
|
vector.v2zero = [0,0];
|
|
vector.v3zero = [0,0,0];
|
|
|
|
return vector
|