tdf#132853 - UI: fix different sizes in Image tabs Type & Crop
[LibreOffice.git] / include / basegfx / tuple / Tuple2D.hxx
blob09acb035620cc641a5150d25379b05048ca80b85
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 <o3tl/concepts.hxx>
14 #include <basegfx/utils/common.hxx>
15 #include <basegfx/numeric/ftools.hxx>
17 namespace basegfx
19 template <typename TYPE> class Tuple2D
21 protected:
22 TYPE mnX;
23 TYPE mnY;
25 public:
26 /** Create a 2D Tuple
28 @param nX
29 This parameter is used to initialize the X-coordinate
30 of the 2D Tuple.
32 @param nY
33 This parameter is used to initialize the Y-coordinate
34 of the 2D Tuple.
36 Tuple2D(TYPE x, TYPE y)
37 : mnX(x)
38 , mnY(y)
42 double get(Axis2D eAxis) { return eAxis == Axis2D::X ? getX() : getY(); }
44 void set(Axis2D eAxis, TYPE fValue)
46 if (eAxis == Axis2D::X)
47 setX(fValue);
48 else
49 setY(fValue);
52 /// Get X-Coordinate of 2D Tuple
53 TYPE getX() const { return mnX; }
55 /// Get Y-Coordinate of 2D Tuple
56 TYPE getY() const { return mnY; }
58 /// Set X-Coordinate of 2D Tuple
59 void setX(TYPE fX) { mnX = fX; }
61 /// Set Y-Coordinate of 2D Tuple
62 void setY(TYPE fY) { mnY = fY; }
64 /// Adjust X-Coordinate of 2D Tuple
65 void adjustX(TYPE fX) { mnX += fX; }
67 /// Adjust Y-Coordinate of 2D Tuple
68 void adjustY(TYPE fY) { mnY += fY; }
70 // comparators with tolerance
72 template <o3tl::integral T = TYPE> bool equal(const Tuple2D<TYPE>& rTup) const
74 return mnX == rTup.mnX && mnY == rTup.mnY;
77 template <o3tl::floating_point T = TYPE> bool equal(const Tuple2D<TYPE>& rTup) const
79 return this == &rTup || (fTools::equal(mnX, rTup.mnX) && fTools::equal(mnY, rTup.mnY));
82 template <o3tl::integral T = TYPE> bool equalZero() const { return mnX == 0 && mnY == 0; }
84 template <o3tl::floating_point T = TYPE> bool equalZero() const
86 return fTools::equalZero(mnX) && fTools::equalZero(mnY);
89 // operator overrides
91 Tuple2D<TYPE>& operator+=(const Tuple2D<TYPE>& rTup)
93 mnX += rTup.mnX;
94 mnY += rTup.mnY;
95 return *this;
98 Tuple2D<TYPE>& operator-=(const Tuple2D<TYPE>& rTup)
100 mnX -= rTup.mnX;
101 mnY -= rTup.mnY;
102 return *this;
105 Tuple2D<TYPE>& operator/=(const Tuple2D<TYPE>& rTup)
107 mnX /= rTup.mnX;
108 mnY /= rTup.mnY;
109 return *this;
112 Tuple2D<TYPE>& operator*=(const Tuple2D<TYPE>& rTup)
114 mnX *= rTup.mnX;
115 mnY *= rTup.mnY;
116 return *this;
119 Tuple2D<TYPE>& operator*=(TYPE t)
121 mnX *= t;
122 mnY *= t;
123 return *this;
126 Tuple2D<TYPE>& operator/=(TYPE t)
128 mnX /= t;
129 mnY /= t;
130 return *this;
133 Tuple2D<TYPE> operator-(void) const { return Tuple2D<TYPE>(-mnX, -mnY); }
135 bool operator==(const Tuple2D<TYPE>& rTup) const { return mnX == rTup.mnX && mnY == rTup.mnY; }
137 bool operator!=(const Tuple2D<TYPE>& rTup) const { return !(*this == rTup); }
140 template <typename TYPE>
141 inline Tuple2D<TYPE> operator-(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
143 Tuple2D<TYPE> aNew(rTupA);
144 aNew -= rTupB;
145 return aNew;
148 template <typename TYPE>
149 inline Tuple2D<TYPE> operator+(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
151 Tuple2D<TYPE> aNew(rTupA);
152 aNew += rTupB;
153 return aNew;
156 template <typename TYPE>
157 inline Tuple2D<TYPE> operator*(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
159 Tuple2D<TYPE> aNew(rTupA);
160 aNew *= rTupB;
161 return aNew;
164 template <typename TYPE>
165 inline Tuple2D<TYPE> operator/(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
167 Tuple2D<TYPE> aNew(rTupA);
168 aNew /= rTupB;
169 return aNew;
172 } // end of namespace basegfx
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */