Home | History | Annotate | Download | only in gfx
      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 #ifndef UI_GFX_POINT_BASE_H_
      6 #define UI_GFX_POINT_BASE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "build/build_config.h"
     12 #include "ui/gfx/gfx_export.h"
     13 
     14 namespace gfx {
     15 
     16 // A point has an x and y coordinate.
     17 template<typename Class, typename Type, typename VectorClass>
     18 class GFX_EXPORT PointBase {
     19  public:
     20   Type x() const { return x_; }
     21   Type y() const { return y_; }
     22 
     23   void SetPoint(Type x, Type y) {
     24     x_ = x;
     25     y_ = y;
     26   }
     27 
     28   void set_x(Type x) { x_ = x; }
     29   void set_y(Type y) { y_ = y; }
     30 
     31   void Offset(Type delta_x, Type delta_y) {
     32     x_ += delta_x;
     33     y_ += delta_y;
     34   }
     35 
     36   void operator+=(const VectorClass& vector) {
     37     x_ += vector.x();
     38     y_ += vector.y();
     39   }
     40 
     41   void operator-=(const VectorClass& vector) {
     42     x_ -= vector.x();
     43     y_ -= vector.y();
     44   }
     45 
     46   void SetToMin(const Class& other) {
     47     x_ = x_ <= other.x_ ? x_ : other.x_;
     48     y_ = y_ <= other.y_ ? y_ : other.y_;
     49   }
     50 
     51   void SetToMax(const Class& other) {
     52     x_ = x_ >= other.x_ ? x_ : other.x_;
     53     y_ = y_ >= other.y_ ? y_ : other.y_;
     54   }
     55 
     56   bool IsOrigin() const {
     57     return x_ == 0 && y_ == 0;
     58   }
     59 
     60   VectorClass OffsetFromOrigin() const {
     61     return VectorClass(x_, y_);
     62   }
     63 
     64   // A point is less than another point if its y-value is closer
     65   // to the origin. If the y-values are the same, then point with
     66   // the x-value closer to the origin is considered less than the
     67   // other.
     68   // This comparison is required to use Point in sets, or sorted
     69   // vectors.
     70   bool operator<(const Class& rhs) const {
     71     return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_);
     72   }
     73 
     74  protected:
     75   PointBase(Type x, Type y) : x_(x), y_(y) {}
     76   // Destructor is intentionally made non virtual and protected.
     77   // Do not make this public.
     78   ~PointBase() {}
     79 
     80  private:
     81   Type x_;
     82   Type y_;
     83 };
     84 
     85 }  // namespace gfx
     86 
     87 #endif  // UI_GFX_POINT_BASE_H_
     88