Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / tuple / b2dtuple.hxx
blobcba49bf0325cc4167599d0fba086af6b6c717b7c
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 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_BASEGFX_TUPLE_B2DTUPLE_HXX
21 #define INCLUDED_BASEGFX_TUPLE_B2DTUPLE_HXX
23 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <algorithm>
26 #include <basegfx/basegfxdllapi.h>
28 namespace basegfx
30 class B2ITuple;
32 /** Base class for all Points/Vectors with two double values
34 This class provides all methods common to Point
35 avd Vector classes which are derived from here.
37 @derive Use this class to implement Points or Vectors
38 which are based on two double values
40 class SAL_WARN_UNUSED B2DTuple
42 protected:
43 double mfX;
44 double mfY;
46 public:
47 /** Create a 2D Tuple
49 The tuple is initialized to (0.0, 0.0)
51 B2DTuple()
52 : mfX(0.0),
53 mfY(0.0)
56 /** Create a 2D Tuple
58 @param fX
59 This parameter is used to initialize the X-coordinate
60 of the 2D Tuple.
62 @param fY
63 This parameter is used to initialize the Y-coordinate
64 of the 2D Tuple.
66 B2DTuple(double fX, double fY)
67 : mfX( fX ),
68 mfY( fY )
71 /** Create a copy of a 2D integer Tuple
73 @param rTup
74 The 2D Tuple which will be copied.
76 BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
78 /// Get X-Coordinate of 2D Tuple
79 double getX() const
81 return mfX;
84 /// Get Y-Coordinate of 2D Tuple
85 double getY() const
87 return mfY;
90 /// Set X-Coordinate of 2D Tuple
91 void setX(double fX)
93 mfX = fX;
96 /// Set Y-Coordinate of 2D Tuple
97 void setY(double fY)
99 mfY = fY;
102 /// Array-access to 2D Tuple
103 const double& operator[] (int nPos) const
105 // Here, normally one if(...) should be used. In the assumption that
106 // both double members can be accessed as an array a shortcut is used here.
107 // if(0 == nPos) return mfX; return mfY;
108 return *((&mfX) + nPos);
111 /// Array-access to 2D Tuple
112 double& operator[] (int nPos)
114 // Here, normally one if(...) should be used. In the assumption that
115 // both double members can be accessed as an array a shortcut is used here.
116 // if(0 == nPos) return mfX; return mfY;
117 return *((&mfX) + nPos);
120 // comparators with tolerance
123 bool equalZero() const
125 return (this == &getEmptyTuple() ||
126 (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
129 bool equal(const B2DTuple& rTup) const
131 return (
132 this == &rTup ||
133 (fTools::equal(mfX, rTup.mfX) &&
134 fTools::equal(mfY, rTup.mfY)));
137 // operators
140 B2DTuple& operator+=( const B2DTuple& rTup )
142 mfX += rTup.mfX;
143 mfY += rTup.mfY;
144 return *this;
147 B2DTuple& operator-=( const B2DTuple& rTup )
149 mfX -= rTup.mfX;
150 mfY -= rTup.mfY;
151 return *this;
154 B2DTuple& operator/=( const B2DTuple& rTup )
156 mfX /= rTup.mfX;
157 mfY /= rTup.mfY;
158 return *this;
161 B2DTuple& operator*=( const B2DTuple& rTup )
163 mfX *= rTup.mfX;
164 mfY *= rTup.mfY;
165 return *this;
168 B2DTuple& operator*=(double t)
170 mfX *= t;
171 mfY *= t;
172 return *this;
175 B2DTuple& operator/=(double t)
177 const double fVal(1.0 / t);
178 mfX *= fVal;
179 mfY *= fVal;
180 return *this;
183 B2DTuple operator-(void) const
185 return B2DTuple(-mfX, -mfY);
188 bool operator==( const B2DTuple& rTup ) const
190 return mfX == rTup.mfX && mfY == rTup.mfY;
193 bool operator!=( const B2DTuple& rTup ) const
195 return mfX != rTup.mfX || mfY != rTup.mfY;
198 BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
201 // external operators
204 inline B2DTuple absolute(const B2DTuple& rTup)
206 B2DTuple aAbs(
207 fabs(rTup.getX()),
208 fabs(rTup.getY()));
209 return aAbs;
212 inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
214 if(rOld1 == rOld2)
216 return rOld1;
218 else if(0.0 >= t)
220 return rOld1;
222 else if(1.0 <= t)
224 return rOld2;
226 else
228 return B2DTuple(
229 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
230 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
234 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
236 return B2DTuple(
237 rtl::math::approxEqual(rOld1.getX(), rOld2.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
238 rtl::math::approxEqual(rOld1.getY(), rOld2.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
241 inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
243 B2DTuple aSum(rTupA);
244 aSum += rTupB;
245 return aSum;
248 inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
250 B2DTuple aSub(rTupA);
251 aSub -= rTupB;
252 return aSub;
255 inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
257 B2DTuple aDiv(rTupA);
258 aDiv /= rTupB;
259 return aDiv;
262 inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
264 B2DTuple aMul(rTupA);
265 aMul *= rTupB;
266 return aMul;
269 inline B2DTuple operator*(const B2DTuple& rTup, double t)
271 B2DTuple aNew(rTup);
272 aNew *= t;
273 return aNew;
276 inline B2DTuple operator*(double t, const B2DTuple& rTup)
278 B2DTuple aNew(rTup);
279 aNew *= t;
280 return aNew;
283 inline B2DTuple operator/(const B2DTuple& rTup, double t)
285 B2DTuple aNew(rTup);
286 aNew /= t;
287 return aNew;
290 /** Round double to nearest integer for 2D tuple
292 @return the nearest integer for this tuple
294 BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
295 } // end of namespace basegfx
297 #endif // INCLUDED_BASEGFX_TUPLE_B2DTUPLE_HXX
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */