(function () { /** @namespace */ itm.Util = itm.Util || {}; itm.Util.Point = itm.Class( /** @lends itm.Util.Point# */ { /** * @class * @constructs * * @param {number|{x:number,y:number}|{top:number,left:number}}param * @param {number} [y] */ init: function (param, y) { this.set(param, y); }, /** * @param {number|{x:number,y:number}|{top:number,left:number}}point * @param {number} [y] * @returns {itm.Util.Point} this */ set: function (point, y) { if (typeof point == "number") { this.x = point; this.y = y != null ? y : point; } else { point = point || {}; this.x = point.x != null ? point.x : point.left != null ? point.left : 0; this.y = point.y != null ? point.y : point.top != null ? point.top : 0; } this.synch(); return this; }, /** * update top and left properties from y and x * @returns {itm.Util.Point} this */ synch: function () { this.top = this.y; this.left = this.x; return this; }, /** * @returns {itm.Util.Point} clone */ clone: function () { return new itm.Util.Point({x: this.x, y: this.y}); }, /** * @param {number|{x:number,y:number}|{top:number,left:number}}point * @param {number} [y] * @returns {itm.Util.Point} this */ add: function (point, y) { if (typeof point == "number") { this.x += point; this.y += y != null ? y : point; } else { this.x += point.x != null ? point.x : point.left; this.y += point.y != null ? point.y : point.top; } this.synch(); return this; }, /** * @param {number|{x:number,y:number}|{top:number,left:number}}point * @param {number} [y] * @returns {itm.Util.Point} this */ sub: function (point, y) { if (typeof point == "number") { this.x -= point; this.y -= y != null ? y : point; } else { this.x -= point.x != null ? point.x : point.left; this.y -= point.y != null ? point.y : point.top; } this.synch(); return this; }, /** * @param {number|{x:number,y:number}|{top:number,left:number}}point * @param {number} [y] * @returns {itm.Util.Point} this */ mul: function (point, y) { if (typeof point == "number") { this.x *= point; this.y *= y != null ? y : point; } else { this.x *= point.x != null ? point.x : point.left; this.y *= point.y != null ? point.y : point.top; } this.synch(); return this; }, /** * @param {number|{x:number,y:number}|{top:number,left:number}}point * @param {number} [y] * @returns {itm.Util.Point} this */ div: function (point, y) { if (typeof point == "number") { this.x /= point; this.y /= y != null ? y : point; } else { this.x /= point.x != null ? point.x : point.left; this.y /= point.y != null ? point.y : point.top; } this.synch(); return this; }, /** * @returns {itm.Util.Point} this */ negate: function () { this.x = -this.x; this.y = -this.y; this.synch(); return this; }, /** * @returns {itm.Util.Point} this */ invert: function () { this.x = 1 / this.x; this.y = 1 / this.y; this.synch(); return this; }, /** * @returns {{x: number, y: number}} */ getXY: function () { return {x: this.x, y: this.y}; }, /** * @returns {{top: number, left: number}} */ getTopLeft: function () { this.synch(); return {top: this.top, left: this.left}; }, /** * @param {number|{x:number,y:number}|{top:number,left:number}}point * @param {number} [y] * @returns {boolean} */ equals: function (point, y) { if (point instanceof itm.Util.Point) { return this.x == point.x && this.y == point.y; } else { return this.equals(new itm.Util.Point(point, y)); } }, /** * @param {function} fn * @returns {itm.Util.Point} this */ doMath: function (fn) { this.x = fn(this.x); this.y = fn(this.y); this.synch(); return this; }, /** * @returns {itm.Util.Point} this */ floor: function () { return this.doMath(Math.floor); }, /** * @returns {itm.Util.Point} this */ round: function () { return this.doMath(Math.round); }, /** * @returns {itm.Util.Point} this */ ceil: function () { return this.doMath(Math.ceil); }, /** * @returns {itm.Util.Point} this */ abs: function () { return this.doMath(Math.abs); }, /** * @returns {number} */ length: function () { return Math.sqrt(this.x * this.x + this.y * this.y); } }); })();