xref: /AOO41X/main/tools/inc/tools/vector2d.hxx (revision f1d601d52b7ab85e9456cf10ca14f7a084ce1c1e)
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:
41 	inline Vector2D() : mfX( 0.0 ), mfY( 0.0 ) {}
42 	inline Vector2D( double fX, double fY ) : mfX( fX ), mfY( fY ) {}
43 	inline Vector2D( const Vector2D& rVec ) : mfX( rVec.mfX ), mfY( rVec.mfY ) {}
44 	inline Vector2D( const Pair& rPair ) : mfX( rPair.nA ), mfY( rPair.nB ) {};
45 	inline ~Vector2D() {}
46 
47 	inline const double& X() const { return mfX; }
48 	inline const double& Y() const { return mfY; }
49 	inline double& X() { return mfX; }
50 	inline double& Y() { return mfY; }
51 	inline const double& operator[] (int nPos) const { return (nPos ? mfY : mfX); }
52 	inline double& operator[] (int nPos) { return (nPos ? mfY : mfX); }
53 
54 	inline double GetLength() const { return hypot( mfX, mfY ); }
55 	inline Vector2D& Normalize();
56 
57 	inline void Min(const Vector2D& rVec) { if(rVec.mfX < mfX) mfX = rVec.mfX; if(rVec.mfY < mfY) mfY = rVec.mfY; }
58 	inline void Max(const Vector2D& rVec) { if(rVec.mfX > mfX) mfX = rVec.mfX; if(rVec.mfY > mfY) mfY = rVec.mfY; }
59 	inline void Abs() { if(mfX < 0.0) mfX = -mfX; if(mfY < 0.0) mfY = -mfY; }
60 
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; }
63 	inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2)
64 		{ mfX = (rOld1.mfX + rOld2.mfX) / 2.0; mfY = (rOld1.mfY + rOld2.mfY) / 2.0; }
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 
68 	inline Vector2D&	operator+=( const Vector2D& rVec ) { mfX += rVec.mfX, mfY += rVec.mfY; return *this; }
69 	inline Vector2D&	operator-=( const Vector2D& rVec ) { mfX -= rVec.mfX, mfY -= rVec.mfY; return *this; }
70 	inline Vector2D		operator+(const Vector2D& rVec) const { Vector2D aSum(*this); aSum += rVec; return aSum; }
71 	inline Vector2D		operator-(const Vector2D& rVec) const { Vector2D aSub(*this); aSub -= rVec; return aSub; }
72 	inline Vector2D		operator-(void) const {	return Vector2D(-mfX, -mfY); }
73 
74 	inline double		Scalar( const Vector2D& rVec ) const { return( mfX * rVec.mfX + mfY * rVec.mfY ); }
75 
76 	inline Vector2D&	operator/=( const Vector2D& rVec ) { mfX /= rVec.mfX, mfY /= rVec.mfY; return *this; }
77 	inline Vector2D&	operator*=( const Vector2D& rVec ) { mfX *= rVec.mfX, mfY *= rVec.mfY; return *this; }
78 	inline Vector2D		operator/(const Vector2D& rVec) const { Vector2D aDiv(*this); aDiv /= rVec; return aDiv; }
79 	inline Vector2D		operator*(const Vector2D& rVec) const { Vector2D aMul(*this); aMul *= rVec; return aMul; }
80 
81 	inline Vector2D&	operator*=(double t) { mfX *= t; mfY *= t; return *this; }
82 	inline Vector2D		operator*(double t) const { Vector2D aNew(*this); aNew *= t; return aNew; }
83 	inline Vector2D&	operator/=(double t) { mfX /= t; mfY /= t; return *this; }
84 	inline Vector2D		operator/(double t) const { Vector2D aNew(*this); aNew /= t; return aNew; }
85 
86 	inline sal_Bool			operator==( const Vector2D& rVec ) const { return( mfX == rVec.mfX && mfY == rVec.mfY ); }
87 	inline sal_Bool			operator!=( const Vector2D& rVec ) const { return !( *this == rVec ); }
88 
89 	inline Vector2D&	operator=( const Vector2D& rVec ) { mfX = rVec.mfX, mfY = rVec.mfY; return *this; }
90 	inline Vector2D&	operator=( const Pair& rPair ) { mfX = rPair.nA, mfY = rPair.nB; return *this; }
91 	inline Vector2D&	operator-=( const Pair& rPair ) { mfX -= rPair.nA, mfY -= rPair.nB; return *this; }
92 	inline Vector2D&	operator+=( const Pair& rPair ) { mfX += rPair.nA, mfY += rPair.nB; return *this; }
93 	inline Vector2D&	operator*=( const Pair& rPair ) { mfX *= rPair.nA, mfY *= rPair.nB; return *this; }
94 	inline Vector2D&	operator/=( const Pair& rPair ) { mfX /= rPair.nA, mfY /= rPair.nB; return *this; }
95 
96 	inline sal_Bool			operator==( const Pair& rPair ) const { return( mfX == rPair.nA && mfY == rPair.nB ); }
97 	inline sal_Bool			operator!=( const Pair& rPair ) const { return !( *this == rPair ); }
98 
99 	inline sal_Bool			IsPositive( Vector2D& rVec ) const { return( ( mfX * rVec.mfY - mfY * rVec.mfX ) >= 0.0 ); }
100 	inline sal_Bool			IsNegative( Vector2D& rVec ) const { return !IsPositive( rVec ); }
101 };
102 
103 // -----------------------------------------------------------------------------
104 
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