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>
26 #include <basegfx/basegfxdllapi.h>
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
50 The tuple is initialized to (0.0, 0.0)
60 This parameter is used to initialize the X-coordinate
64 This parameter is used to initialize the Y-coordinate
67 B2DTuple(double fX
, double fY
)
72 /** Create a copy of a 2D Tuple
75 The 2D Tuple which will be copied.
77 B2DTuple(const B2DTuple
& rTup
)
82 /** Create a copy of a 2D integer Tuple
85 The 2D Tuple which will be copied.
87 BASEGFX_DLLPUBLIC
explicit B2DTuple(const B2ITuple
& rTup
);
92 /// Get X-Coordinate of 2D Tuple
98 /// Get Y-Coordinate of 2D Tuple
104 /// Set X-Coordinate of 2D Tuple
110 /// Set Y-Coordinate of 2D Tuple
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
153 (fTools::equal(mfX
, rTup
.mfX
) &&
154 fTools::equal(mfY
, rTup
.mfY
)));
157 bool equal(const B2DTuple
& rTup
, const double& rfSmallValue
) const
161 (fTools::equal(mfX
, rTup
.mfX
, rfSmallValue
) &&
162 fTools::equal(mfY
, rTup
.mfY
, rfSmallValue
)));
168 B2DTuple
& operator+=( const B2DTuple
& rTup
)
175 B2DTuple
& operator-=( const B2DTuple
& rTup
)
182 B2DTuple
& operator/=( const B2DTuple
& rTup
)
189 B2DTuple
& operator*=( const B2DTuple
& rTup
)
196 B2DTuple
& operator*=(double t
)
203 B2DTuple
& operator/=(double t
)
205 const double fVal(1.0 / t
);
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
)
233 BASEGFX_DLLPUBLIC
static const B2DTuple
& getEmptyTuple();
236 // external operators
239 inline B2DTuple
minimum(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
242 std::min(rTupB
.getX(), rTupA
.getX()),
243 std::min(rTupB
.getY(), rTupA
.getY()));
246 inline B2DTuple
maximum(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
249 std::max(rTupB
.getX(), rTupA
.getX()),
250 std::max(rTupB
.getY(), rTupA
.getY()));
253 inline B2DTuple
absolute(const B2DTuple
& rTup
)
261 inline B2DTuple
interpolate(const B2DTuple
& rOld1
, const B2DTuple
& rOld2
, double t
)
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
)
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
)
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
);
304 inline B2DTuple
operator-(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
306 B2DTuple
aSub(rTupA
);
311 inline B2DTuple
operator/(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
313 B2DTuple
aDiv(rTupA
);
318 inline B2DTuple
operator*(const B2DTuple
& rTupA
, const B2DTuple
& rTupB
)
320 B2DTuple
aMul(rTupA
);
325 inline B2DTuple
operator*(const B2DTuple
& rTup
, double t
)
332 inline B2DTuple
operator*(double t
, const B2DTuple
& rTup
)
339 inline B2DTuple
operator/(const B2DTuple
& rTup
, double t
)
346 inline B2DTuple
operator/(double t
, const B2DTuple
& rTup
)
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: */