Bump version to 21.06.18.1
[LibreOffice.git] / include / basegfx / tuple / b2dtuple.hxx
blob0c7b7f46326162c8d46328c46b5cc357349911e3
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 #pragma once
22 #include <sal/types.h>
23 #include <basegfx/numeric/ftools.hxx>
24 #include <basegfx/basegfxdllapi.h>
25 #include <basegfx/utils/common.hxx>
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 and 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:
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 double get(Axis2D eAxis)
104 return eAxis == Axis2D::X ? getX() : getY();
107 void set(Axis2D eAxis, double fValue)
109 if (eAxis == Axis2D::X)
110 setX(fValue);
111 else
112 setY(fValue);
115 // comparators with tolerance
117 bool equalZero() const
119 return (this == &getEmptyTuple() ||
120 (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
123 bool equal(const B2DTuple& rTup) const
125 return (
126 this == &rTup ||
127 (fTools::equal(mfX, rTup.mfX) &&
128 fTools::equal(mfY, rTup.mfY)));
131 // operators
134 B2DTuple& operator+=( const B2DTuple& rTup )
136 mfX += rTup.mfX;
137 mfY += rTup.mfY;
138 return *this;
141 B2DTuple& operator-=( const B2DTuple& rTup )
143 mfX -= rTup.mfX;
144 mfY -= rTup.mfY;
145 return *this;
148 B2DTuple& operator/=( const B2DTuple& rTup )
150 mfX /= rTup.mfX;
151 mfY /= rTup.mfY;
152 return *this;
155 B2DTuple& operator*=( const B2DTuple& rTup )
157 mfX *= rTup.mfX;
158 mfY *= rTup.mfY;
159 return *this;
162 B2DTuple& operator*=(double t)
164 mfX *= t;
165 mfY *= t;
166 return *this;
169 B2DTuple& operator/=(double t)
171 const double fVal(1.0 / t);
172 mfX *= fVal;
173 mfY *= fVal;
174 return *this;
177 B2DTuple operator-(void) const
179 return B2DTuple(-mfX, -mfY);
182 bool operator==( const B2DTuple& rTup ) const
184 return mfX == rTup.mfX && mfY == rTup.mfY;
187 bool operator!=( const B2DTuple& rTup ) const
189 return mfX != rTup.mfX || mfY != rTup.mfY;
192 BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
195 // external operators
198 inline B2DTuple absolute(const B2DTuple& rTup)
200 B2DTuple aAbs(
201 fabs(rTup.getX()),
202 fabs(rTup.getY()));
203 return aAbs;
206 inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
208 if(rOld1 == rOld2)
210 return rOld1;
212 else if(0.0 >= t)
214 return rOld1;
216 else if(1.0 <= t)
218 return rOld2;
220 else
222 return B2DTuple(
223 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
224 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
228 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
230 return B2DTuple(
231 rtl_math_approxEqual(rOld1.getX(), rOld2.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
232 rtl_math_approxEqual(rOld1.getY(), rOld2.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
235 inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
237 B2DTuple aSum(rTupA);
238 aSum += rTupB;
239 return aSum;
242 inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
244 B2DTuple aSub(rTupA);
245 aSub -= rTupB;
246 return aSub;
249 inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
251 B2DTuple aDiv(rTupA);
252 aDiv /= rTupB;
253 return aDiv;
256 inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
258 B2DTuple aMul(rTupA);
259 aMul *= rTupB;
260 return aMul;
263 inline B2DTuple operator*(const B2DTuple& rTup, double t)
265 B2DTuple aNew(rTup);
266 aNew *= t;
267 return aNew;
270 inline B2DTuple operator*(double t, const B2DTuple& rTup)
272 B2DTuple aNew(rTup);
273 aNew *= t;
274 return aNew;
277 inline B2DTuple operator/(const B2DTuple& rTup, double t)
279 B2DTuple aNew(rTup);
280 aNew /= t;
281 return aNew;
284 /** Round double to nearest integer for 2D tuple
286 @return the nearest integer for this tuple
288 BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
289 } // end of namespace basegfx
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */