bump product version to 4.1.6.2
[LibreOffice.git] / include / basegfx / tuple / b2dtuple.hxx
blob380ba8adf4931da8258d16dffc24429a07962dc4
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 _BGFX_TUPLE_B2DTUPLE_HXX
21 #define _BGFX_TUPLE_B2DTUPLE_HXX
23 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <basegfx/basegfxdllapi.h>
27 namespace basegfx
29 // predeclarations
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 Tuple
73 @param rTup
74 The 2D Tuple which will be copied.
76 B2DTuple(const B2DTuple& rTup)
77 : mfX( rTup.mfX ),
78 mfY( rTup.mfY )
81 /** Create a copy of a 2D integer Tuple
83 @param rTup
84 The 2D Tuple which will be copied.
86 BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
88 ~B2DTuple()
91 /// Get X-Coordinate of 2D Tuple
92 double getX() const
94 return mfX;
97 /// Get Y-Coordinate of 2D Tuple
98 double getY() const
100 return mfY;
103 /// Set X-Coordinate of 2D Tuple
104 void setX(double fX)
106 mfX = fX;
109 /// Set Y-Coordinate of 2D Tuple
110 void setY(double fY)
112 mfY = fY;
115 /// Array-access to 2D Tuple
116 const double& operator[] (int nPos) const
118 // Here, normally one if(...) should be used. In the assumption that
119 // both double members can be accessed as an array a shortcut is used here.
120 // if(0 == nPos) return mfX; return mfY;
121 return *((&mfX) + nPos);
124 /// Array-access to 2D Tuple
125 double& operator[] (int nPos)
127 // Here, normally one if(...) should be used. In the assumption that
128 // both double members can be accessed as an array a shortcut is used here.
129 // if(0 == nPos) return mfX; return mfY;
130 return *((&mfX) + nPos);
133 // comparators with tolerance
134 //////////////////////////////////////////////////////////////////////
136 bool equalZero() const
138 return (this == &getEmptyTuple() ||
139 (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
142 bool equalZero(const double& rfSmallValue) const
144 return (this == &getEmptyTuple() ||
145 (fTools::equalZero(mfX, rfSmallValue) && fTools::equalZero(mfY, rfSmallValue)));
148 bool equal(const B2DTuple& rTup) const
150 return (
151 this == &rTup ||
152 (fTools::equal(mfX, rTup.mfX) &&
153 fTools::equal(mfY, rTup.mfY)));
156 bool equal(const B2DTuple& rTup, const double& rfSmallValue) const
158 return (
159 this == &rTup ||
160 (fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
161 fTools::equal(mfY, rTup.mfY, rfSmallValue)));
164 // operators
165 //////////////////////////////////////////////////////////////////////
167 B2DTuple& operator+=( const B2DTuple& rTup )
169 mfX += rTup.mfX;
170 mfY += rTup.mfY;
171 return *this;
174 B2DTuple& operator-=( const B2DTuple& rTup )
176 mfX -= rTup.mfX;
177 mfY -= rTup.mfY;
178 return *this;
181 B2DTuple& operator/=( const B2DTuple& rTup )
183 mfX /= rTup.mfX;
184 mfY /= rTup.mfY;
185 return *this;
188 B2DTuple& operator*=( const B2DTuple& rTup )
190 mfX *= rTup.mfX;
191 mfY *= rTup.mfY;
192 return *this;
195 B2DTuple& operator*=(double t)
197 mfX *= t;
198 mfY *= t;
199 return *this;
202 B2DTuple& operator/=(double t)
204 const double fVal(1.0 / t);
205 mfX *= fVal;
206 mfY *= fVal;
207 return *this;
210 B2DTuple operator-(void) const
212 return B2DTuple(-mfX, -mfY);
215 bool operator==( const B2DTuple& rTup ) const
217 return equal(rTup);
220 bool operator!=( const B2DTuple& rTup ) const
222 return !equal(rTup);
225 B2DTuple& operator=( const B2DTuple& rTup )
227 mfX = rTup.mfX;
228 mfY = rTup.mfY;
229 return *this;
232 BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
235 // external operators
236 //////////////////////////////////////////////////////////////////////////
238 inline B2DTuple minimum(const B2DTuple& rTupA, const B2DTuple& rTupB)
240 B2DTuple aMin(
241 (rTupB.getX() < rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
242 (rTupB.getY() < rTupA.getY()) ? rTupB.getY() : rTupA.getY());
243 return aMin;
246 inline B2DTuple maximum(const B2DTuple& rTupA, const B2DTuple& rTupB)
248 B2DTuple aMax(
249 (rTupB.getX() > rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
250 (rTupB.getY() > rTupA.getY()) ? rTupB.getY() : rTupA.getY());
251 return aMax;
254 inline B2DTuple absolute(const B2DTuple& rTup)
256 B2DTuple aAbs(
257 (0.0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
258 (0.0 > rTup.getY()) ? -rTup.getY() : rTup.getY());
259 return aAbs;
262 inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
264 B2DTuple aInt(
265 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
266 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
267 return aInt;
270 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
272 B2DTuple aAvg(
273 (rOld1.getX() + rOld2.getX()) * 0.5,
274 (rOld1.getY() + rOld2.getY()) * 0.5);
275 return aAvg;
278 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2, const B2DTuple& rOld3)
280 B2DTuple aAvg(
281 (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
282 (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
283 return aAvg;
286 inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
288 B2DTuple aSum(rTupA);
289 aSum += rTupB;
290 return aSum;
293 inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
295 B2DTuple aSub(rTupA);
296 aSub -= rTupB;
297 return aSub;
300 inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
302 B2DTuple aDiv(rTupA);
303 aDiv /= rTupB;
304 return aDiv;
307 inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
309 B2DTuple aMul(rTupA);
310 aMul *= rTupB;
311 return aMul;
314 inline B2DTuple operator*(const B2DTuple& rTup, double t)
316 B2DTuple aNew(rTup);
317 aNew *= t;
318 return aNew;
321 inline B2DTuple operator*(double t, const B2DTuple& rTup)
323 B2DTuple aNew(rTup);
324 aNew *= t;
325 return aNew;
328 inline B2DTuple operator/(const B2DTuple& rTup, double t)
330 B2DTuple aNew(rTup);
331 aNew /= t;
332 return aNew;
335 inline B2DTuple operator/(double t, const B2DTuple& rTup)
337 B2DTuple aNew(t, t);
338 B2DTuple aTmp(rTup);
339 aNew /= aTmp;
340 return aNew;
343 /** Round double to nearest integer for 2D tuple
345 @return the nearest integer for this tuple
347 BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
348 } // end of namespace basegfx
350 #endif /* _BGFX_TUPLE_B2DTUPLE_HXX */
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */