Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef _BGFX_TUPLE_B2I64TUPLE_HXX
21 : #define _BGFX_TUPLE_B2I64TUPLE_HXX
22 :
23 : #include <sal/types.h>
24 : #include <basegfx/tuple/b2dtuple.hxx>
25 : #include <basegfx/basegfxdllapi.h>
26 :
27 :
28 : namespace basegfx
29 : {
30 : /** Base class for all Points/Vectors with two sal_Int64 values
31 :
32 : This class provides all methods common to Point
33 : avd Vector classes which are derived from here.
34 :
35 : @derive Use this class to implement Points or Vectors
36 : which are based on two sal_Int64 values
37 : */
38 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED B2I64Tuple
39 : {
40 : protected:
41 : sal_Int64 mnX;
42 : sal_Int64 mnY;
43 :
44 : public:
45 : /** Create a 2D Tuple
46 :
47 : The tuple is initialized to (0, 0)
48 : */
49 : B2I64Tuple()
50 : : mnX(0),
51 : mnY(0)
52 : {}
53 :
54 : /** Create a 2D Tuple
55 :
56 : @param fX
57 : This parameter is used to initialize the X-coordinate
58 : of the 2D Tuple.
59 :
60 : @param fY
61 : This parameter is used to initialize the Y-coordinate
62 : of the 2D Tuple.
63 : */
64 0 : B2I64Tuple(sal_Int64 fX, sal_Int64 fY)
65 : : mnX( fX ),
66 0 : mnY( fY )
67 0 : {}
68 :
69 : /** Create a copy of a 2D Tuple
70 :
71 : @param rTup
72 : The 2D Tuple which will be copied.
73 : */
74 : B2I64Tuple(const B2I64Tuple& rTup)
75 : : mnX( rTup.mnX ),
76 : mnY( rTup.mnY )
77 : {}
78 :
79 0 : ~B2I64Tuple()
80 0 : {}
81 :
82 : /// Get X-Coordinate of 2D Tuple
83 0 : sal_Int64 getX() const
84 : {
85 0 : return mnX;
86 : }
87 :
88 : /// Get Y-Coordinate of 2D Tuple
89 0 : sal_Int64 getY() const
90 : {
91 0 : return mnY;
92 : }
93 :
94 : /// Set X-Coordinate of 2D Tuple
95 : void setX(sal_Int64 fX)
96 : {
97 : mnX = fX;
98 : }
99 :
100 : /// Set Y-Coordinate of 2D Tuple
101 : void setY(sal_Int64 fY)
102 : {
103 : mnY = fY;
104 : }
105 :
106 : /// Array-access to 2D Tuple
107 : const sal_Int64& operator[] (int nPos) const
108 : {
109 : // Here, normally one if(...) should be used. In the assumption that
110 : // both sal_Int64 members can be accessed as an array a shortcut is used here.
111 : // if(0 == nPos) return mnX; return mnY;
112 : return *((&mnX) + nPos);
113 : }
114 :
115 : /// Array-access to 2D Tuple
116 : sal_Int64& operator[] (int nPos)
117 : {
118 : // Here, normally one if(...) should be used. In the assumption that
119 : // both sal_Int64 members can be accessed as an array a shortcut is used here.
120 : // if(0 == nPos) return mnX; return mnY;
121 : return *((&mnX) + nPos);
122 : }
123 :
124 : // operators
125 : //////////////////////////////////////////////////////////////////////
126 :
127 : B2I64Tuple& operator+=( const B2I64Tuple& rTup )
128 : {
129 : mnX += rTup.mnX;
130 : mnY += rTup.mnY;
131 : return *this;
132 : }
133 :
134 : B2I64Tuple& operator-=( const B2I64Tuple& rTup )
135 : {
136 : mnX -= rTup.mnX;
137 : mnY -= rTup.mnY;
138 : return *this;
139 : }
140 :
141 : B2I64Tuple& operator/=( const B2I64Tuple& rTup )
142 : {
143 : mnX /= rTup.mnX;
144 : mnY /= rTup.mnY;
145 : return *this;
146 : }
147 :
148 : B2I64Tuple& operator*=( const B2I64Tuple& rTup )
149 : {
150 : mnX *= rTup.mnX;
151 : mnY *= rTup.mnY;
152 : return *this;
153 : }
154 :
155 : B2I64Tuple& operator*=(sal_Int64 t)
156 : {
157 : mnX *= t;
158 : mnY *= t;
159 : return *this;
160 : }
161 :
162 : B2I64Tuple& operator/=(sal_Int64 t)
163 : {
164 : mnX /= t;
165 : mnY /= t;
166 : return *this;
167 : }
168 :
169 : B2I64Tuple operator-(void) const
170 : {
171 : return B2I64Tuple(-mnX, -mnY);
172 : }
173 :
174 : bool equalZero() const { return mnX == 0 && mnY == 0; }
175 :
176 : bool operator==( const B2I64Tuple& rTup ) const
177 : {
178 : return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY);
179 : }
180 :
181 : bool operator!=( const B2I64Tuple& rTup ) const
182 : {
183 : return !(*this == rTup);
184 : }
185 :
186 : B2I64Tuple& operator=( const B2I64Tuple& rTup )
187 : {
188 : mnX = rTup.mnX;
189 : mnY = rTup.mnY;
190 : return *this;
191 : }
192 : };
193 :
194 : // external operators
195 : //////////////////////////////////////////////////////////////////////////
196 :
197 : inline B2I64Tuple minimum(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
198 : {
199 : B2I64Tuple aMin(
200 : (rTupB.getX() < rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
201 : (rTupB.getY() < rTupA.getY()) ? rTupB.getY() : rTupA.getY());
202 : return aMin;
203 : }
204 :
205 : inline B2I64Tuple maximum(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
206 : {
207 : B2I64Tuple aMax(
208 : (rTupB.getX() > rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
209 : (rTupB.getY() > rTupA.getY()) ? rTupB.getY() : rTupA.getY());
210 : return aMax;
211 : }
212 :
213 : inline B2I64Tuple absolute(const B2I64Tuple& rTup)
214 : {
215 : B2I64Tuple aAbs(
216 : (0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
217 : (0 > rTup.getY()) ? -rTup.getY() : rTup.getY());
218 : return aAbs;
219 : }
220 :
221 : inline B2DTuple interpolate(const B2I64Tuple& rOld1, const B2I64Tuple& rOld2, double t)
222 : {
223 : B2DTuple aInt(
224 : ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
225 : ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
226 : return aInt;
227 : }
228 :
229 : inline B2DTuple average(const B2I64Tuple& rOld1, const B2I64Tuple& rOld2)
230 : {
231 : B2DTuple aAvg(
232 : (rOld1.getX() + rOld2.getX()) * 0.5,
233 : (rOld1.getY() + rOld2.getY()) * 0.5);
234 : return aAvg;
235 : }
236 :
237 : inline B2DTuple average(const B2I64Tuple& rOld1, const B2I64Tuple& rOld2, const B2I64Tuple& rOld3)
238 : {
239 : B2DTuple aAvg(
240 : (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
241 : (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
242 : return aAvg;
243 : }
244 :
245 : inline B2I64Tuple operator+(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
246 : {
247 : B2I64Tuple aSum(rTupA);
248 : aSum += rTupB;
249 : return aSum;
250 : }
251 :
252 : inline B2I64Tuple operator-(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
253 : {
254 : B2I64Tuple aSub(rTupA);
255 : aSub -= rTupB;
256 : return aSub;
257 : }
258 :
259 : inline B2I64Tuple operator/(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
260 : {
261 : B2I64Tuple aDiv(rTupA);
262 : aDiv /= rTupB;
263 : return aDiv;
264 : }
265 :
266 : inline B2I64Tuple operator*(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
267 : {
268 : B2I64Tuple aMul(rTupA);
269 : aMul *= rTupB;
270 : return aMul;
271 : }
272 :
273 : inline B2I64Tuple operator*(const B2I64Tuple& rTup, sal_Int64 t)
274 : {
275 : B2I64Tuple aNew(rTup);
276 : aNew *= t;
277 : return aNew;
278 : }
279 :
280 : inline B2I64Tuple operator*(sal_Int64 t, const B2I64Tuple& rTup)
281 : {
282 : B2I64Tuple aNew(rTup);
283 : aNew *= t;
284 : return aNew;
285 : }
286 :
287 : inline B2I64Tuple operator/(const B2I64Tuple& rTup, sal_Int64 t)
288 : {
289 : B2I64Tuple aNew(rTup);
290 : aNew /= t;
291 : return aNew;
292 : }
293 :
294 : inline B2I64Tuple operator/(sal_Int64 t, const B2I64Tuple& rTup)
295 : {
296 : B2I64Tuple aNew(t, t);
297 : B2I64Tuple aTmp(rTup);
298 : aNew /= aTmp;
299 : return aNew;
300 : }
301 : } // end of namespace basegfx
302 :
303 : #endif /* _BGFX_TUPLE_B2I64TUPLE_HXX */
304 :
305 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|