Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / tuple / b2ituple.hxx
blobf8df10fe2851cd32af6bc01f71a4e2c7ff47361a
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_B2ITUPLE_HXX
21 #define INCLUDED_BASEGFX_TUPLE_B2ITUPLE_HXX
23 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <algorithm>
26 #include <basegfx/basegfxdllapi.h>
28 namespace basegfx
30 /** Base class for all Points/Vectors with two sal_Int32 values
32 This class provides all methods common to Point
33 avd Vector classes which are derived from here.
35 @derive Use this class to implement Points or Vectors
36 which are based on two sal_Int32 values
38 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B2ITuple
40 protected:
41 sal_Int32 mnX;
42 sal_Int32 mnY;
44 public:
45 /** Create a 2D Tuple
47 The tuple is initialized to (0, 0)
49 B2ITuple()
50 : mnX(0),
51 mnY(0)
54 /** Create a 2D Tuple
56 @param fX
57 This parameter is used to initialize the X-coordinate
58 of the 2D Tuple.
60 @param fY
61 This parameter is used to initialize the Y-coordinate
62 of the 2D Tuple.
64 B2ITuple(sal_Int32 fX, sal_Int32 fY)
65 : mnX( fX ),
66 mnY( fY )
69 /// Get X-Coordinate of 2D Tuple
70 sal_Int32 getX() const
72 return mnX;
75 /// Get Y-Coordinate of 2D Tuple
76 sal_Int32 getY() const
78 return mnY;
81 /// Set X-Coordinate of 2D Tuple
82 void setX(sal_Int32 fX)
84 mnX = fX;
87 /// Set Y-Coordinate of 2D Tuple
88 void setY(sal_Int32 fY)
90 mnY = fY;
93 /// Array-access to 2D Tuple
94 const sal_Int32& operator[] (int nPos) const
96 // Here, normally one if(...) should be used. In the assumption that
97 // both sal_Int32 members can be accessed as an array a shortcut is used here.
98 // if(0 == nPos) return mnX; return mnY;
99 return *((&mnX) + nPos);
102 /// Array-access to 2D Tuple
103 sal_Int32& operator[] (int nPos)
105 // Here, normally one if(...) should be used. In the assumption that
106 // both sal_Int32 members can be accessed as an array a shortcut is used here.
107 // if(0 == nPos) return mnX; return mnY;
108 return *((&mnX) + nPos);
111 // operators
114 B2ITuple& operator+=( const B2ITuple& rTup )
116 mnX += rTup.mnX;
117 mnY += rTup.mnY;
118 return *this;
121 B2ITuple& operator-=( const B2ITuple& rTup )
123 mnX -= rTup.mnX;
124 mnY -= rTup.mnY;
125 return *this;
128 B2ITuple& operator/=( const B2ITuple& rTup )
130 mnX /= rTup.mnX;
131 mnY /= rTup.mnY;
132 return *this;
135 B2ITuple& operator*=( const B2ITuple& rTup )
137 mnX *= rTup.mnX;
138 mnY *= rTup.mnY;
139 return *this;
142 B2ITuple& operator*=(sal_Int32 t)
144 mnX *= t;
145 mnY *= t;
146 return *this;
149 B2ITuple& operator/=(sal_Int32 t)
151 mnX /= t;
152 mnY /= t;
153 return *this;
156 B2ITuple operator-(void) const
158 return B2ITuple(-mnX, -mnY);
161 bool equalZero() const
163 return mnX == 0 && mnY == 0;
166 bool operator==( const B2ITuple& rTup ) const
168 return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY);
171 bool operator!=( const B2ITuple& rTup ) const
173 return !(*this == rTup);
177 // external operators
180 inline B2ITuple operator+(const B2ITuple& rTupA, const B2ITuple& rTupB)
182 B2ITuple aSum(rTupA);
183 aSum += rTupB;
184 return aSum;
187 inline B2ITuple operator-(const B2ITuple& rTupA, const B2ITuple& rTupB)
189 B2ITuple aSub(rTupA);
190 aSub -= rTupB;
191 return aSub;
194 inline B2ITuple operator*(sal_Int32 t, const B2ITuple& rTup)
196 B2ITuple aNew(rTup);
197 aNew *= t;
198 return aNew;
201 } // end of namespace basegfx
203 #endif // INCLUDED_BASEGFX_TUPLE_B2ITUPLE_HXX
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */