1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 var EXPORTED_SYMBOLS = ["Point", "Rect"];
10 * Any method that takes an x and y may also take a point.
12 function Point(x, y) {
17 clone: function clone() {
18 return new Point(this.x, this.y);
21 set: function set(x, y) {
27 equals: function equals(x, y) {
28 return this.x == x && this.y == y;
31 toString: function toString() {
32 return "(" + this.x + "," + this.y + ")";
35 map: function map(f) {
36 this.x = f.call(this, this.x);
37 this.y = f.call(this, this.y);
41 add: function add(x, y) {
47 subtract: function subtract(x, y) {
53 scale: function scale(s) {
60 return this.x == 0 && this.y == 0;
65 function takePointOrArgs(f) {
66 return function(arg1, arg2) {
67 if (arg2 === undefined) {
68 return f.call(this, arg1.x, arg1.y);
70 return f.call(this, arg1, arg2);
74 for (let f of ["add", "subtract", "equals", "set"]) {
75 Point.prototype[f] = takePointOrArgs(Point.prototype[f]);
80 * Rect is a simple data structure for representation of a rectangle supporting
81 * many basic geometric operations.
83 * NOTE: Since its operations are closed, rectangles may be empty and will report
84 * non-positive widths and heights in that case.
87 function Rect(x, y, w, h) {
94 Rect.fromRect = function fromRect(r) {
95 return new Rect(r.left, r.top, r.right - r.left, r.bottom - r.top);
106 return this.right - this.left;
109 return this.bottom - this.top;
112 let diff = this.left - v;
117 let diff = this.top - v;
122 this.right = this.left + v;
125 this.bottom = this.top + v;
128 isEmpty: function isEmpty() {
129 return this.left >= this.right || this.top >= this.bottom;
132 setRect(x, y, w, h) {
141 setBounds(l, t, r, b) {
150 equals: function equals(other) {
153 ((this.isEmpty() && other.isEmpty()) ||
154 (this.top == other.top &&
155 this.left == other.left &&
156 this.bottom == other.bottom &&
157 this.right == other.right))
161 clone: function clone() {
165 this.right - this.left,
166 this.bottom - this.top
170 center: function center() {
171 if (this.isEmpty()) {
172 throw new Error("Empty rectangles do not have centers");
175 this.left + (this.right - this.left) / 2,
176 this.top + (this.bottom - this.top) / 2
181 this.top = other.top;
182 this.left = other.left;
183 this.bottom = other.bottom;
184 this.right = other.right;
200 "[" + this.x + "," + this.y + "," + this.width + "," + this.height + "]"
204 /** return a new rect that is the union of that one and this one */
206 return this.clone().expandToContain(other);
210 if (other.isEmpty()) {
213 if (this.isEmpty()) {
218 other.left >= this.left &&
219 other.right <= this.right &&
220 other.top >= this.top &&
221 other.bottom <= this.bottom
226 return this.clone().restrictTo(other);
230 if (this.isEmpty() || other.isEmpty()) {
234 let x1 = Math.max(this.left, other.left);
235 let x2 = Math.min(this.right, other.right);
236 let y1 = Math.max(this.top, other.top);
237 let y2 = Math.min(this.bottom, other.bottom);
238 return x1 < x2 && y1 < y2;
241 /** Restrict area of this rectangle to the intersection of both rectangles. */
242 restrictTo: function restrictTo(other) {
243 if (this.isEmpty() || other.isEmpty()) {
244 return this.setRect(0, 0, 0, 0);
247 let x1 = Math.max(this.left, other.left);
248 let x2 = Math.min(this.right, other.right);
249 let y1 = Math.max(this.top, other.top);
250 let y2 = Math.min(this.bottom, other.bottom);
251 // If width or height is 0, the intersection was empty.
252 return this.setRect(x1, y1, Math.max(0, x2 - x1), Math.max(0, y2 - y1));
255 /** Expand this rectangle to the union of both rectangles. */
256 expandToContain: function expandToContain(other) {
257 if (this.isEmpty()) {
258 return this.copyFrom(other);
260 if (other.isEmpty()) {
264 let l = Math.min(this.left, other.left);
265 let r = Math.max(this.right, other.right);
266 let t = Math.min(this.top, other.top);
267 let b = Math.max(this.bottom, other.bottom);
268 return this.setRect(l, t, r - l, b - t);
272 * Expands to the smallest rectangle that contains original rectangle and is bounded
273 * by lines with integer coefficients.
275 expandToIntegers: function round() {
276 this.left = Math.floor(this.left);
277 this.top = Math.floor(this.top);
278 this.right = Math.ceil(this.right);
279 this.bottom = Math.ceil(this.bottom);
283 scale: function scale(xscl, yscl) {
291 map: function map(f) {
292 this.left = f.call(this, this.left);
293 this.top = f.call(this, this.top);
294 this.right = f.call(this, this.right);
295 this.bottom = f.call(this, this.bottom);
299 /** Ensure this rectangle is inside the other, if possible. Preserves w, h. */
300 translateInside: function translateInside(other) {
302 if (this.left <= other.left) {
303 offsetX = other.left - this.left;
304 } else if (this.right > other.right) {
305 offsetX = other.right - this.right;
309 if (this.top <= other.top) {
310 offsetY = other.top - this.top;
311 } else if (this.bottom > other.bottom) {
312 offsetY = other.bottom - this.bottom;
315 return this.translate(offsetX, offsetY);
318 /** Subtract other area from this. Returns array of rects whose union is this-other. */
319 subtract: function subtract(other) {
320 let r = new Rect(0, 0, 0, 0);
322 other = other.intersect(this);
323 if (other.isEmpty()) {
324 return [this.clone()];
328 r.setBounds(this.left, this.top, other.left, this.bottom);
330 result.push(r.clone());
333 r.setBounds(other.left, this.top, other.right, other.top);
335 result.push(r.clone());
337 r.setBounds(other.left, other.bottom, other.right, this.bottom);
339 result.push(r.clone());
342 r.setBounds(other.right, this.top, this.right, this.bottom);
344 result.push(r.clone());
351 * Blends two rectangles together.
352 * @param rect Rectangle to blend this one with
353 * @param scalar Ratio from 0 (returns a clone of this rect) to 1 (clone of rect).
354 * @return New blended rectangle.
356 blend: function blend(rect, scalar) {
358 this.left + (rect.left - this.left) * scalar,
359 this.top + (rect.top - this.top) * scalar,
360 this.width + (rect.width - this.width) * scalar,
361 this.height + (rect.height - this.height) * scalar
366 * Grows or shrinks the rectangle while keeping the center point.
367 * Accepts single multipler, or separate for both axes.
369 inflate: function inflate(xscl, yscl) {
370 let xAdj = (this.width * xscl - this.width) / 2;
371 let s = arguments.length > 1 ? yscl : xscl;
372 let yAdj = (this.height * s - this.height) / 2;
381 * Grows or shrinks the rectangle by fixed amount while keeping the center point.
382 * Accepts single fixed amount
384 inflateFixed: function inflateFixed(fixed) {
388 this.bottom += fixed;