1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
48 The tuple is initialized to (0.0, 0.0)
58 This parameter is used to initialize the X-coordinate
62 This parameter is used to initialize the Y-coordinate
65 B2DTuple(double fX
, double fY
)
70 /** Create a copy of a 2D integer Tuple
73 The 2D Tuple which will be copied.
75 BASEGFX_DLLPUBLIC
explicit B2DTuple(const B2ITuple
& rTup
);
77 /// Get X-Coordinate of 2D Tuple
83 /// Get Y-Coordinate of 2D Tuple
89 /// Set X-Coordinate of 2D Tuple
95 /// Set Y-Coordinate of 2D Tuple
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
132 (fTools::equal(mfX
, rTup
.mfX
) &&
133 fTools::equal(mfY
, rTup
.mfY
)));
139 B2DTuple
& operator+=( const B2DTuple
& rTup
)
146 B2DTuple
& operator-=( const B2DTuple
& rTup
)
153 B2DTuple
& operator/=( const B2DTuple
& rTup
)
160 B2DTuple
& operator*=( const B2DTuple
& rTup
)
167 B2DTuple
& operator*=(double t
)
174 B2DTuple
& operator/=(double t
)
176 const double fVal(1.0 / t
);
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
)
211 inline B2DTuple
interpolate(const B2DTuple
& rOld1
, const B2DTuple
& rOld2
, double t
)
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
)
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
);
247 inline B2DTuple
operator-(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
249 B2DTuple
aSub(rTupA
);
254 inline B2DTuple
operator/(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
256 B2DTuple
aDiv(rTupA
);
261 inline B2DTuple
operator*(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
263 B2DTuple
aMul(rTupA
);
268 inline B2DTuple
operator*(const B2DTuple
& rTup
, double t
)
275 inline B2DTuple
operator*(double t
, const B2DTuple
& rTup
)
282 inline B2DTuple
operator/(const B2DTuple
& rTup
, double t
)
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: */