 this.x + "," + this.y + "," + this.width + "," + this.height + "]"
    );
  },

  /** return a new rect that is the union of that one and this one */
  union(other) {
    return this.clone().expandToContain(other);
  },

  contains(other) {
    if (other.isEmpty()) {
      return true;
    }
    if (this.isEmpty()) {
      return false;
    }

    return (
      other.left >= this.left &&
      other.right <= this.right &&
      other.top >= this.top &&
      other.bottom <= this.bottom
    );
  },

  intersect(other) {
    return this.clone().restrictTo(other);
  },

  intersects(other) {
    if (this.isEmpty() || other.isEmpty()) {
      return false;
    }

    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);
    }

    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);
    }
    if (other.isEmpty()) {
      return this;
    }

    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);
    return this.setRect(l, t, r - l, b - t);
  },

  /**
   * Expands to the smallest rectangle that contains original rectangle and is bounded
   * by lines with integer coefficients.
   */
  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);
    return this;
  },

  scale: function scale(xscl, yscl) {
    this.left *= xscl;
    this.right *= xscl;
    this.top *= yscl;
    this.bottom *= yscl;
    return this;
  },

  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);
    return this;
  },

  /** Ensure this rectangle is inside the other, if possible. Preserves w, h. */
  translateInside: function translateInside(other) {
    let offsetX = 0;
    if (this.left <= other.left) {
      offsetX = other.left - this.left;
    } else if (this.right > other.right) {
      offsetX = other.right - this.right;
    }

    let offsetY = 0;
    if (this.top <= other.top) {
      offsetY = other.top - this.top;
    } else if (this.bottom > other.bottom) {
      offsetY = other.bottom - this.bottom;
    }

    return this.translate(offsetX, offsetY);
  },

  /** 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()];
    }

    // left strip
    r.setBounds(this.left, this.top, other.left, this.bottom);
    if (!r.isEmpty()) {
      result.push(r.clone());
    }
    // inside strip
    r.setBounds(other.left, this.top, other.right, other.top);
    if (!r.isEmpty()) {
      result.push(r.clone());
    }
    r.setBounds(other.left, other.bottom, other.right, this.bottom);
    if (!r.isEmpty()) {
      result.push(r.clone());
    }
    // right strip
    r.setBounds(other.right, this.top, this.right, this.bottom);
    if (!r.isEmpty()) {
      result.push(r.clone());
    }

    return result;
  },

  /**
   * 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(
      this.left + (rect.left - this.left) * scalar,
      this.top + (rect.top - this.top) * scalar,
      this.width + (rect.width - this.width) * scalar,
      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;
  },

  /**
   * 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;
  },
};
PK