summaryrefslogtreecommitdiffstats
path: root/include/basegfx/tuple/Tuple2D.hxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /include/basegfx/tuple/Tuple2D.hxx
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'include/basegfx/tuple/Tuple2D.hxx')
-rw-r--r--include/basegfx/tuple/Tuple2D.hxx157
1 files changed, 157 insertions, 0 deletions
diff --git a/include/basegfx/tuple/Tuple2D.hxx b/include/basegfx/tuple/Tuple2D.hxx
new file mode 100644
index 000000000..44d546df6
--- /dev/null
+++ b/include/basegfx/tuple/Tuple2D.hxx
@@ -0,0 +1,157 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ */
+
+#pragma once
+
+#include <basegfx/utils/common.hxx>
+#include <basegfx/numeric/ftools.hxx>
+
+namespace basegfx
+{
+template <typename TYPE> class Tuple2D
+{
+protected:
+ union {
+ // temporary alias mnX with mfX and mnY with mfY
+ struct
+ {
+ TYPE mnX;
+ TYPE mnY;
+ };
+ struct
+ {
+ TYPE mfX;
+ TYPE mfY;
+ };
+ };
+
+public:
+ /** Create a 2D Tuple
+
+ @param nX
+ This parameter is used to initialize the X-coordinate
+ of the 2D Tuple.
+
+ @param nY
+ This parameter is used to initialize the Y-coordinate
+ of the 2D Tuple.
+ */
+ Tuple2D(TYPE x, TYPE y)
+ : mnX(x)
+ , mnY(y)
+ {
+ }
+
+ double get(Axis2D eAxis) { return eAxis == Axis2D::X ? getX() : getY(); }
+
+ void set(Axis2D eAxis, TYPE fValue)
+ {
+ if (eAxis == Axis2D::X)
+ setX(fValue);
+ else
+ setY(fValue);
+ }
+
+ /// Get X-Coordinate of 2D Tuple
+ TYPE getX() const { return mnX; }
+
+ /// Get Y-Coordinate of 2D Tuple
+ TYPE getY() const { return mnY; }
+
+ /// Set X-Coordinate of 2D Tuple
+ void setX(TYPE fX) { mnX = fX; }
+
+ /// Set Y-Coordinate of 2D Tuple
+ void setY(TYPE fY) { mnY = fY; }
+
+ /// Adjust X-Coordinate of 2D Tuple
+ void adjustX(TYPE fX) { mnX += fX; }
+
+ /// Adjust Y-Coordinate of 2D Tuple
+ void adjustY(TYPE fY) { mnY += fY; }
+
+ // comparators with tolerance
+
+ template <typename T = TYPE, std::enable_if_t<std::is_integral_v<T>, int> = 0>
+ bool equal(const Tuple2D<TYPE>& rTup) const
+ {
+ return mfX == rTup.mfX && mfY == rTup.mfY;
+ }
+
+ template <typename T = TYPE, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
+ bool equal(const Tuple2D<TYPE>& rTup) const
+ {
+ return this == &rTup || (fTools::equal(mfX, rTup.mfX) && fTools::equal(mfY, rTup.mfY));
+ }
+
+ template <typename T = TYPE, std::enable_if_t<std::is_integral_v<T>, int> = 0>
+ bool equalZero() const
+ {
+ return mnX == 0 && mnY == 0;
+ }
+
+ template <typename T = TYPE, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
+ bool equalZero() const
+ {
+ return fTools::equalZero(mfX) && fTools::equalZero(mfY);
+ }
+
+ // operator overrides
+
+ Tuple2D& operator+=(const Tuple2D& rTup)
+ {
+ mfX += rTup.mfX;
+ mfY += rTup.mfY;
+ return *this;
+ }
+
+ Tuple2D& operator-=(const Tuple2D& rTup)
+ {
+ mfX -= rTup.mfX;
+ mfY -= rTup.mfY;
+ return *this;
+ }
+
+ Tuple2D& operator/=(const Tuple2D& rTup)
+ {
+ mfX /= rTup.mfX;
+ mfY /= rTup.mfY;
+ return *this;
+ }
+
+ Tuple2D& operator*=(const Tuple2D& rTup)
+ {
+ mfX *= rTup.mfX;
+ mfY *= rTup.mfY;
+ return *this;
+ }
+
+ Tuple2D& operator*=(TYPE t)
+ {
+ mfX *= t;
+ mfY *= t;
+ return *this;
+ }
+
+ Tuple2D& operator/=(TYPE t)
+ {
+ mfX /= t;
+ mfY /= t;
+ return *this;
+ }
+
+ bool operator==(const Tuple2D& rTup) const { return mfX == rTup.mfX && mfY == rTup.mfY; }
+
+ bool operator!=(const Tuple2D& rTup) const { return !(*this == rTup); }
+};
+
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */