origami demo
by jmchen
JavaScript
function Vector (x,y) {
this.x = x || 0;
this.y = y || 0;
};
function Matrix (e00,e01,e10,e11) {
this.element = [e00, e01, e10, e11];
}
Vector.prototype.dot = function (a) {
return this.x * a.x + this.y*a.y;
}
Vector.prototype.abs2 = function () {
return (this.x*this.x + this.y*this.y);
};
Vector.prototype.angle = function (a) {
return Math.acos(this.dot(a)/Math.sqrt (a.abs2() * this.abs2()));
}
Vector.prototype.add = function (a,b) {
this.x += a;
this.y += b;
};
Vector.prototype.affine = function (m, v) {
if (! (m instanceof Matrix))
throw new Error ('matrix*vector');
this.x = m.element[0]*v.x + m.element[1]*v.y
this.y = m.element[2]*v.x + m.element[3]*v.y;
}
/*
var v = new Vector (-1,0);
v.add (2,3); // v = (1, 3)
var m = new Matrix (1,2,0,1);
var result = new Vector();
//result.affine (v, m);
result.affine (m, v);
console.log (result); // (7,3)
*/
var v1 = new Vector (1,0);
var v2 = new Vector (0,-3);
console.log (v2.angle (v1));