LibreOffice Module basegfx (master) 1
Tuple2D.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 */
10
11#pragma once
12
15
16namespace basegfx
17{
18template <typename TYPE> class Tuple2D
19{
20protected:
21 union {
22 // temporary alias mnX with mfX and mnY with mfY
23 struct
24 {
27 };
28 struct
29 {
32 };
33 };
34
35public:
46 Tuple2D(TYPE x, TYPE y)
47 : mnX(x)
48 , mnY(y)
49 {
50 }
51
52 double get(Axis2D eAxis) { return eAxis == Axis2D::X ? getX() : getY(); }
53
54 void set(Axis2D eAxis, TYPE fValue)
55 {
56 if (eAxis == Axis2D::X)
57 setX(fValue);
58 else
59 setY(fValue);
60 }
61
63 TYPE getX() const { return mnX; }
64
66 TYPE getY() const { return mnY; }
67
69 void setX(TYPE fX) { mnX = fX; }
70
72 void setY(TYPE fY) { mnY = fY; }
73
75 void adjustX(TYPE fX) { mnX += fX; }
76
78 void adjustY(TYPE fY) { mnY += fY; }
79
80 // comparators with tolerance
81
82 template <typename T = TYPE, std::enable_if_t<std::is_integral_v<T>, int> = 0>
83 bool equal(const Tuple2D<TYPE>& rTup) const
84 {
85 return mfX == rTup.mfX && mfY == rTup.mfY;
86 }
87
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
90 {
91 return this == &rTup || (fTools::equal(mfX, rTup.mfX) && fTools::equal(mfY, rTup.mfY));
92 }
93
94 template <typename T = TYPE, std::enable_if_t<std::is_integral_v<T>, int> = 0>
95 bool equalZero() const
96 {
97 return mnX == 0 && mnY == 0;
98 }
99
100 template <typename T = TYPE, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
101 bool equalZero() const
102 {
104 }
105
106 // operator overrides
107
109 {
110 mfX += rTup.mfX;
111 mfY += rTup.mfY;
112 return *this;
113 }
114
116 {
117 mfX -= rTup.mfX;
118 mfY -= rTup.mfY;
119 return *this;
120 }
121
123 {
124 mfX /= rTup.mfX;
125 mfY /= rTup.mfY;
126 return *this;
127 }
128
130 {
131 mfX *= rTup.mfX;
132 mfY *= rTup.mfY;
133 return *this;
134 }
135
137 {
138 mfX *= t;
139 mfY *= t;
140 return *this;
141 }
142
144 {
145 mfX /= t;
146 mfY /= t;
147 return *this;
148 }
149
150 Tuple2D<TYPE> operator-(void) const { return Tuple2D<TYPE>(-mfX, -mfY); }
151
152 bool operator==(const Tuple2D<TYPE>& rTup) const { return mfX == rTup.mfX && mfY == rTup.mfY; }
153
154 bool operator!=(const Tuple2D<TYPE>& rTup) const { return !(*this == rTup); }
155};
156
157template <typename TYPE>
158inline Tuple2D<TYPE> operator-(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
159{
160 Tuple2D<TYPE> aNew(rTupA);
161 aNew -= rTupB;
162 return aNew;
163}
164
165template <typename TYPE>
166inline Tuple2D<TYPE> operator+(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
167{
168 Tuple2D<TYPE> aNew(rTupA);
169 aNew += rTupB;
170 return aNew;
171}
172
173template <typename TYPE>
174inline Tuple2D<TYPE> operator*(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
175{
176 Tuple2D<TYPE> aNew(rTupA);
177 aNew *= rTupB;
178 return aNew;
179}
180
181template <typename TYPE>
182inline Tuple2D<TYPE> operator/(const Tuple2D<TYPE>& rTupA, const Tuple2D<TYPE>& rTupB)
183{
184 Tuple2D<TYPE> aNew(rTupA);
185 aNew /= rTupB;
186 return aNew;
187}
188
189} // end of namespace basegfx
190
191/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
bool operator!=(const Tuple2D< TYPE > &rTup) const
Definition: Tuple2D.hxx:154
void adjustY(TYPE fY)
Adjust Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:78
Tuple2D< TYPE > & operator+=(const Tuple2D< TYPE > &rTup)
Definition: Tuple2D.hxx:108
Tuple2D< TYPE > & operator*=(TYPE t)
Definition: Tuple2D.hxx:136
bool equalZero() const
Definition: Tuple2D.hxx:95
bool equal(const Tuple2D< TYPE > &rTup) const
Definition: Tuple2D.hxx:83
TYPE getX() const
Get X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:63
Tuple2D< TYPE > & operator-=(const Tuple2D< TYPE > &rTup)
Definition: Tuple2D.hxx:115
void setY(TYPE fY)
Set Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:72
void set(Axis2D eAxis, TYPE fValue)
Definition: Tuple2D.hxx:54
void adjustX(TYPE fX)
Adjust X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:75
bool operator==(const Tuple2D< TYPE > &rTup) const
Definition: Tuple2D.hxx:152
Tuple2D< TYPE > & operator/=(const Tuple2D< TYPE > &rTup)
Definition: Tuple2D.hxx:122
TYPE getY() const
Get Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:66
double get(Axis2D eAxis)
Definition: Tuple2D.hxx:52
Tuple2D< TYPE > & operator/=(TYPE t)
Definition: Tuple2D.hxx:143
void setX(TYPE fX)
Set X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:69
Tuple2D< TYPE > & operator*=(const Tuple2D< TYPE > &rTup)
Definition: Tuple2D.hxx:129
Tuple2D< TYPE > operator-(void) const
Definition: Tuple2D.hxx:150
Tuple2D(TYPE x, TYPE y)
Create a 2D Tuple.
Definition: Tuple2D.hxx:46
float y
float x
bool equalZero(const T &rfVal)
Compare against small value.
Definition: ftools.hxx:156
bool equal(T const &rfValA, T const &rfValB)
Definition: ftools.hxx:169
B2ITuple operator+(const B2ITuple &rTupA, const B2ITuple &rTupB)
Definition: b2ituple.hxx:72
B2ITuple operator-(const B2ITuple &rTupA, const B2ITuple &rTupB)
Definition: b2ituple.hxx:79
B2DPoint operator*(const ::basegfx::B2DHomMatrix &rMat, const B2DPoint &rPoint)
Definition: b2dpoint.cxx:43
B2DTuple operator/(const B2DTuple &rTup, double t)
Definition: b2dtuple.hxx:139
TYPE