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 _VECTOR2D_HXX 29 #define _VECTOR2D_HXX 30 31 #include <math.h> 32 #include <tools/gen.hxx> 33 34 // ------------ 35 // - Vector2D - 36 // ------------ 37 38 class Vector2D 39 { 40 private: 41 double mfX; 42 double mfY; 43 44 public: 45 inline Vector2D() : mfX( 0.0 ), mfY( 0.0 ) {} 46 inline Vector2D( double fX, double fY ) : mfX( fX ), mfY( fY ) {} 47 inline Vector2D( const Vector2D& rVec ) : mfX( rVec.mfX ), mfY( rVec.mfY ) {} 48 inline Vector2D( const Pair& rPair ) : mfX( rPair.nA ), mfY( rPair.nB ) {}; 49 inline ~Vector2D() {} 50 51 inline const double& X() const { return mfX; } 52 inline const double& Y() const { return mfY; } 53 inline double& X() { return mfX; } 54 inline double& Y() { return mfY; } 55 inline const double& operator[] (int nPos) const { return (nPos ? mfY : mfX); } 56 inline double& operator[] (int nPos) { return (nPos ? mfY : mfX); } 57 58 inline double GetLength() const { return hypot( mfX, mfY ); } 59 inline Vector2D& Normalize(); 60 61 inline void Min(const Vector2D& rVec) { if(rVec.mfX < mfX) mfX = rVec.mfX; if(rVec.mfY < mfY) mfY = rVec.mfY; } 62 inline void Max(const Vector2D& rVec) { if(rVec.mfX > mfX) mfX = rVec.mfX; if(rVec.mfY > mfY) mfY = rVec.mfY; } 63 inline void Abs() { if(mfX < 0.0) mfX = -mfX; if(mfY < 0.0) mfY = -mfY; } 64 65 inline void CalcInBetween(Vector2D& rOld1, Vector2D& rOld2, double t) 66 { mfX = ((rOld2.mfX - rOld1.mfX) + t) + rOld1.mfX; mfY = ((rOld2.mfY - rOld1.mfY) + t) + rOld1.mfY; } 67 inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2) 68 { mfX = (rOld1.mfX + rOld2.mfX) / 2.0; mfY = (rOld1.mfY + rOld2.mfY) / 2.0; } 69 inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2, Vector2D& rOld3) 70 { mfX = (rOld1.mfX + rOld2.mfX + rOld3.mfX) / 3.0; mfY = (rOld1.mfY + rOld2.mfY + rOld3.mfY) / 3.0; } 71 72 inline Vector2D& operator+=( const Vector2D& rVec ) { mfX += rVec.mfX, mfY += rVec.mfY; return *this; } 73 inline Vector2D& operator-=( const Vector2D& rVec ) { mfX -= rVec.mfX, mfY -= rVec.mfY; return *this; } 74 inline Vector2D operator+(const Vector2D& rVec) const { Vector2D aSum(*this); aSum += rVec; return aSum; } 75 inline Vector2D operator-(const Vector2D& rVec) const { Vector2D aSub(*this); aSub -= rVec; return aSub; } 76 inline Vector2D operator-(void) const { return Vector2D(-mfX, -mfY); } 77 78 inline double Scalar( const Vector2D& rVec ) const { return( mfX * rVec.mfX + mfY * rVec.mfY ); } 79 80 inline Vector2D& operator/=( const Vector2D& rVec ) { mfX /= rVec.mfX, mfY /= rVec.mfY; return *this; } 81 inline Vector2D& operator*=( const Vector2D& rVec ) { mfX *= rVec.mfX, mfY *= rVec.mfY; return *this; } 82 inline Vector2D operator/(const Vector2D& rVec) const { Vector2D aDiv(*this); aDiv /= rVec; return aDiv; } 83 inline Vector2D operator*(const Vector2D& rVec) const { Vector2D aMul(*this); aMul *= rVec; return aMul; } 84 85 inline Vector2D& operator*=(double t) { mfX *= t; mfY *= t; return *this; } 86 inline Vector2D operator*(double t) const { Vector2D aNew(*this); aNew *= t; return aNew; } 87 inline Vector2D& operator/=(double t) { mfX /= t; mfY /= t; return *this; } 88 inline Vector2D operator/(double t) const { Vector2D aNew(*this); aNew /= t; return aNew; } 89 90 inline sal_Bool operator==( const Vector2D& rVec ) const { return( mfX == rVec.mfX && mfY == rVec.mfY ); } 91 inline sal_Bool operator!=( const Vector2D& rVec ) const { return !( *this == rVec ); } 92 93 inline Vector2D& operator=( const Vector2D& rVec ) { mfX = rVec.mfX, mfY = rVec.mfY; return *this; } 94 inline Vector2D& operator=( const Pair& rPair ) { mfX = rPair.nA, mfY = rPair.nB; return *this; } 95 inline Vector2D& operator-=( const Pair& rPair ) { mfX -= rPair.nA, mfY -= rPair.nB; return *this; } 96 inline Vector2D& operator+=( const Pair& rPair ) { mfX += rPair.nA, mfY += rPair.nB; return *this; } 97 inline Vector2D& operator*=( const Pair& rPair ) { mfX *= rPair.nA, mfY *= rPair.nB; return *this; } 98 inline Vector2D& operator/=( const Pair& rPair ) { mfX /= rPair.nA, mfY /= rPair.nB; return *this; } 99 100 inline sal_Bool operator==( const Pair& rPair ) const { return( mfX == rPair.nA && mfY == rPair.nB ); } 101 inline sal_Bool operator!=( const Pair& rPair ) const { return !( *this == rPair ); } 102 103 inline sal_Bool IsPositive( Vector2D& rVec ) const { return( ( mfX * rVec.mfY - mfY * rVec.mfX ) >= 0.0 ); } 104 inline sal_Bool IsNegative( Vector2D& rVec ) const { return !IsPositive( rVec ); } 105 }; 106 107 // ----------------------------------------------------------------------------- 108 109 inline Vector2D& Vector2D::Normalize() 110 { 111 double fLen = Scalar( *this ); 112 113 if( ( fLen != 0.0 ) && ( fLen != 1.0 ) && ( ( fLen = sqrt( fLen ) ) != 0.0 ) ) 114 mfX /= fLen, mfY /= fLen; 115 116 return *this; 117 } 118 119 #endif // _SV_VECTOR2D_HXX 120