1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_TUPLE_B2ITUPLE_HXX
25 #define _BGFX_TUPLE_B2ITUPLE_HXX
26 
27 #include <sal/types.h>
28 
29 
30 namespace basegfx
31 {
32 	/** Base class for all Points/Vectors with two sal_Int32 values
33 
34 		This class provides all methods common to Point
35 		avd Vector classes which are derived from here.
36 
37 		@derive Use this class to implement Points or Vectors
38 		which are based on two sal_Int32 values
39 	*/
40 	class B2ITuple
41 	{
42 	protected:
43 		sal_Int32										mnX;
44 		sal_Int32										mnY;
45 
46 	public:
47 		/**	Create a 2D Tuple
48 
49         	The tuple is initialized to (0, 0)
50 		*/
51 		B2ITuple()
52 		:	mnX(0),
53 			mnY(0)
54 		{}
55 
56 		/**	Create a 2D Tuple
57 
58 			@param fX
59 			This parameter is used to initialize the X-coordinate
60 			of the 2D Tuple.
61 
62 			@param fY
63 			This parameter is used to initialize the Y-coordinate
64 			of the 2D Tuple.
65 		*/
66 		B2ITuple(sal_Int32 fX, sal_Int32 fY)
67 		:	mnX( fX ),
68 			mnY( fY )
69 		{}
70 
71 		/**	Create a copy of a 2D Tuple
72 
73 			@param rTup
74 			The 2D Tuple which will be copied.
75 		*/
76 		B2ITuple(const B2ITuple& rTup)
77 		:	mnX( rTup.mnX ),
78 			mnY( rTup.mnY )
79 		{}
80 
81 		~B2ITuple()
82 		{}
83 
84 		/// Get X-Coordinate of 2D Tuple
85 		sal_Int32 getX() const
86 		{
87 			return mnX;
88 		}
89 
90 		/// Get Y-Coordinate of 2D Tuple
91 		sal_Int32 getY() const
92 		{
93 			return mnY;
94 		}
95 
96 		/// Set X-Coordinate of 2D Tuple
97 		void setX(sal_Int32 fX)
98 		{
99 			mnX = fX;
100 		}
101 
102 		/// Set Y-Coordinate of 2D Tuple
103 		void setY(sal_Int32 fY)
104 		{
105 			mnY = fY;
106 		}
107 
108 		/// Array-access to 2D Tuple
109 		const sal_Int32& operator[] (int nPos) const
110 		{
111 			// Here, normally one if(...) should be used. In the assumption that
112 			// both sal_Int32 members can be accessed as an array a shortcut is used here.
113 			// if(0 == nPos) return mnX; return mnY;
114 			return *((&mnX) + nPos);
115 		}
116 
117 		/// Array-access to 2D Tuple
118 		sal_Int32& operator[] (int nPos)
119 		{
120 			// Here, normally one if(...) should be used. In the assumption that
121 			// both sal_Int32 members can be accessed as an array a shortcut is used here.
122 			// if(0 == nPos) return mnX; return mnY;
123 			return *((&mnX) + nPos);
124 		}
125 
126 		// operators
127 		//////////////////////////////////////////////////////////////////////
128 
129 		B2ITuple& operator+=( const B2ITuple& rTup )
130 		{
131 			mnX += rTup.mnX;
132 			mnY += rTup.mnY;
133 			return *this;
134 		}
135 
136 		B2ITuple& operator-=( const B2ITuple& rTup )
137 		{
138 			mnX -= rTup.mnX;
139 			mnY -= rTup.mnY;
140 			return *this;
141 		}
142 
143 		B2ITuple& operator/=( const B2ITuple& rTup )
144 		{
145 			mnX /= rTup.mnX;
146 			mnY /= rTup.mnY;
147 			return *this;
148 		}
149 
150 		B2ITuple& operator*=( const B2ITuple& rTup )
151 		{
152 			mnX *= rTup.mnX;
153 			mnY *= rTup.mnY;
154 			return *this;
155 		}
156 
157 		B2ITuple& operator*=(sal_Int32 t)
158 		{
159 			mnX *= t;
160 			mnY *= t;
161 			return *this;
162 		}
163 
164 		B2ITuple& operator/=(sal_Int32 t)
165 		{
166 			mnX /= t;
167 			mnY /= t;
168 			return *this;
169 		}
170 
171 		B2ITuple operator-(void) const
172 		{
173 			return B2ITuple(-mnX, -mnY);
174 		}
175 
176 		bool equalZero() const { return mnX == 0 && mnY == 0; }
177 
178 		bool operator==( const B2ITuple& rTup ) const
179 		{
180 			return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY);
181 		}
182 
183 		bool operator!=( const B2ITuple& rTup ) const
184 		{
185 			return !(*this == rTup);
186 		}
187 
188 		B2ITuple& operator=( const B2ITuple& rTup )
189 		{
190 			mnX = rTup.mnX;
191 			mnY = rTup.mnY;
192 			return *this;
193 		}
194 
195 		static const B2ITuple& getEmptyTuple();
196 	};
197 
198 	// external operators
199 	//////////////////////////////////////////////////////////////////////////
200 
201     class B2DTuple;
202 
203 	B2ITuple minimum(const B2ITuple& rTupA, const B2ITuple& rTupB);
204 
205 	B2ITuple maximum(const B2ITuple& rTupA, const B2ITuple& rTupB);
206 
207 	B2ITuple absolute(const B2ITuple& rTup);
208 
209 	B2DTuple interpolate(const B2ITuple& rOld1, const B2ITuple& rOld2, double t);
210 
211 	B2DTuple average(const B2ITuple& rOld1, const B2ITuple& rOld2);
212 
213 	B2DTuple average(const B2ITuple& rOld1, const B2ITuple& rOld2, const B2ITuple& rOld3);
214 
215 	B2ITuple operator+(const B2ITuple& rTupA, const B2ITuple& rTupB);
216 
217 	B2ITuple operator-(const B2ITuple& rTupA, const B2ITuple& rTupB);
218 
219 	B2ITuple operator/(const B2ITuple& rTupA, const B2ITuple& rTupB);
220 
221 	B2ITuple operator*(const B2ITuple& rTupA, const B2ITuple& rTupB);
222 
223 	B2ITuple operator*(const B2ITuple& rTup, sal_Int32 t);
224 
225 	B2ITuple operator*(sal_Int32 t, const B2ITuple& rTup);
226 
227 	B2ITuple operator/(const B2ITuple& rTup, sal_Int32 t);
228 
229 	B2ITuple operator/(sal_Int32 t, const B2ITuple& rTup);
230 
231 } // end of namespace basegfx
232 
233 #endif /* _BGFX_TUPLE_B2ITUPLE_HXX */
234