//geom2d.js

Vec2 = function(x, z){
    this.x = x;
    this.z = z;
}
 
Vec2.prototype.set = function(x,z){
    this.x = x;
    this.z = z;
    return this;
}
 
Vec2.prototype.copy = function() {
    return new Vec2(this.x, this.z);
}
 
Vec2.prototype.getLength = function() {
    return Math.sqrt(this.x*this.x+this.z*this.z);
}
 
Vec2.prototype.normalize = function() {
    var l = this.getLength();
    if(l > 0){
        this.x = this.x / l;
        this.z = this.z / l;
    }
    return this;
}
 
Vec2.prototype.getTheta = function() {
    var a = Math.atan2(this.z, this.x);
    if(a < 0){
        a += 2 * Math.PI;
    }
    return 180 * a / Math.PI;
}
 
Vec2.prototype.setTheta = function(theta) {
    if ((theta < -360) || (theta > 360)) {
        theta = theta % 360;
    }
    if (theta < 0) {
        theta = 360 + theta;
    }
    theta = Math.PI * theta / 180;
    var l = this.getLength();
    this.x = l * Math.cos(theta);
    this.z = l * Math.sin(theta);
    return this;
}
 
Vec2.prototype.addTheta = function(add) {
    var theta = this.getTheta();
    this.setTheta(theta+add);
    return this;
}
 
Vec2.prototype.add = function(a) {
    this.x = this.x + a.x;
    this.z = this.z + a.z;
    return this;
}
 
Vec2.prototype.sub = function(a) {
    this.x = this.x - a.x;
    this.z = this.z - a.z;
    return this;
}
 
Vec2.prototype.scale = function(s) {
    this.x = this.x * s;
    this.z = this.z * s;
    return this;
}
 
Vec2.prototype.setLength = function(l) {
    this.normalize();
    this.scale(l);
    return this;
}
 
Vec2.prototype.transform = function(m){
    this.x = m.a11 * this.x + m.a12 * this.z;
    this.z = m.a21 * this.x + m.a22 * this.z;
    return this;
}
 
Mat2x2 = function(a11, a12, a21, a22) {
    this.a11 = a11;
    this.a12 = a12;
    this.a21 = a21;
    this.a22 = a22;
}