1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef GEOMETRY_OFFSET_H 17 #define GEOMETRY_OFFSET_H 18 19 #include <limits> 20 21 #include "size.h" 22 #include "utils.h" 23 #include "ui_model.h" 24 25 namespace OHOS::uitest { 26 class Offset { 27 public: 28 Offset() = default; 29 ~Offset() = default; Offset(double deltaX,double deltaY)30 Offset(double deltaX, double deltaY) : deltaX_(deltaX), deltaY_(deltaY) {} Offset(Point p1,Point p2)31 Offset(Point p1, Point p2) : deltaX_(p2.px_-p1.px_), deltaY_(p2.py_-p1.py_) {} 32 Resets()33 void Resets() 34 { 35 deltaX_ = 0.0; 36 deltaY_ = 0.0; 37 } 38 Zero()39 static Offset Zero() 40 { 41 return Offset(); 42 } 43 ErrorOffset()44 static Offset ErrorOffset() 45 { 46 return Offset((std::numeric_limits<double>::max)(), (std::numeric_limits<double>::max)()); 47 } 48 IsZero()49 bool IsZero() const 50 { 51 return operator==(Offset()); 52 } 53 IsErrorOffset()54 bool IsErrorOffset() const 55 { 56 return operator==(ErrorOffset()); 57 } 58 GetX()59 double GetX() const 60 { 61 return deltaX_; 62 } 63 GetY()64 double GetY() const 65 { 66 return deltaY_; 67 } 68 GetDistance()69 double GetDistance() const 70 { 71 return std::sqrt((deltaX_ * deltaX_) + (deltaY_ * deltaY_)); 72 } 73 74 Offset operator+(const Offset& offset) const 75 { 76 return Offset(deltaX_ + offset.deltaX_, deltaY_ + offset.deltaY_); 77 } 78 79 Offset operator+(const Size& size) const 80 { 81 return Offset(deltaX_ + size.Width(), deltaY_ + size.Height()); 82 } 83 84 Offset operator-(const Offset& offset) const 85 { 86 return Offset(deltaX_ - offset.deltaX_, deltaY_ - offset.deltaY_); 87 } 88 89 Offset operator-(const Size& size) const 90 { 91 return Offset(deltaX_ - size.Width(), deltaY_ - size.Height()); 92 } 93 94 Offset operator*(double value) const 95 { 96 return Offset(deltaX_ * value, deltaY_ * value); 97 } 98 99 Offset operator/(double value) const 100 { 101 if (NearZero(value)) { 102 return ErrorOffset(); 103 } 104 return Offset(deltaX_ / value, deltaY_ / value); 105 } 106 107 Offset& operator+=(const Offset& offset) 108 { 109 deltaX_ += offset.deltaX_; 110 deltaY_ += offset.deltaY_; 111 return *this; 112 } 113 114 Offset& operator-=(const Offset& offset) 115 { 116 deltaX_ -= offset.deltaX_; 117 deltaY_ -= offset.deltaY_; 118 return *this; 119 } 120 121 bool operator==(const Offset& offset) const 122 { 123 return NearEqual(deltaX_, offset.deltaX_) && NearEqual(deltaY_, offset.deltaY_); 124 } 125 126 bool operator!=(const Offset& offset) const 127 { 128 return !operator==(offset); 129 } 130 ToString()131 std::string ToString() const 132 { 133 std::stringstream ss; 134 ss << "Offset (" << std::fixed << std::setprecision(TWO) << deltaX_ << ", " << deltaY_ << ")"; 135 std::string output = ss.str(); 136 return output; 137 } 138 IsPositiveOffset()139 bool IsPositiveOffset() const 140 { 141 return deltaX_ >= 0 && deltaY_ >= 0; 142 } 143 144 private: 145 double deltaX_ = 0.0; 146 double deltaY_ = 0.0; 147 }; 148 } // namespace OHOS::uitest 149 150 #endif // GEOMETRY_OFFSET_H 151