Home | History | Annotate | Download | only in geometry
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/gfx/geometry/rect_conversions.h"
      6 
      7 #include <algorithm>
      8 #include <cmath>
      9 
     10 #include "base/logging.h"
     11 #include "ui/gfx/geometry/safe_integer_conversions.h"
     12 
     13 namespace gfx {
     14 
     15 Rect ToEnclosingRect(const RectF& rect) {
     16   int min_x = ToFlooredInt(rect.x());
     17   int min_y = ToFlooredInt(rect.y());
     18   float max_x = rect.right();
     19   float max_y = rect.bottom();
     20   int width = rect.width() == 0 ? 0 : std::max(ToCeiledInt(max_x) - min_x, 0);
     21   int height = rect.height() == 0 ? 0 : std::max(ToCeiledInt(max_y) - min_y, 0);
     22   return Rect(min_x, min_y, width, height);
     23 }
     24 
     25 Rect ToEnclosedRect(const RectF& rect) {
     26   int min_x = ToCeiledInt(rect.x());
     27   int min_y = ToCeiledInt(rect.y());
     28   float max_x = rect.right();
     29   float max_y = rect.bottom();
     30   int width = std::max(ToFlooredInt(max_x) - min_x, 0);
     31   int height = std::max(ToFlooredInt(max_y) - min_y, 0);
     32   return Rect(min_x, min_y, width, height);
     33 }
     34 
     35 Rect ToNearestRect(const RectF& rect) {
     36   float float_min_x = rect.x();
     37   float float_min_y = rect.y();
     38   float float_max_x = rect.right();
     39   float float_max_y = rect.bottom();
     40 
     41   int min_x = ToRoundedInt(float_min_x);
     42   int min_y = ToRoundedInt(float_min_y);
     43   int max_x = ToRoundedInt(float_max_x);
     44   int max_y = ToRoundedInt(float_max_y);
     45 
     46   // If these DCHECKs fail, you're using the wrong method, consider using
     47   // ToEnclosingRect or ToEnclosedRect instead.
     48   DCHECK(std::abs(min_x - float_min_x) < 0.01f);
     49   DCHECK(std::abs(min_y - float_min_y) < 0.01f);
     50   DCHECK(std::abs(max_x - float_max_x) < 0.01f);
     51   DCHECK(std::abs(max_y - float_max_y) < 0.01f);
     52 
     53   return Rect(min_x, min_y, max_x - min_x, max_y - min_y);
     54 }
     55 
     56 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) {
     57   float float_min_x = rect.x();
     58   float float_min_y = rect.y();
     59   float float_max_x = rect.right();
     60   float float_max_y = rect.bottom();
     61 
     62   int min_x = ToRoundedInt(float_min_x);
     63   int min_y = ToRoundedInt(float_min_y);
     64   int max_x = ToRoundedInt(float_max_x);
     65   int max_y = ToRoundedInt(float_max_y);
     66 
     67   return
     68       (std::abs(min_x - float_min_x) < distance) &&
     69       (std::abs(min_y - float_min_y) < distance) &&
     70       (std::abs(max_x - float_max_x) < distance) &&
     71       (std::abs(max_y - float_max_y) < distance);
     72 }
     73 
     74 Rect ToFlooredRectDeprecated(const RectF& rect) {
     75   return Rect(ToFlooredInt(rect.x()),
     76               ToFlooredInt(rect.y()),
     77               ToFlooredInt(rect.width()),
     78               ToFlooredInt(rect.height()));
     79 }
     80 
     81 }  // namespace gfx
     82 
     83