Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Unified Diff: ui/gfx/geometry/rect_f.cc

Issue 506803004: gfx: Don't template gfx::Rect and gfx::RectF (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: inlines: rm-rectbase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/geometry/rect_f.h ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/rect_f.cc
diff --git a/ui/gfx/geometry/rect_f.cc b/ui/gfx/geometry/rect_f.cc
index 44c08aafaff5506edc621ba9fe4d632600d68f90..c87ea3de6ac05f9fb700a92a3ff28b874244f3b4 100644
--- a/ui/gfx/geometry/rect_f.cc
+++ b/ui/gfx/geometry/rect_f.cc
@@ -9,15 +9,201 @@
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "ui/gfx/geometry/insets_f.h"
-#include "ui/gfx/geometry/rect_base_impl.h"
#include "ui/gfx/geometry/safe_integer_conversions.h"
namespace gfx {
-template class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float>;
+static void AdjustAlongAxis(float dst_origin,
+ float dst_size,
+ float* origin,
+ float* size) {
+ *size = std::min(dst_size, *size);
+ if (*origin < dst_origin)
+ *origin = dst_origin;
+ else
+ *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
+}
+
+void RectF::Inset(const InsetsF& insets) {
+ Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
+}
+
+void RectF::Inset(float left, float top, float right, float bottom) {
+ origin_ += Vector2dF(left, top);
+ set_width(std::max(width() - left - right, static_cast<float>(0)));
+ set_height(std::max(height() - top - bottom, static_cast<float>(0)));
+}
+
+void RectF::Offset(float horizontal, float vertical) {
+ origin_ += Vector2dF(horizontal, vertical);
+}
+
+void RectF::operator+=(const Vector2dF& offset) {
+ origin_ += offset;
+}
+
+void RectF::operator-=(const Vector2dF& offset) {
+ origin_ -= offset;
+}
+
+InsetsF RectF::InsetsFrom(const RectF& inner) const {
+ return InsetsF(inner.y() - y(),
+ inner.x() - x(),
+ bottom() - inner.bottom(),
+ right() - inner.right());
+}
+
+bool RectF::operator<(const RectF& other) const {
+ if (origin_ == other.origin_) {
+ if (width() == other.width()) {
+ return height() < other.height();
+ } else {
+ return width() < other.width();
+ }
+ } else {
+ return origin_ < other.origin_;
+ }
+}
+
+bool RectF::Contains(float point_x, float point_y) const {
+ return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
+ (point_y < bottom());
+}
+
+bool RectF::Contains(const RectF& rect) const {
+ return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
+ rect.bottom() <= bottom());
+}
+
+bool RectF::Intersects(const RectF& rect) const {
+ return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
+ rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
+}
+
+void RectF::Intersect(const RectF& rect) {
+ if (IsEmpty() || rect.IsEmpty()) {
+ SetRect(0, 0, 0, 0);
+ return;
+ }
+
+ float rx = std::max(x(), rect.x());
+ float ry = std::max(y(), rect.y());
+ float rr = std::min(right(), rect.right());
+ float rb = std::min(bottom(), rect.bottom());
+
+ if (rx >= rr || ry >= rb)
+ rx = ry = rr = rb = 0; // non-intersecting
+
+ SetRect(rx, ry, rr - rx, rb - ry);
+}
-typedef class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF,
- float> RectBaseT;
+void RectF::Union(const RectF& rect) {
+ if (IsEmpty()) {
+ *this = rect;
+ return;
+ }
+ if (rect.IsEmpty())
+ return;
+
+ float rx = std::min(x(), rect.x());
+ float ry = std::min(y(), rect.y());
+ float rr = std::max(right(), rect.right());
+ float rb = std::max(bottom(), rect.bottom());
+
+ SetRect(rx, ry, rr - rx, rb - ry);
+}
+
+void RectF::Subtract(const RectF& rect) {
+ if (!Intersects(rect))
+ return;
+ if (rect.Contains(*static_cast<const RectF*>(this))) {
+ SetRect(0, 0, 0, 0);
+ return;
+ }
+
+ float rx = x();
+ float ry = y();
+ float rr = right();
+ float rb = bottom();
+
+ if (rect.y() <= y() && rect.bottom() >= bottom()) {
+ // complete intersection in the y-direction
+ if (rect.x() <= x()) {
+ rx = rect.right();
+ } else if (rect.right() >= right()) {
+ rr = rect.x();
+ }
+ } else if (rect.x() <= x() && rect.right() >= right()) {
+ // complete intersection in the x-direction
+ if (rect.y() <= y()) {
+ ry = rect.bottom();
+ } else if (rect.bottom() >= bottom()) {
+ rb = rect.y();
+ }
+ }
+ SetRect(rx, ry, rr - rx, rb - ry);
+}
+
+void RectF::AdjustToFit(const RectF& rect) {
+ float new_x = x();
+ float new_y = y();
+ float new_width = width();
+ float new_height = height();
+ AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
+ AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
+ SetRect(new_x, new_y, new_width, new_height);
+}
+
+PointF RectF::CenterPoint() const {
+ return PointF(x() + width() / 2, y() + height() / 2);
+}
+
+void RectF::ClampToCenteredSize(const SizeF& size) {
+ float new_width = std::min(width(), size.width());
+ float new_height = std::min(height(), size.height());
+ float new_x = x() + (width() - new_width) / 2;
+ float new_y = y() + (height() - new_height) / 2;
+ SetRect(new_x, new_y, new_width, new_height);
+}
+
+void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
+ DCHECK(left_half);
+ DCHECK(right_half);
+
+ left_half->SetRect(x(), y(), width() / 2, height());
+ right_half->SetRect(
+ left_half->right(), y(), width() - left_half->width(), height());
+}
+
+bool RectF::SharesEdgeWith(const RectF& rect) const {
+ return (y() == rect.y() && height() == rect.height() &&
+ (x() == rect.right() || right() == rect.x())) ||
+ (x() == rect.x() && width() == rect.width() &&
+ (y() == rect.bottom() || bottom() == rect.y()));
+}
+
+float RectF::ManhattanDistanceToPoint(const PointF& point) const {
+ float x_distance =
+ std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
+ float y_distance =
+ std::max<float>(0, std::max(y() - point.y(), point.y() - bottom()));
+
+ return x_distance + y_distance;
+}
+
+float RectF::ManhattanInternalDistance(const RectF& rect) const {
+ RectF c(*this);
+ c.Union(rect);
+
+ static const float kEpsilon = std::numeric_limits<float>::is_integer
+ ? 1
+ : std::numeric_limits<float>::epsilon();
+
+ float x = std::max<float>(0, c.width() - width() - rect.width() + kEpsilon);
+ float y =
+ std::max<float>(0, c.height() - height() - rect.height() + kEpsilon);
+ return x + y;
+}
bool RectF::IsExpressibleAsRect() const {
return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) &&
« no previous file with comments | « ui/gfx/geometry/rect_f.h ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698