18ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// Copyright 2017 The Chromium Authors. All rights reserved.
28ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// Use of this source code is governed by a BSD-style license that can be
38ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// found in the LICENSE file.
48ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
58ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// Defines a simple integer rectangle class.  The containment semantics
68ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// are array-like; that is, the coordinate (x, y) is considered to be
78ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// contained by the rectangle, but the coordinate (x + width, y) is not.
88ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// The class will happily let you create malformed rectangles (that is,
98ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// rectangles with negative width and/or height), but there will be assertions
108ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// in the operations (such as Contains()) to complain in this case.
118ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
128ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin#ifndef RECT_H_
138ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin#define RECT_H_
148ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
158ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin#include <string>
168ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
178ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin#include "base/strings/stringprintf.h"
188ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin#include "size.h"
198ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
208ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Linnamespace media {
218ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
228ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// Helper struct for rect to replace gfx::Rect usage from original code.
238ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin// Only partial functions of gfx::Rect is implemented here.
248ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Linclass Rect {
258ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin public:
268ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  Rect() : x_(0), y_(0), size_(0, 0) {}
278ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  Rect(int width, int height) : x_(0), y_(0), size_(width, height) {}
288ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  Rect(int x, int y, int width, int height)
298ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin      : x_(x), y_(y), size_(width, height) {}
308ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  explicit Rect(const Size& size) : x_(0), y_(0), size_(size) {}
318ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
328ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  int x() const { return x_; }
338ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  void set_x(int x) { x_ = x; }
348ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
358ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  int y() const { return y_; }
368ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  void set_y(int y) { y_ = y; }
378ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
388ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  int width() const { return size_.width(); }
398ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  void set_width(int width) { size_.set_width(width); }
408ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
418ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  int height() const { return size_.height(); }
428ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  void set_height(int height) { size_.set_height(height); }
438ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
448ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  const Size& size() const { return size_; }
458ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  void set_size(const Size& size) {
468ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    set_width(size.width());
478ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    set_height(size.height());
488ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  }
498ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
508ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  constexpr int right() const { return x() + width(); }
518ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  constexpr int bottom() const { return y() + height(); }
528ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
538ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  void SetRect(int x, int y, int width, int height) {
548ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    set_x(x);
558ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    set_y(y);
568ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    set_width(width);
578ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    set_height(height);
588ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  }
598ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
608ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  // Returns true if the area of the rectangle is zero.
618ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  bool IsEmpty() const { return size_.IsEmpty(); }
628ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
638ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  // Returns true if this rectangle contains the specified rectangle.
648ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  bool Contains(const Rect& rect) const {
658ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
668ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin            rect.bottom() <= bottom());
678ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  }
688ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
698ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  std::string ToString() const {
708ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin    return base::StringPrintf("(%d,%d) %s",
718ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin                              x_, y_, size().ToString().c_str());
728ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  }
738ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
748ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin private:
758ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  int x_;
768ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  int y_;
778ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  Size size_;
788ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin};
798ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
808ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lininline bool operator==(const Rect& lhs, const Rect& rhs) {
818ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.size() == rhs.size();
828ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin}
838ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
848ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lininline bool operator!=(const Rect& lhs, const Rect& rhs) {
858ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin  return !(lhs == rhs);
868ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin}
878ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
888ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin}  // namespace media
898ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin
908ceb3cc8d22071ea416a8fd0d3a954fd73efa189Johny Lin#endif  // RECT_H_
91