Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #ifndef _VECTOR2D_HXX
21 : : #define _VECTOR2D_HXX
22 : :
23 : : #include <math.h>
24 : : #include <tools/gen.hxx>
25 : :
26 : : class Vector2D
27 : : {
28 : : private:
29 : : double mfX;
30 : : double mfY;
31 : :
32 : : public:
33 : : inline Vector2D() : mfX( 0.0 ), mfY( 0.0 ) {}
34 : : inline Vector2D( double fX, double fY ) : mfX( fX ), mfY( fY ) {}
35 : : inline Vector2D( const Vector2D& rVec ) : mfX( rVec.mfX ), mfY( rVec.mfY ) {}
36 : 0 : inline Vector2D( const Pair& rPair ) : mfX( rPair.nA ), mfY( rPair.nB ) {};
37 : 0 : inline ~Vector2D() {}
38 : :
39 : : inline const double& X() const { return mfX; }
40 : : inline const double& Y() const { return mfY; }
41 : : inline double& X() { return mfX; }
42 : : inline double& Y() { return mfY; }
43 : : inline const double& operator[] (int nPos) const { return (nPos ? mfY : mfX); }
44 : : inline double& operator[] (int nPos) { return (nPos ? mfY : mfX); }
45 : :
46 : 0 : inline double GetLength() const { return hypot( mfX, mfY ); }
47 : : inline Vector2D& Normalize();
48 : :
49 : : inline void Min(const Vector2D& rVec) { if(rVec.mfX < mfX) mfX = rVec.mfX; if(rVec.mfY < mfY) mfY = rVec.mfY; }
50 : : inline void Max(const Vector2D& rVec) { if(rVec.mfX > mfX) mfX = rVec.mfX; if(rVec.mfY > mfY) mfY = rVec.mfY; }
51 : : inline void Abs() { if(mfX < 0.0) mfX = -mfX; if(mfY < 0.0) mfY = -mfY; }
52 : :
53 : : inline void CalcInBetween(Vector2D& rOld1, Vector2D& rOld2, double t)
54 : : { mfX = ((rOld2.mfX - rOld1.mfX) + t) + rOld1.mfX; mfY = ((rOld2.mfY - rOld1.mfY) + t) + rOld1.mfY; }
55 : : inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2)
56 : : { mfX = (rOld1.mfX + rOld2.mfX) / 2.0; mfY = (rOld1.mfY + rOld2.mfY) / 2.0; }
57 : : inline void CalcMiddle(Vector2D& rOld1, Vector2D& rOld2, Vector2D& rOld3)
58 : : { mfX = (rOld1.mfX + rOld2.mfX + rOld3.mfX) / 3.0; mfY = (rOld1.mfY + rOld2.mfY + rOld3.mfY) / 3.0; }
59 : :
60 : : inline Vector2D& operator+=( const Vector2D& rVec ) { mfX += rVec.mfX, mfY += rVec.mfY; return *this; }
61 : : inline Vector2D& operator-=( const Vector2D& rVec ) { mfX -= rVec.mfX, mfY -= rVec.mfY; return *this; }
62 : : inline Vector2D operator+(const Vector2D& rVec) const { Vector2D aSum(*this); aSum += rVec; return aSum; }
63 : : inline Vector2D operator-(const Vector2D& rVec) const { Vector2D aSub(*this); aSub -= rVec; return aSub; }
64 : : inline Vector2D operator-(void) const { return Vector2D(-mfX, -mfY); }
65 : :
66 : 0 : inline double Scalar( const Vector2D& rVec ) const { return( mfX * rVec.mfX + mfY * rVec.mfY ); }
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 aDiv(*this); aDiv /= rVec; return aDiv; }
71 : : inline Vector2D operator*(const Vector2D& rVec) const { Vector2D aMul(*this); aMul *= rVec; return aMul; }
72 : :
73 : : inline Vector2D& operator*=(double t) { mfX *= t; mfY *= t; return *this; }
74 : : inline Vector2D operator*(double t) const { Vector2D aNew(*this); aNew *= t; return aNew; }
75 : : inline Vector2D& operator/=(double t) { mfX /= t; mfY /= t; return *this; }
76 : : inline Vector2D operator/(double t) const { Vector2D aNew(*this); aNew /= t; return aNew; }
77 : :
78 : : inline sal_Bool operator==( const Vector2D& rVec ) const { return( mfX == rVec.mfX && mfY == rVec.mfY ); }
79 : : inline sal_Bool operator!=( const Vector2D& rVec ) const { return !( *this == rVec ); }
80 : :
81 : : inline Vector2D& operator=( const Vector2D& rVec ) { mfX = rVec.mfX, mfY = rVec.mfY; return *this; }
82 : : inline Vector2D& operator=( const Pair& rPair ) { mfX = rPair.nA, mfY = rPair.nB; return *this; }
83 : 0 : inline Vector2D& operator-=( const Pair& rPair ) { mfX -= rPair.nA, mfY -= rPair.nB; return *this; }
84 : : inline Vector2D& operator+=( const Pair& rPair ) { mfX += rPair.nA, mfY += rPair.nB; return *this; }
85 : : inline Vector2D& operator*=( const Pair& rPair ) { mfX *= rPair.nA, mfY *= rPair.nB; return *this; }
86 : : inline Vector2D& operator/=( const Pair& rPair ) { mfX /= rPair.nA, mfY /= rPair.nB; return *this; }
87 : :
88 : : inline sal_Bool operator==( const Pair& rPair ) const { return( mfX == rPair.nA && mfY == rPair.nB ); }
89 : : inline sal_Bool operator!=( const Pair& rPair ) const { return !( *this == rPair ); }
90 : :
91 : 0 : inline sal_Bool IsPositive( Vector2D& rVec ) const { return( ( mfX * rVec.mfY - mfY * rVec.mfX ) >= 0.0 ); }
92 : 0 : inline sal_Bool IsNegative( Vector2D& rVec ) const { return !IsPositive( rVec ); }
93 : : };
94 : :
95 : 0 : inline Vector2D& Vector2D::Normalize()
96 : : {
97 : 0 : double fLen = Scalar( *this );
98 : :
99 [ # # ][ # # ]: 0 : if( ( fLen != 0.0 ) && ( fLen != 1.0 ) && ( ( fLen = sqrt( fLen ) ) != 0.0 ) )
[ # # ][ # # ]
100 : 0 : mfX /= fLen, mfY /= fLen;
101 : :
102 : 0 : return *this;
103 : : }
104 : :
105 : : #endif
106 : :
107 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|