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 FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_PointT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_PointT_H
18 
19 #include <cmath>
20 
21 #include "base/geometry/ng/offset_t.h"
22 
23 namespace OHOS::Ace::NG {
24 template<typename T>
25 class PointT {
26 public:
27     PointT() = default;
28     ~PointT() = default;
PointT(T x,T y)29     PointT(T x, T y) : x_(x), y_(y) {}
30 
GetX()31     T GetX() const
32     {
33         return x_;
34     }
35 
GetY()36     T GetY() const
37     {
38         return y_;
39     }
40 
SetX(T x)41     void SetX(T x)
42     {
43         x_ = x;
44     }
45 
SetY(T y)46     void SetY(T y)
47     {
48         y_ = y;
49     }
50 
Rotate(const PointT & center,T angle)51     void Rotate(const PointT& center, T angle)
52     {
53         T x = (x_ - center.GetX()) * std::cos(angle) - (y_ - center.GetY()) * std::sin(angle) + center.GetX();
54         T y = (x_ - center.GetX()) * std::sin(angle) + (y_ - center.GetY()) * std::cos(angle) + center.GetY();
55         x_ = x;
56         y_ = y;
57     }
58 
59     PointT operator-(const OffsetF& offset) const
60     {
61         return PointT(x_ - offset.GetX(), y_ - offset.GetY());
62     }
63 
64     PointT operator+(const OffsetF& offset) const
65     {
66         return PointT(x_ + offset.GetX(), y_ + offset.GetY());
67     }
68 
69     OffsetF operator-(const PointT& PointT) const
70     {
71         return OffsetF(x_ - PointT.x_, y_ - PointT.y_);
72     }
73 
74     bool operator==(const PointT& PointT) const
75     {
76         return NearEqual(x_, PointT.x_) && NearEqual(y_, PointT.y_);
77     }
78 
79     bool operator!=(const PointT& PointT) const
80     {
81         return !operator==(PointT);
82     }
83 
ToString()84     std::string ToString() const
85     {
86         static const int32_t precision = 2;
87         std::stringstream ss;
88         ss << "Point (" << std::fixed << std::setprecision(precision) << x_ << ", " << y_ << ")";
89         std::string output = ss.str();
90         return output;
91     }
92 
93 private:
94     T x_ { 0 };
95     T y_ { 0 };
96 };
97 
98 using PointF = PointT<float>;
99 } // namespace OHOS::Ace::NG
100 
101 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_PointT_H
102