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