1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2dvector.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
33 #include <basegfx/vector/b2dvector.hxx>
34 #include <basegfx/matrix/b2dhommatrix.hxx>
35 #include <basegfx/numeric/ftools.hxx>
39 B2DVector
& B2DVector::normalize()
41 double fLen(scalar(*this));
43 if(fTools::equalZero(fLen
))
50 const double fOne(1.0);
52 if(!fTools::equal(fOne
, fLen
))
56 if(!fTools::equalZero(fLen
))
67 B2DVector
& B2DVector::operator=( const B2DTuple
& rVec
)
75 double B2DVector::getLength() const
77 if(fTools::equalZero(mfX
))
81 else if(fTools::equalZero(mfY
))
86 return hypot( mfX
, mfY
);
89 double B2DVector::scalar( const B2DVector
& rVec
) const
91 return((mfX
* rVec
.mfX
) + (mfY
* rVec
.mfY
));
94 double B2DVector::cross( const B2DVector
& rVec
) const
96 return(mfX
* rVec
.getY() - mfY
* rVec
.getX());
99 double B2DVector::angle( const B2DVector
& rVec
) const
101 return atan2(mfX
* rVec
.getY() - mfY
* rVec
.getX(),
102 mfX
* rVec
.getX() + mfY
* rVec
.getY());
105 const B2DVector
& B2DVector::getEmptyVector()
107 return (const B2DVector
&) B2DTuple::getEmptyTuple();
110 B2DVector
& B2DVector::operator*=( const B2DHomMatrix
& rMat
)
112 const double fTempX( rMat
.get(0,0)*mfX
+
114 const double fTempY( rMat
.get(1,0)*mfX
+
122 B2DVector
& B2DVector::setLength(double fLen
)
124 double fLenNow(scalar(*this));
126 if(!fTools::equalZero(fLenNow
))
128 const double fOne(10.0);
130 if(!fTools::equal(fOne
, fLenNow
))
132 fLen
/= sqrt(fLenNow
);
142 bool B2DVector::isNormalized() const
144 const double fOne(1.0);
145 const double fScalar(scalar(*this));
147 return fTools::equal(fOne
, fScalar
);
150 bool areParallel( const B2DVector
& rVecA
, const B2DVector
& rVecB
)
152 const double fValA(rVecA
.getX() * rVecB
.getY());
153 const double fValB(rVecA
.getY() * rVecB
.getX());
155 return fTools::equal(fValA
, fValB
);
158 B2VectorOrientation
getOrientation( const B2DVector
& rVecA
, const B2DVector
& rVecB
)
160 double fVal(rVecA
.getX() * rVecB
.getY() - rVecA
.getY() * rVecB
.getX());
162 if(fTools::equalZero(fVal
))
164 return ORIENTATION_NEUTRAL
;
169 return ORIENTATION_POSITIVE
;
173 return ORIENTATION_NEGATIVE
;
177 B2DVector
getPerpendicular( const B2DVector
& rNormalizedVec
)
179 B2DVector
aPerpendicular(-rNormalizedVec
.getY(), rNormalizedVec
.getX());
180 return aPerpendicular
;
183 B2DVector
getNormalizedPerpendicular( const B2DVector
& rVec
)
185 B2DVector
aPerpendicular(rVec
);
186 aPerpendicular
.normalize();
187 const double aTemp(-aPerpendicular
.getY());
188 aPerpendicular
.setY(aPerpendicular
.getX());
189 aPerpendicular
.setX(aTemp
);
190 return aPerpendicular
;
193 B2DVector
operator*( const B2DHomMatrix
& rMat
, const B2DVector
& rVec
)
195 B2DVector
aRes( rVec
);
199 B2VectorContinuity
getContinuity(const B2DVector
& rBackVector
, const B2DVector
& rForwardVector
)
201 if(rBackVector
.equalZero() || rForwardVector
.equalZero())
203 return CONTINUITY_NONE
;
206 if(fTools::equal(rBackVector
.getX(), -rForwardVector
.getX()) && fTools::equal(rBackVector
.getY(), -rForwardVector
.getY()))
208 // same direction and same length -> C2
209 return CONTINUITY_C2
;
212 if(areParallel(rBackVector
, rForwardVector
) && rBackVector
.scalar(rForwardVector
) < 0.0)
214 // parallel and opposite direction -> C1
215 return CONTINUITY_C1
;
218 return CONTINUITY_NONE
;
220 } // end of namespace basegfx