Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / include / basegfx / tuple / b2dtuple.hxx
blob91df59c3ac6ec4d6b4395b3d95cc693b652471d3
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 // predeclarations
31 class B2ITuple;
33 /** Base class for all Points/Vectors with two double values
35 This class provides all methods common to Point
36 avd Vector classes which are derived from here.
38 @derive Use this class to implement Points or Vectors
39 which are based on two double values
41 class SAL_WARN_UNUSED B2DTuple
43 protected:
44 double mfX;
45 double mfY;
47 public:
48 /** Create a 2D Tuple
50 The tuple is initialized to (0.0, 0.0)
52 B2DTuple()
53 : mfX(0.0),
54 mfY(0.0)
57 /** Create a 2D Tuple
59 @param fX
60 This parameter is used to initialize the X-coordinate
61 of the 2D Tuple.
63 @param fY
64 This parameter is used to initialize the Y-coordinate
65 of the 2D Tuple.
67 B2DTuple(double fX, double fY)
68 : mfX( fX ),
69 mfY( fY )
72 /** Create a copy of a 2D Tuple
74 @param rTup
75 The 2D Tuple which will be copied.
77 B2DTuple(const B2DTuple& rTup)
78 : mfX( rTup.mfX ),
79 mfY( rTup.mfY )
82 /** Create a copy of a 2D integer Tuple
84 @param rTup
85 The 2D Tuple which will be copied.
87 BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
89 ~B2DTuple()
92 /// Get X-Coordinate of 2D Tuple
93 double getX() const
95 return mfX;
98 /// Get Y-Coordinate of 2D Tuple
99 double getY() const
101 return mfY;
104 /// Set X-Coordinate of 2D Tuple
105 void setX(double fX)
107 mfX = fX;
110 /// Set Y-Coordinate of 2D Tuple
111 void setY(double fY)
113 mfY = fY;
116 /// Array-access to 2D Tuple
117 const double& operator[] (int nPos) const
119 // Here, normally one if(...) should be used. In the assumption that
120 // both double members can be accessed as an array a shortcut is used here.
121 // if(0 == nPos) return mfX; return mfY;
122 return *((&mfX) + nPos);
125 /// Array-access to 2D Tuple
126 double& operator[] (int nPos)
128 // Here, normally one if(...) should be used. In the assumption that
129 // both double members can be accessed as an array a shortcut is used here.
130 // if(0 == nPos) return mfX; return mfY;
131 return *((&mfX) + nPos);
134 // comparators with tolerance
137 bool equalZero() const
139 return (this == &getEmptyTuple() ||
140 (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
143 bool equalZero(const double& rfSmallValue) const
145 return (this == &getEmptyTuple() ||
146 (fTools::equalZero(mfX, rfSmallValue) && fTools::equalZero(mfY, rfSmallValue)));
149 bool equal(const B2DTuple& rTup) const
151 return (
152 this == &rTup ||
153 (fTools::equal(mfX, rTup.mfX) &&
154 fTools::equal(mfY, rTup.mfY)));
157 bool equal(const B2DTuple& rTup, const double& rfSmallValue) const
159 return (
160 this == &rTup ||
161 (fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
162 fTools::equal(mfY, rTup.mfY, rfSmallValue)));
165 // operators
168 B2DTuple& operator+=( const B2DTuple& rTup )
170 mfX += rTup.mfX;
171 mfY += rTup.mfY;
172 return *this;
175 B2DTuple& operator-=( const B2DTuple& rTup )
177 mfX -= rTup.mfX;
178 mfY -= rTup.mfY;
179 return *this;
182 B2DTuple& operator/=( const B2DTuple& rTup )
184 mfX /= rTup.mfX;
185 mfY /= rTup.mfY;
186 return *this;
189 B2DTuple& operator*=( const B2DTuple& rTup )
191 mfX *= rTup.mfX;
192 mfY *= rTup.mfY;
193 return *this;
196 B2DTuple& operator*=(double t)
198 mfX *= t;
199 mfY *= t;
200 return *this;
203 B2DTuple& operator/=(double t)
205 const double fVal(1.0 / t);
206 mfX *= fVal;
207 mfY *= fVal;
208 return *this;
211 B2DTuple operator-(void) const
213 return B2DTuple(-mfX, -mfY);
216 bool operator==( const B2DTuple& rTup ) const
218 return mfX == rTup.mfX && mfY == rTup.mfY;
221 bool operator!=( const B2DTuple& rTup ) const
223 return mfX != rTup.mfX || mfY != rTup.mfY;
226 B2DTuple& operator=( const B2DTuple& rTup )
228 mfX = rTup.mfX;
229 mfY = rTup.mfY;
230 return *this;
233 BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
236 // external operators
239 inline B2DTuple minimum(const B2DTuple& rTupA, const B2DTuple& rTupB)
241 return B2DTuple(
242 std::min(rTupB.getX(), rTupA.getX()),
243 std::min(rTupB.getY(), rTupA.getY()));
246 inline B2DTuple maximum(const B2DTuple& rTupA, const B2DTuple& rTupB)
248 return B2DTuple(
249 std::max(rTupB.getX(), rTupA.getX()),
250 std::max(rTupB.getY(), rTupA.getY()));
253 inline B2DTuple absolute(const B2DTuple& rTup)
255 B2DTuple aAbs(
256 fabs(rTup.getX()),
257 fabs(rTup.getY()));
258 return aAbs;
261 inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
263 if(rOld1 == rOld2)
265 return rOld1;
267 else if(0.0 >= t)
269 return rOld1;
271 else if(1.0 <= t)
273 return rOld2;
275 else
277 return B2DTuple(
278 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
279 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
283 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
285 return B2DTuple(
286 rOld1.getX() == rOld2.getX() ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
287 rOld1.getY() == rOld2.getY() ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
290 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2, const B2DTuple& rOld3)
292 return B2DTuple(
293 (rOld1.getX() == rOld2.getX() && rOld2.getX() == rOld3.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
294 (rOld1.getY() == rOld2.getY() && rOld2.getY() == rOld3.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
297 inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
299 B2DTuple aSum(rTupA);
300 aSum += rTupB;
301 return aSum;
304 inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
306 B2DTuple aSub(rTupA);
307 aSub -= rTupB;
308 return aSub;
311 inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
313 B2DTuple aDiv(rTupA);
314 aDiv /= rTupB;
315 return aDiv;
318 inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
320 B2DTuple aMul(rTupA);
321 aMul *= rTupB;
322 return aMul;
325 inline B2DTuple operator*(const B2DTuple& rTup, double t)
327 B2DTuple aNew(rTup);
328 aNew *= t;
329 return aNew;
332 inline B2DTuple operator*(double t, const B2DTuple& rTup)
334 B2DTuple aNew(rTup);
335 aNew *= t;
336 return aNew;
339 inline B2DTuple operator/(const B2DTuple& rTup, double t)
341 B2DTuple aNew(rTup);
342 aNew /= t;
343 return aNew;
346 inline B2DTuple operator/(double t, const B2DTuple& rTup)
348 B2DTuple aNew(t, t);
349 B2DTuple aTmp(rTup);
350 aNew /= aTmp;
351 return aNew;
354 /** Round double to nearest integer for 2D tuple
356 @return the nearest integer for this tuple
358 BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
359 } // end of namespace basegfx
361 #endif // INCLUDED_BASEGFX_TUPLE_B2DTUPLE_HXX
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */