merged tag ooo/OOO330_m14
[LibreOffice.git] / basegfx / source / vector / b2dvector.cxx
blobbb08b104eff836b616a426e78a75f435dd382567
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
30 #include <basegfx/vector/b2dvector.hxx>
31 #include <basegfx/matrix/b2dhommatrix.hxx>
32 #include <basegfx/numeric/ftools.hxx>
34 namespace basegfx
36 B2DVector& B2DVector::normalize()
38 double fLen(scalar(*this));
40 if(fTools::equalZero(fLen))
42 mfX = 0.0;
43 mfY = 0.0;
45 else
47 const double fOne(1.0);
49 if(!fTools::equal(fOne, fLen))
51 fLen = sqrt(fLen);
53 if(!fTools::equalZero(fLen))
55 mfX /= fLen;
56 mfY /= fLen;
61 return *this;
64 B2DVector& B2DVector::operator=( const B2DTuple& rVec )
66 mfX = rVec.getX();
67 mfY = rVec.getY();
68 return *this;
72 double B2DVector::getLength() const
74 if(fTools::equalZero(mfX))
76 return fabs(mfY);
78 else if(fTools::equalZero(mfY))
80 return fabs(mfX);
83 return hypot( mfX, mfY );
86 double B2DVector::scalar( const B2DVector& rVec ) const
88 return((mfX * rVec.mfX) + (mfY * rVec.mfY));
91 double B2DVector::cross( const B2DVector& rVec ) const
93 return(mfX * rVec.getY() - mfY * rVec.getX());
96 double B2DVector::angle( const B2DVector& rVec ) const
98 return atan2(mfX * rVec.getY() - mfY * rVec.getX(),
99 mfX * rVec.getX() + mfY * rVec.getY());
102 const B2DVector& B2DVector::getEmptyVector()
104 return (const B2DVector&) B2DTuple::getEmptyTuple();
107 B2DVector& B2DVector::operator*=( const B2DHomMatrix& rMat )
109 const double fTempX( rMat.get(0,0)*mfX +
110 rMat.get(0,1)*mfY );
111 const double fTempY( rMat.get(1,0)*mfX +
112 rMat.get(1,1)*mfY );
113 mfX = fTempX;
114 mfY = fTempY;
116 return *this;
119 B2DVector& B2DVector::setLength(double fLen)
121 double fLenNow(scalar(*this));
123 if(!fTools::equalZero(fLenNow))
125 const double fOne(10.0);
127 if(!fTools::equal(fOne, fLenNow))
129 fLen /= sqrt(fLenNow);
132 mfX *= fLen;
133 mfY *= fLen;
136 return *this;
139 bool B2DVector::isNormalized() const
141 const double fOne(1.0);
142 const double fScalar(scalar(*this));
144 return fTools::equal(fOne, fScalar);
147 bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB )
149 const double fValA(rVecA.getX() * rVecB.getY());
150 const double fValB(rVecA.getY() * rVecB.getX());
152 return fTools::equal(fValA, fValB);
155 B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB )
157 double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
159 if(fTools::equalZero(fVal))
161 return ORIENTATION_NEUTRAL;
164 if(fVal > 0.0)
166 return ORIENTATION_POSITIVE;
168 else
170 return ORIENTATION_NEGATIVE;
174 B2DVector getPerpendicular( const B2DVector& rNormalizedVec )
176 B2DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
177 return aPerpendicular;
180 B2DVector getNormalizedPerpendicular( const B2DVector& rVec )
182 B2DVector aPerpendicular(rVec);
183 aPerpendicular.normalize();
184 const double aTemp(-aPerpendicular.getY());
185 aPerpendicular.setY(aPerpendicular.getX());
186 aPerpendicular.setX(aTemp);
187 return aPerpendicular;
190 B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec )
192 B2DVector aRes( rVec );
193 return aRes*=rMat;
196 B2VectorContinuity getContinuity(const B2DVector& rBackVector, const B2DVector& rForwardVector )
198 if(rBackVector.equalZero() || rForwardVector.equalZero())
200 return CONTINUITY_NONE;
203 if(fTools::equal(rBackVector.getX(), -rForwardVector.getX()) && fTools::equal(rBackVector.getY(), -rForwardVector.getY()))
205 // same direction and same length -> C2
206 return CONTINUITY_C2;
209 if(areParallel(rBackVector, rForwardVector) && rBackVector.scalar(rForwardVector) < 0.0)
211 // parallel and opposite direction -> C1
212 return CONTINUITY_C1;
215 return CONTINUITY_NONE;
217 } // end of namespace basegfx
219 // eof