1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef _BGFX_TUPLE_B2ITUPLE_HXX 29 #define _BGFX_TUPLE_B2ITUPLE_HXX 30 31 #include <sal/types.h> 32 33 34 namespace basegfx 35 { 36 /** Base class for all Points/Vectors with two sal_Int32 values 37 38 This class provides all methods common to Point 39 avd Vector classes which are derived from here. 40 41 @derive Use this class to implement Points or Vectors 42 which are based on two sal_Int32 values 43 */ 44 class B2ITuple 45 { 46 protected: 47 sal_Int32 mnX; 48 sal_Int32 mnY; 49 50 public: 51 /** Create a 2D Tuple 52 53 The tuple is initialized to (0, 0) 54 */ 55 B2ITuple() 56 : mnX(0), 57 mnY(0) 58 {} 59 60 /** Create a 2D Tuple 61 62 @param fX 63 This parameter is used to initialize the X-coordinate 64 of the 2D Tuple. 65 66 @param fY 67 This parameter is used to initialize the Y-coordinate 68 of the 2D Tuple. 69 */ 70 B2ITuple(sal_Int32 fX, sal_Int32 fY) 71 : mnX( fX ), 72 mnY( fY ) 73 {} 74 75 /** Create a copy of a 2D Tuple 76 77 @param rTup 78 The 2D Tuple which will be copied. 79 */ 80 B2ITuple(const B2ITuple& rTup) 81 : mnX( rTup.mnX ), 82 mnY( rTup.mnY ) 83 {} 84 85 ~B2ITuple() 86 {} 87 88 /// Get X-Coordinate of 2D Tuple 89 sal_Int32 getX() const 90 { 91 return mnX; 92 } 93 94 /// Get Y-Coordinate of 2D Tuple 95 sal_Int32 getY() const 96 { 97 return mnY; 98 } 99 100 /// Set X-Coordinate of 2D Tuple 101 void setX(sal_Int32 fX) 102 { 103 mnX = fX; 104 } 105 106 /// Set Y-Coordinate of 2D Tuple 107 void setY(sal_Int32 fY) 108 { 109 mnY = fY; 110 } 111 112 /// Array-access to 2D Tuple 113 const sal_Int32& operator[] (int nPos) const 114 { 115 // Here, normally one if(...) should be used. In the assumption that 116 // both sal_Int32 members can be accessed as an array a shortcut is used here. 117 // if(0 == nPos) return mnX; return mnY; 118 return *((&mnX) + nPos); 119 } 120 121 /// Array-access to 2D Tuple 122 sal_Int32& operator[] (int nPos) 123 { 124 // Here, normally one if(...) should be used. In the assumption that 125 // both sal_Int32 members can be accessed as an array a shortcut is used here. 126 // if(0 == nPos) return mnX; return mnY; 127 return *((&mnX) + nPos); 128 } 129 130 // operators 131 ////////////////////////////////////////////////////////////////////// 132 133 B2ITuple& operator+=( const B2ITuple& rTup ) 134 { 135 mnX += rTup.mnX; 136 mnY += rTup.mnY; 137 return *this; 138 } 139 140 B2ITuple& operator-=( const B2ITuple& rTup ) 141 { 142 mnX -= rTup.mnX; 143 mnY -= rTup.mnY; 144 return *this; 145 } 146 147 B2ITuple& operator/=( const B2ITuple& rTup ) 148 { 149 mnX /= rTup.mnX; 150 mnY /= rTup.mnY; 151 return *this; 152 } 153 154 B2ITuple& operator*=( const B2ITuple& rTup ) 155 { 156 mnX *= rTup.mnX; 157 mnY *= rTup.mnY; 158 return *this; 159 } 160 161 B2ITuple& operator*=(sal_Int32 t) 162 { 163 mnX *= t; 164 mnY *= t; 165 return *this; 166 } 167 168 B2ITuple& operator/=(sal_Int32 t) 169 { 170 mnX /= t; 171 mnY /= t; 172 return *this; 173 } 174 175 B2ITuple operator-(void) const 176 { 177 return B2ITuple(-mnX, -mnY); 178 } 179 180 bool equalZero() const { return mnX == 0 && mnY == 0; } 181 182 bool operator==( const B2ITuple& rTup ) const 183 { 184 return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY); 185 } 186 187 bool operator!=( const B2ITuple& rTup ) const 188 { 189 return !(*this == rTup); 190 } 191 192 B2ITuple& operator=( const B2ITuple& rTup ) 193 { 194 mnX = rTup.mnX; 195 mnY = rTup.mnY; 196 return *this; 197 } 198 199 static const B2ITuple& getEmptyTuple(); 200 }; 201 202 // external operators 203 ////////////////////////////////////////////////////////////////////////// 204 205 class B2DTuple; 206 207 B2ITuple minimum(const B2ITuple& rTupA, const B2ITuple& rTupB); 208 209 B2ITuple maximum(const B2ITuple& rTupA, const B2ITuple& rTupB); 210 211 B2ITuple absolute(const B2ITuple& rTup); 212 213 B2DTuple interpolate(const B2ITuple& rOld1, const B2ITuple& rOld2, double t); 214 215 B2DTuple average(const B2ITuple& rOld1, const B2ITuple& rOld2); 216 217 B2DTuple average(const B2ITuple& rOld1, const B2ITuple& rOld2, const B2ITuple& rOld3); 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& rTupA, const B2ITuple& rTupB); 224 225 B2ITuple operator*(const B2ITuple& rTupA, const B2ITuple& rTupB); 226 227 B2ITuple operator*(const B2ITuple& rTup, sal_Int32 t); 228 229 B2ITuple operator*(sal_Int32 t, const B2ITuple& rTup); 230 231 B2ITuple operator/(const B2ITuple& rTup, sal_Int32 t); 232 233 B2ITuple operator/(sal_Int32 t, const B2ITuple& rTup); 234 235 } // end of namespace basegfx 236 237 #endif /* _BGFX_TUPLE_B2ITUPLE_HXX */ 238