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 _VECTOR2D_HXX
25 #define _VECTOR2D_HXX
26
27 #include <math.h>
28 #include <tools/gen.hxx>
29
30 // ------------
31 // - Vector2D -
32 // ------------
33
34 class Vector2D
35 {
36 private:
37 double mfX;
38 double mfY;
39
40 public:
Vector2D()41 inline Vector2D() : mfX( 0.0 ), mfY( 0.0 ) {}
Vector2D(double fX,double fY)42 inline Vector2D( double fX, double fY ) : mfX( fX ), mfY( fY ) {}
Vector2D(const Vector2D & rVec)43 inline Vector2D( const Vector2D& rVec ) : mfX( rVec.mfX ), mfY( rVec.mfY ) {}
Vector2D(const Pair & rPair)44 inline Vector2D( const Pair& rPair ) : mfX( rPair.nA ), mfY( rPair.nB ) {};
~Vector2D()45 inline ~Vector2D() {}
46
X() const47 inline const double& X() const { return mfX; }
Y() const48 inline const double& Y() const { return mfY; }
X()49 inline double& X() { return mfX; }
Y()50 inline double& Y() { return mfY; }
operator [](int nPos) const51 inline const double& operator[] (int nPos) const { return (nPos ? mfY : mfX); }
operator [](int nPos)52 inline double& operator[] (int nPos) { return (nPos ? mfY : mfX); }
53
GetLength() const54 inline double GetLength() const { return hypot( mfX, mfY ); }
55 inline Vector2D& Normalize();
56
Min(const Vector2D & rVec)57 inline void Min(const Vector2D& rVec) { if(rVec.mfX < mfX) mfX = rVec.mfX; if(rVec.mfY < mfY) mfY = rVec.mfY; }
Max(const Vector2D & rVec)58 inline void Max(const Vector2D& rVec) { if(rVec.mfX > mfX) mfX = rVec.mfX; if(rVec.mfY > mfY) mfY = rVec.mfY; }
Abs()59 inline void Abs() { if(mfX < 0.0) mfX = -mfX; if(mfY < 0.0) mfY = -mfY; }
60
CalcInBetween(Vector2D & rOld1,Vector2D & rOld2,double t)61 inline void CalcInBetween(Vector2D& rOld1, Vector2D& rOld2, double t)
62 { mfX = ((rOld2.mfX - rOld1.mfX) + t) + rOld1.mfX; mfY = ((rOld2.mfY - rOld1.mfY) + t) + rOld1.mfY; }
CalcMiddle(Vector2D & rOld1,Vector2D & rOld2)63 inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2)
64 { mfX = (rOld1.mfX + rOld2.mfX) / 2.0; mfY = (rOld1.mfY + rOld2.mfY) / 2.0; }
CalcMiddle(Vector2D & rOld1,Vector2D & rOld2,Vector2D & rOld3)65 inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2, Vector2D& rOld3)
66 { mfX = (rOld1.mfX + rOld2.mfX + rOld3.mfX) / 3.0; mfY = (rOld1.mfY + rOld2.mfY + rOld3.mfY) / 3.0; }
67
operator +=(const Vector2D & rVec)68 inline Vector2D& operator+=( const Vector2D& rVec ) { mfX += rVec.mfX, mfY += rVec.mfY; return *this; }
operator -=(const Vector2D & rVec)69 inline Vector2D& operator-=( const Vector2D& rVec ) { mfX -= rVec.mfX, mfY -= rVec.mfY; return *this; }
operator +(const Vector2D & rVec) const70 inline Vector2D operator+(const Vector2D& rVec) const { Vector2D aSum(*this); aSum += rVec; return aSum; }
operator -(const Vector2D & rVec) const71 inline Vector2D operator-(const Vector2D& rVec) const { Vector2D aSub(*this); aSub -= rVec; return aSub; }
operator -(void) const72 inline Vector2D operator-(void) const { return Vector2D(-mfX, -mfY); }
73
Scalar(const Vector2D & rVec) const74 inline double Scalar( const Vector2D& rVec ) const { return( mfX * rVec.mfX + mfY * rVec.mfY ); }
75
operator /=(const Vector2D & rVec)76 inline Vector2D& operator/=( const Vector2D& rVec ) { mfX /= rVec.mfX, mfY /= rVec.mfY; return *this; }
operator *=(const Vector2D & rVec)77 inline Vector2D& operator*=( const Vector2D& rVec ) { mfX *= rVec.mfX, mfY *= rVec.mfY; return *this; }
operator /(const Vector2D & rVec) const78 inline Vector2D operator/(const Vector2D& rVec) const { Vector2D aDiv(*this); aDiv /= rVec; return aDiv; }
operator *(const Vector2D & rVec) const79 inline Vector2D operator*(const Vector2D& rVec) const { Vector2D aMul(*this); aMul *= rVec; return aMul; }
80
operator *=(double t)81 inline Vector2D& operator*=(double t) { mfX *= t; mfY *= t; return *this; }
operator *(double t) const82 inline Vector2D operator*(double t) const { Vector2D aNew(*this); aNew *= t; return aNew; }
operator /=(double t)83 inline Vector2D& operator/=(double t) { mfX /= t; mfY /= t; return *this; }
operator /(double t) const84 inline Vector2D operator/(double t) const { Vector2D aNew(*this); aNew /= t; return aNew; }
85
operator ==(const Vector2D & rVec) const86 inline sal_Bool operator==( const Vector2D& rVec ) const { return( mfX == rVec.mfX && mfY == rVec.mfY ); }
operator !=(const Vector2D & rVec) const87 inline sal_Bool operator!=( const Vector2D& rVec ) const { return !( *this == rVec ); }
88
operator =(const Vector2D & rVec)89 inline Vector2D& operator=( const Vector2D& rVec ) { mfX = rVec.mfX, mfY = rVec.mfY; return *this; }
operator =(const Pair & rPair)90 inline Vector2D& operator=( const Pair& rPair ) { mfX = rPair.nA, mfY = rPair.nB; return *this; }
operator -=(const Pair & rPair)91 inline Vector2D& operator-=( const Pair& rPair ) { mfX -= rPair.nA, mfY -= rPair.nB; return *this; }
operator +=(const Pair & rPair)92 inline Vector2D& operator+=( const Pair& rPair ) { mfX += rPair.nA, mfY += rPair.nB; return *this; }
operator *=(const Pair & rPair)93 inline Vector2D& operator*=( const Pair& rPair ) { mfX *= rPair.nA, mfY *= rPair.nB; return *this; }
operator /=(const Pair & rPair)94 inline Vector2D& operator/=( const Pair& rPair ) { mfX /= rPair.nA, mfY /= rPair.nB; return *this; }
95
operator ==(const Pair & rPair) const96 inline sal_Bool operator==( const Pair& rPair ) const { return( mfX == rPair.nA && mfY == rPair.nB ); }
operator !=(const Pair & rPair) const97 inline sal_Bool operator!=( const Pair& rPair ) const { return !( *this == rPair ); }
98
IsPositive(Vector2D & rVec) const99 inline sal_Bool IsPositive( Vector2D& rVec ) const { return( ( mfX * rVec.mfY - mfY * rVec.mfX ) >= 0.0 ); }
IsNegative(Vector2D & rVec) const100 inline sal_Bool IsNegative( Vector2D& rVec ) const { return !IsPositive( rVec ); }
101 };
102
103 // -----------------------------------------------------------------------------
104
Normalize()105 inline Vector2D& Vector2D::Normalize()
106 {
107 double fLen = Scalar( *this );
108
109 if( ( fLen != 0.0 ) && ( fLen != 1.0 ) && ( ( fLen = sqrt( fLen ) ) != 0.0 ) )
110 mfX /= fLen, mfY /= fLen;
111
112 return *this;
113 }
114
115 #endif // _SV_VECTOR2D_HXX
116