asciiflow2/js-lib/common.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-01-08 23:06:08 +00:00
/**
* Common classes.
*/
goog.provide('ascii.Vector');
2014-01-08 23:06:08 +00:00
/**
* @constructor
2014-01-12 10:37:38 +00:00
* @param {number} x
* @param {number} y
2014-01-08 23:06:08 +00:00
*/
ascii.Vector = function(x, y) {
2014-01-08 23:06:08 +00:00
/** type {Number} */ this.x = x;
/** type {Number} */ this.y = y;
};
2014-01-09 20:18:46 +00:00
2014-01-12 10:37:38 +00:00
/**
* @param {ascii.Vector} other
* @return {boolean}
*/
ascii.Vector.prototype.equals = function(other) {
2014-01-12 14:38:56 +00:00
return (other != null) && (this.x == other.x) && (this.y == other.y);
2014-01-09 20:18:46 +00:00
};
2014-01-12 10:37:38 +00:00
/**
* @param {ascii.Vector} other
2014-01-12 10:48:39 +00:00
* @return {ascii.Vector}
2014-01-12 10:37:38 +00:00
*/
ascii.Vector.prototype.subtract = function(other) {
return new ascii.Vector(this.x - other.x, this.y - other.y);
};
2014-01-12 10:37:38 +00:00
/**
* @param {ascii.Vector} other
2014-01-12 10:48:39 +00:00
* @return {ascii.Vector}
2014-01-12 10:37:38 +00:00
*/
ascii.Vector.prototype.add = function(other) {
return new ascii.Vector(this.x + other.x, this.y + other.y);
};
/** @return {number} */
ascii.Vector.prototype.length = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
2014-01-12 10:37:38 +00:00
/**
* @param {number} scale
* @return {ascii.Vector}
*/
ascii.Vector.prototype.scale = function(scale) {
return new ascii.Vector(this.x * scale, this.y * scale);
};