bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / basegfx / tuple / b2dtuple.hxx
blobb0297b5f0a6c16329a04996ba88e492ade5f6f57
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 <basegfx/basegfxdllapi.h>
27 namespace basegfx
29 class B2ITuple;
31 /** Base class for all Points/Vectors with two double values
33 This class provides all methods common to Point
34 avd Vector classes which are derived from here.
36 @derive Use this class to implement Points or Vectors
37 which are based on two double values
39 class SAL_WARN_UNUSED B2DTuple
41 protected:
42 double mfX;
43 double mfY;
45 public:
46 /** Create a 2D Tuple
48 The tuple is initialized to (0.0, 0.0)
50 B2DTuple()
51 : mfX(0.0),
52 mfY(0.0)
55 /** Create a 2D Tuple
57 @param fX
58 This parameter is used to initialize the X-coordinate
59 of the 2D Tuple.
61 @param fY
62 This parameter is used to initialize the Y-coordinate
63 of the 2D Tuple.
65 B2DTuple(double fX, double fY)
66 : mfX( fX ),
67 mfY( fY )
70 /** Create a copy of a 2D integer Tuple
72 @param rTup
73 The 2D Tuple which will be copied.
75 BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
77 /// Get X-Coordinate of 2D Tuple
78 double getX() const
80 return mfX;
83 /// Get Y-Coordinate of 2D Tuple
84 double getY() const
86 return mfY;
89 /// Set X-Coordinate of 2D Tuple
90 void setX(double fX)
92 mfX = fX;
95 /// Set Y-Coordinate of 2D Tuple
96 void setY(double fY)
98 mfY = fY;
101 /// Array-access to 2D Tuple
102 const double& operator[] (int nPos) const
104 // Here, normally one if(...) should be used. In the assumption that
105 // both double members can be accessed as an array a shortcut is used here.
106 // if(0 == nPos) return mfX; return mfY;
107 return *((&mfX) + nPos);
110 /// Array-access to 2D Tuple
111 double& operator[] (int nPos)
113 // Here, normally one if(...) should be used. In the assumption that
114 // both double members can be accessed as an array a shortcut is used here.
115 // if(0 == nPos) return mfX; return mfY;
116 return *((&mfX) + nPos);
119 // comparators with tolerance
122 bool equalZero() const
124 return (this == &getEmptyTuple() ||
125 (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
128 bool equal(const B2DTuple& rTup) const
130 return (
131 this == &rTup ||
132 (fTools::equal(mfX, rTup.mfX) &&
133 fTools::equal(mfY, rTup.mfY)));
136 // operators
139 B2DTuple& operator+=( const B2DTuple& rTup )
141 mfX += rTup.mfX;
142 mfY += rTup.mfY;
143 return *this;
146 B2DTuple& operator-=( const B2DTuple& rTup )
148 mfX -= rTup.mfX;
149 mfY -= rTup.mfY;
150 return *this;
153 B2DTuple& operator/=( const B2DTuple& rTup )
155 mfX /= rTup.mfX;
156 mfY /= rTup.mfY;
157 return *this;
160 B2DTuple& operator*=( const B2DTuple& rTup )
162 mfX *= rTup.mfX;
163 mfY *= rTup.mfY;
164 return *this;
167 B2DTuple& operator*=(double t)
169 mfX *= t;
170 mfY *= t;
171 return *this;
174 B2DTuple& operator/=(double t)
176 const double fVal(1.0 / t);
177 mfX *= fVal;
178 mfY *= fVal;
179 return *this;
182 B2DTuple operator-(void) const
184 return B2DTuple(-mfX, -mfY);
187 bool operator==( const B2DTuple& rTup ) const
189 return mfX == rTup.mfX && mfY == rTup.mfY;
192 bool operator!=( const B2DTuple& rTup ) const
194 return mfX != rTup.mfX || mfY != rTup.mfY;
197 BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
200 // external operators
203 inline B2DTuple absolute(const B2DTuple& rTup)
205 B2DTuple aAbs(
206 fabs(rTup.getX()),
207 fabs(rTup.getY()));
208 return aAbs;
211 inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
213 if(rOld1 == rOld2)
215 return rOld1;
217 else if(0.0 >= t)
219 return rOld1;
221 else if(1.0 <= t)
223 return rOld2;
225 else
227 return B2DTuple(
228 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
229 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
233 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
235 return B2DTuple(
236 rtl::math::approxEqual(rOld1.getX(), rOld2.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
237 rtl::math::approxEqual(rOld1.getY(), rOld2.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
240 inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
242 B2DTuple aSum(rTupA);
243 aSum += rTupB;
244 return aSum;
247 inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
249 B2DTuple aSub(rTupA);
250 aSub -= rTupB;
251 return aSub;
254 inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
256 B2DTuple aDiv(rTupA);
257 aDiv /= rTupB;
258 return aDiv;
261 inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
263 B2DTuple aMul(rTupA);
264 aMul *= rTupB;
265 return aMul;
268 inline B2DTuple operator*(const B2DTuple& rTup, double t)
270 B2DTuple aNew(rTup);
271 aNew *= t;
272 return aNew;
275 inline B2DTuple operator*(double t, const B2DTuple& rTup)
277 B2DTuple aNew(rTup);
278 aNew *= t;
279 return aNew;
282 inline B2DTuple operator/(const B2DTuple& rTup, double t)
284 B2DTuple aNew(rTup);
285 aNew /= t;
286 return aNew;
289 /** Round double to nearest integer for 2D tuple
291 @return the nearest integer for this tuple
293 BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
294 } // end of namespace basegfx
296 #endif // INCLUDED_BASEGFX_TUPLE_B2DTUPLE_HXX
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */