Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / basegfx / tuple / Tuple2D.hxx
blobb4d1dbe2f50892149459ad7394a1c27c80a0d678
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #pragma once
13 #include <basegfx/utils/common.hxx>
14 #include <basegfx/numeric/ftools.hxx>
16 namespace basegfx
18 template <typename TYPE> class Tuple2D
20 protected:
21 union {
22 // temporary alias mnX with mfX and mnY with mfY
23 struct
25 TYPE mnX;
26 TYPE mnY;
28 struct
30 TYPE mfX;
31 TYPE mfY;
35 public:
36 /** Create a 2D Tuple
38 @param nX
39 This parameter is used to initialize the X-coordinate
40 of the 2D Tuple.
42 @param nY
43 This parameter is used to initialize the Y-coordinate
44 of the 2D Tuple.
46 Tuple2D(TYPE x, TYPE y)
47 : mnX(x)
48 , mnY(y)
52 double get(Axis2D eAxis) { return eAxis == Axis2D::X ? getX() : getY(); }
54 void set(Axis2D eAxis, TYPE fValue)
56 if (eAxis == Axis2D::X)
57 setX(fValue);
58 else
59 setY(fValue);
62 /// Get X-Coordinate of 2D Tuple
63 TYPE getX() const { return mnX; }
65 /// Get Y-Coordinate of 2D Tuple
66 TYPE getY() const { return mnY; }
68 /// Set X-Coordinate of 2D Tuple
69 void setX(TYPE fX) { mnX = fX; }
71 /// Set Y-Coordinate of 2D Tuple
72 void setY(TYPE fY) { mnY = fY; }
74 /// Adjust X-Coordinate of 2D Tuple
75 void adjustX(TYPE fX) { mnX += fX; }
77 /// Adjust Y-Coordinate of 2D Tuple
78 void adjustY(TYPE fY) { mnY += fY; }
80 // comparators with tolerance
82 template <typename T = TYPE, std::enable_if_t<std::is_integral_v<T>, int> = 0>
83 bool equal(const Tuple2D<TYPE>& rTup) const
85 return mfX == rTup.mfX && mfY == rTup.mfY;
88 template <typename T = TYPE, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
89 bool equal(const Tuple2D<TYPE>& rTup) const
91 return this == &rTup || (fTools::equal(mfX, rTup.mfX) && fTools::equal(mfY, rTup.mfY));
94 template <typename T = TYPE, std::enable_if_t<std::is_integral_v<T>, int> = 0>
95 bool equalZero() const
97 return mnX == 0 && mnY == 0;
100 template <typename T = TYPE, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
101 bool equalZero() const
103 return fTools::equalZero(mfX) && fTools::equalZero(mfY);
106 // operator overrides
108 Tuple2D<TYPE>& operator+=(const Tuple2D<TYPE>& rTup)
110 mfX += rTup.mfX;
111 mfY += rTup.mfY;
112 return *this;
115 Tuple2D<TYPE>& operator-=(const Tuple2D<TYPE>& rTup)
117 mfX -= rTup.mfX;
118 mfY -= rTup.mfY;
119 return *this;
122 Tuple2D<TYPE>& operator/=(const Tuple2D<TYPE>& rTup)
124 mfX /= rTup.mfX;
125 mfY /= rTup.mfY;
126 return *this;
129 Tuple2D<TYPE>& operator*=(const Tuple2D<TYPE>& rTup)
131 mfX *= rTup.mfX;
132 mfY *= rTup.mfY;
133 return *this;
136 Tuple2D<TYPE>& operator*=(TYPE t)
138 mfX *= t;
139 mfY *= t;
140 return *this;
143 Tuple2D<TYPE>& operator/=(TYPE t)
145 mfX /= t;
146 mfY /= t;
147 return *this;
150 Tuple2D<TYPE> operator-(void) const { return Tuple2D<TYPE>(-mfX, -mfY); }
152 bool operator==(const Tuple2D<TYPE>& rTup) const { return mfX == rTup.mfX && mfY == rTup.mfY; }
154 bool operator!=(const Tuple2D<TYPE>& rTup) const { return !(*this == rTup); }
157 template <typename TYPE>
158 inline Tuple2D<TYPE> operator-(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
160 Tuple2D<TYPE> aNew(rTupA);
161 aNew -= rTupB;
162 return aNew;
165 template <typename TYPE>
166 inline Tuple2D<TYPE> operator+(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
168 Tuple2D<TYPE> aNew(rTupA);
169 aNew += rTupB;
170 return aNew;
173 template <typename TYPE>
174 inline Tuple2D<TYPE> operator*(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
176 Tuple2D<TYPE> aNew(rTupA);
177 aNew *= rTupB;
178 return aNew;
181 template <typename TYPE>
182 inline Tuple2D<TYPE> operator/(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
184 Tuple2D<TYPE> aNew(rTupA);
185 aNew /= rTupB;
186 return aNew;
189 } // end of namespace basegfx
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */