michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = ["Point", "Rect"]; michael@0: michael@0: /** michael@0: * Simple Point class. michael@0: * michael@0: * Any method that takes an x and y may also take a point. michael@0: */ michael@0: this.Point = function Point(x, y) { michael@0: this.set(x, y); michael@0: } michael@0: michael@0: Point.prototype = { michael@0: clone: function clone() { michael@0: return new Point(this.x, this.y); michael@0: }, michael@0: michael@0: set: function set(x, y) { michael@0: this.x = x; michael@0: this.y = y; michael@0: return this; michael@0: }, michael@0: michael@0: equals: function equals(x, y) { michael@0: return this.x == x && this.y == y; michael@0: }, michael@0: michael@0: toString: function toString() { michael@0: return "(" + this.x + "," + this.y + ")"; michael@0: }, michael@0: michael@0: map: function map(f) { michael@0: this.x = f.call(this, this.x); michael@0: this.y = f.call(this, this.y); michael@0: return this; michael@0: }, michael@0: michael@0: add: function add(x, y) { michael@0: this.x += x; michael@0: this.y += y; michael@0: return this; michael@0: }, michael@0: michael@0: subtract: function subtract(x, y) { michael@0: this.x -= x; michael@0: this.y -= y; michael@0: return this; michael@0: }, michael@0: michael@0: scale: function scale(s) { michael@0: this.x *= s; michael@0: this.y *= s; michael@0: return this; michael@0: }, michael@0: michael@0: isZero: function() { michael@0: return this.x == 0 && this.y == 0; michael@0: } michael@0: }; michael@0: michael@0: (function() { michael@0: function takePointOrArgs(f) { michael@0: return function(arg1, arg2) { michael@0: if (arg2 === undefined) michael@0: return f.call(this, arg1.x, arg1.y); michael@0: else michael@0: return f.call(this, arg1, arg2); michael@0: }; michael@0: } michael@0: michael@0: for each (let f in ['add', 'subtract', 'equals', 'set']) michael@0: Point.prototype[f] = takePointOrArgs(Point.prototype[f]); michael@0: })(); michael@0: michael@0: michael@0: /** michael@0: * Rect is a simple data structure for representation of a rectangle supporting michael@0: * many basic geometric operations. michael@0: * michael@0: * NOTE: Since its operations are closed, rectangles may be empty and will report michael@0: * non-positive widths and heights in that case. michael@0: */ michael@0: michael@0: this.Rect = function Rect(x, y, w, h) { michael@0: this.left = x; michael@0: this.top = y; michael@0: this.right = x + w; michael@0: this.bottom = y + h; michael@0: }; michael@0: michael@0: Rect.fromRect = function fromRect(r) { michael@0: return new Rect(r.left, r.top, r.right - r.left, r.bottom - r.top); michael@0: }; michael@0: michael@0: Rect.prototype = { michael@0: get x() { return this.left; }, michael@0: get y() { return this.top; }, michael@0: get width() { return this.right - this.left; }, michael@0: get height() { return this.bottom - this.top; }, michael@0: set x(v) { michael@0: let diff = this.left - v; michael@0: this.left = v; michael@0: this.right -= diff; michael@0: }, michael@0: set y(v) { michael@0: let diff = this.top - v; michael@0: this.top = v; michael@0: this.bottom -= diff; michael@0: }, michael@0: set width(v) { this.right = this.left + v; }, michael@0: set height(v) { this.bottom = this.top + v; }, michael@0: michael@0: isEmpty: function isEmpty() { michael@0: return this.left >= this.right || this.top >= this.bottom; michael@0: }, michael@0: michael@0: setRect: function(x, y, w, h) { michael@0: this.left = x; michael@0: this.top = y; michael@0: this.right = x+w; michael@0: this.bottom = y+h; michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: setBounds: function(l, t, r, b) { michael@0: this.top = t; michael@0: this.left = l; michael@0: this.bottom = b; michael@0: this.right = r; michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: equals: function equals(other) { michael@0: return other != null && michael@0: (this.isEmpty() && other.isEmpty() || michael@0: this.top == other.top && michael@0: this.left == other.left && michael@0: this.bottom == other.bottom && michael@0: this.right == other.right); michael@0: }, michael@0: michael@0: clone: function clone() { michael@0: return new Rect(this.left, this.top, this.right - this.left, this.bottom - this.top); michael@0: }, michael@0: michael@0: center: function center() { michael@0: if (this.isEmpty()) michael@0: throw "Empty rectangles do not have centers"; michael@0: return new Point(this.left + (this.right - this.left) / 2, michael@0: this.top + (this.bottom - this.top) / 2); michael@0: }, michael@0: michael@0: copyFrom: function(other) { michael@0: this.top = other.top; michael@0: this.left = other.left; michael@0: this.bottom = other.bottom; michael@0: this.right = other.right; michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: translate: function(x, y) { michael@0: this.left += x; michael@0: this.right += x; michael@0: this.top += y; michael@0: this.bottom += y; michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: toString: function() { michael@0: return "[" + this.x + "," + this.y + "," + this.width + "," + this.height + "]"; michael@0: }, michael@0: michael@0: /** return a new rect that is the union of that one and this one */ michael@0: union: function(other) { michael@0: return this.clone().expandToContain(other); michael@0: }, michael@0: michael@0: contains: function(other) { michael@0: if (other.isEmpty()) return true; michael@0: if (this.isEmpty()) return false; michael@0: michael@0: return (other.left >= this.left && michael@0: other.right <= this.right && michael@0: other.top >= this.top && michael@0: other.bottom <= this.bottom); michael@0: }, michael@0: michael@0: intersect: function(other) { michael@0: return this.clone().restrictTo(other); michael@0: }, michael@0: michael@0: intersects: function(other) { michael@0: if (this.isEmpty() || other.isEmpty()) michael@0: return false; michael@0: michael@0: let x1 = Math.max(this.left, other.left); michael@0: let x2 = Math.min(this.right, other.right); michael@0: let y1 = Math.max(this.top, other.top); michael@0: let y2 = Math.min(this.bottom, other.bottom); michael@0: return x1 < x2 && y1 < y2; michael@0: }, michael@0: michael@0: /** Restrict area of this rectangle to the intersection of both rectangles. */ michael@0: restrictTo: function restrictTo(other) { michael@0: if (this.isEmpty() || other.isEmpty()) michael@0: return this.setRect(0, 0, 0, 0); michael@0: michael@0: let x1 = Math.max(this.left, other.left); michael@0: let x2 = Math.min(this.right, other.right); michael@0: let y1 = Math.max(this.top, other.top); michael@0: let y2 = Math.min(this.bottom, other.bottom); michael@0: // If width or height is 0, the intersection was empty. michael@0: return this.setRect(x1, y1, Math.max(0, x2 - x1), Math.max(0, y2 - y1)); michael@0: }, michael@0: michael@0: /** Expand this rectangle to the union of both rectangles. */ michael@0: expandToContain: function expandToContain(other) { michael@0: if (this.isEmpty()) return this.copyFrom(other); michael@0: if (other.isEmpty()) return this; michael@0: michael@0: let l = Math.min(this.left, other.left); michael@0: let r = Math.max(this.right, other.right); michael@0: let t = Math.min(this.top, other.top); michael@0: let b = Math.max(this.bottom, other.bottom); michael@0: return this.setRect(l, t, r-l, b-t); michael@0: }, michael@0: michael@0: /** michael@0: * Expands to the smallest rectangle that contains original rectangle and is bounded michael@0: * by lines with integer coefficients. michael@0: */ michael@0: expandToIntegers: function round() { michael@0: this.left = Math.floor(this.left); michael@0: this.top = Math.floor(this.top); michael@0: this.right = Math.ceil(this.right); michael@0: this.bottom = Math.ceil(this.bottom); michael@0: return this; michael@0: }, michael@0: michael@0: scale: function scale(xscl, yscl) { michael@0: this.left *= xscl; michael@0: this.right *= xscl; michael@0: this.top *= yscl; michael@0: this.bottom *= yscl; michael@0: return this; michael@0: }, michael@0: michael@0: map: function map(f) { michael@0: this.left = f.call(this, this.left); michael@0: this.top = f.call(this, this.top); michael@0: this.right = f.call(this, this.right); michael@0: this.bottom = f.call(this, this.bottom); michael@0: return this; michael@0: }, michael@0: michael@0: /** Ensure this rectangle is inside the other, if possible. Preserves w, h. */ michael@0: translateInside: function translateInside(other) { michael@0: let offsetX = (this.left <= other.left ? other.left - this.left : michael@0: (this.right > other.right ? other.right - this.right : 0)); michael@0: let offsetY = (this.top <= other.top ? other.top - this.top : michael@0: (this.bottom > other.bottom ? other.bottom - this.bottom : 0)); michael@0: return this.translate(offsetX, offsetY); michael@0: }, michael@0: michael@0: /** Subtract other area from this. Returns array of rects whose union is this-other. */ michael@0: subtract: function subtract(other) { michael@0: let r = new Rect(0, 0, 0, 0); michael@0: let result = []; michael@0: other = other.intersect(this); michael@0: if (other.isEmpty()) michael@0: return [this.clone()]; michael@0: michael@0: // left strip michael@0: r.setBounds(this.left, this.top, other.left, this.bottom); michael@0: if (!r.isEmpty()) michael@0: result.push(r.clone()); michael@0: // inside strip michael@0: r.setBounds(other.left, this.top, other.right, other.top); michael@0: if (!r.isEmpty()) michael@0: result.push(r.clone()); michael@0: r.setBounds(other.left, other.bottom, other.right, this.bottom); michael@0: if (!r.isEmpty()) michael@0: result.push(r.clone()); michael@0: // right strip michael@0: r.setBounds(other.right, this.top, this.right, this.bottom); michael@0: if (!r.isEmpty()) michael@0: result.push(r.clone()); michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: /** michael@0: * Blends two rectangles together. michael@0: * @param rect Rectangle to blend this one with michael@0: * @param scalar Ratio from 0 (returns a clone of this rect) to 1 (clone of rect). michael@0: * @return New blended rectangle. michael@0: */ michael@0: blend: function blend(rect, scalar) { michael@0: return new Rect( michael@0: this.left + (rect.left - this.left ) * scalar, michael@0: this.top + (rect.top - this.top ) * scalar, michael@0: this.width + (rect.width - this.width ) * scalar, michael@0: this.height + (rect.height - this.height) * scalar); michael@0: }, michael@0: michael@0: /** michael@0: * Grows or shrinks the rectangle while keeping the center point. michael@0: * Accepts single multipler, or separate for both axes. michael@0: */ michael@0: inflate: function inflate(xscl, yscl) { michael@0: let xAdj = (this.width * xscl - this.width) / 2; michael@0: let s = (arguments.length > 1) ? yscl : xscl; michael@0: let yAdj = (this.height * s - this.height) / 2; michael@0: this.left -= xAdj; michael@0: this.right += xAdj; michael@0: this.top -= yAdj; michael@0: this.bottom += yAdj; michael@0: return this; michael@0: } michael@0: };