| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_GFX_POINT_BASE_H_ | 5 #ifndef UI_GFX_POINT_BASE_H_ |
| 6 #define UI_GFX_POINT_BASE_H_ | 6 #define UI_GFX_POINT_BASE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "ui/base/ui_export.h" | 12 #include "ui/base/ui_export.h" |
| 13 | 13 |
| 14 namespace gfx { | 14 namespace gfx { |
| 15 | 15 |
| 16 // A point has an x and y coordinate. | 16 // A point has an x and y coordinate. |
| 17 template<typename Class, typename Type> | 17 template<typename Class, typename Type, typename VectorClass> |
| 18 class UI_EXPORT PointBase { | 18 class UI_EXPORT PointBase { |
| 19 public: | 19 public: |
| 20 Type x() const { return x_; } | 20 Type x() const { return x_; } |
| 21 Type y() const { return y_; } | 21 Type y() const { return y_; } |
| 22 | 22 |
| 23 void SetPoint(Type x, Type y) { | 23 void SetPoint(Type x, Type y) { |
| 24 x_ = x; | 24 x_ = x; |
| 25 y_ = y; | 25 y_ = y; |
| 26 } | 26 } |
| 27 | 27 |
| 28 void set_x(Type x) { x_ = x; } | 28 void set_x(Type x) { x_ = x; } |
| 29 void set_y(Type y) { y_ = y; } | 29 void set_y(Type y) { y_ = y; } |
| 30 | 30 |
| 31 void Offset(Type delta_x, Type delta_y) { | 31 void Offset(Type delta_x, Type delta_y) { |
| 32 x_ += delta_x; | 32 x_ += delta_x; |
| 33 y_ += delta_y; | 33 y_ += delta_y; |
| 34 } | 34 } |
| 35 | 35 |
| 36 Class Add(const Class& other) const WARN_UNUSED_RESULT { | 36 Class Add(const VectorClass& other) const WARN_UNUSED_RESULT { |
| 37 const Class* orig = static_cast<const Class*>(this); | 37 const Class* orig = static_cast<const Class*>(this); |
| 38 Class copy = *orig; | 38 Class copy = *orig; |
| 39 copy.Offset(other.x_, other.y_); | 39 copy.Offset(other.x(), other.y()); |
| 40 return copy; | 40 return copy; |
| 41 } | 41 } |
| 42 | 42 |
| 43 Class Subtract(const Class& other) const WARN_UNUSED_RESULT { | 43 Class Subtract(const VectorClass& other) const WARN_UNUSED_RESULT { |
| 44 const Class* orig = static_cast<const Class*>(this); | 44 const Class* orig = static_cast<const Class*>(this); |
| 45 Class copy = *orig; | 45 Class copy = *orig; |
| 46 copy.Offset(-other.x_, -other.y_); | 46 copy.Offset(-other.x(), -other.y()); |
| 47 return copy; | 47 return copy; |
| 48 } | 48 } |
| 49 | 49 |
| 50 Class Middle(const Class& other) const WARN_UNUSED_RESULT { | 50 Class Middle(const Class& other) const WARN_UNUSED_RESULT { |
| 51 return Class((x_ + other.x_) / 2, (y_ + other.y_) / 2); | 51 return Class((x_ + other.x_) / 2, (y_ + other.y_) / 2); |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool IsOrigin() const { | 54 bool IsOrigin() const { |
| 55 return x_ == 0 && y_ == 0; | 55 return x_ == 0 && y_ == 0; |
| 56 } | 56 } |
| 57 | 57 |
| 58 VectorClass DistanceFromOrigin() const { |
| 59 return VectorClass(x_, y_); |
| 60 } |
| 61 |
| 62 VectorClass DistanceFrom(const Class& other) const { |
| 63 return VectorClass(x_ - other.x_, y_ - other.y_); |
| 64 } |
| 65 |
| 58 // A point is less than another point if its y-value is closer | 66 // A point is less than another point if its y-value is closer |
| 59 // to the origin. If the y-values are the same, then point with | 67 // to the origin. If the y-values are the same, then point with |
| 60 // the x-value closer to the origin is considered less than the | 68 // the x-value closer to the origin is considered less than the |
| 61 // other. | 69 // other. |
| 62 // This comparison is required to use Point in sets, or sorted | 70 // This comparison is required to use Point in sets, or sorted |
| 63 // vectors. | 71 // vectors. |
| 64 bool operator<(const Class& rhs) const { | 72 bool operator<(const Class& rhs) const { |
| 65 return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); | 73 return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); |
| 66 } | 74 } |
| 67 | 75 |
| 68 protected: | 76 protected: |
| 69 PointBase(Type x, Type y) : x_(x), y_(y) {} | 77 PointBase(Type x, Type y) : x_(x), y_(y) {} |
| 70 // Destructor is intentionally made non virtual and protected. | 78 // Destructor is intentionally made non virtual and protected. |
| 71 // Do not make this public. | 79 // Do not make this public. |
| 72 ~PointBase() {} | 80 ~PointBase() {} |
| 73 | 81 |
| 74 private: | 82 private: |
| 75 Type x_; | 83 Type x_; |
| 76 Type y_; | 84 Type y_; |
| 77 }; | 85 }; |
| 78 | 86 |
| 79 } // namespace gfx | 87 } // namespace gfx |
| 80 | 88 |
| 81 #endif // UI_GFX_POINT_BASE_H_ | 89 #endif // UI_GFX_POINT_BASE_H_ |
| OLD | NEW |