1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basegfx/vector/b2dvector.hxx>
21 #include <basegfx/matrix/b2dhommatrix.hxx>
22 #include <basegfx/numeric/ftools.hxx>
26 B2DVector
& B2DVector::normalize()
28 double fLen(scalar(*this));
30 if(fTools::equalZero(fLen
))
37 const double fOne(1.0);
39 if(!fTools::equal(fOne
, fLen
))
43 if(!fTools::equalZero(fLen
))
54 B2DVector
& B2DVector::operator=( const B2DTuple
& rVec
)
62 double B2DVector::getLength() const
64 if(fTools::equalZero(mfX
))
68 else if(fTools::equalZero(mfY
))
73 return hypot( mfX
, mfY
);
76 double B2DVector::scalar( const B2DVector
& rVec
) const
78 return((mfX
* rVec
.mfX
) + (mfY
* rVec
.mfY
));
81 double B2DVector::cross( const B2DVector
& rVec
) const
83 return(mfX
* rVec
.getY() - mfY
* rVec
.getX());
86 double B2DVector::angle( const B2DVector
& rVec
) const
88 return atan2(mfX
* rVec
.getY() - mfY
* rVec
.getX(),
89 mfX
* rVec
.getX() + mfY
* rVec
.getY());
92 const B2DVector
& B2DVector::getEmptyVector()
94 return (const B2DVector
&) B2DTuple::getEmptyTuple();
97 B2DVector
& B2DVector::operator*=( const B2DHomMatrix
& rMat
)
99 const double fTempX( rMat
.get(0,0)*mfX
+
101 const double fTempY( rMat
.get(1,0)*mfX
+
109 B2DVector
& B2DVector::setLength(double fLen
)
111 double fLenNow(scalar(*this));
113 if(!fTools::equalZero(fLenNow
))
115 const double fOne(1.0);
117 if(!fTools::equal(fOne
, fLenNow
))
119 fLen
/= sqrt(fLenNow
);
129 bool areParallel( const B2DVector
& rVecA
, const B2DVector
& rVecB
)
131 const double fValA(rVecA
.getX() * rVecB
.getY());
132 const double fValB(rVecA
.getY() * rVecB
.getX());
134 return fTools::equal(fValA
, fValB
);
137 B2VectorOrientation
getOrientation( const B2DVector
& rVecA
, const B2DVector
& rVecB
)
139 double fVal(rVecA
.getX() * rVecB
.getY() - rVecA
.getY() * rVecB
.getX());
141 if(fTools::equalZero(fVal
))
143 return ORIENTATION_NEUTRAL
;
148 return ORIENTATION_POSITIVE
;
152 return ORIENTATION_NEGATIVE
;
156 B2DVector
getPerpendicular( const B2DVector
& rNormalizedVec
)
158 B2DVector
aPerpendicular(-rNormalizedVec
.getY(), rNormalizedVec
.getX());
159 return aPerpendicular
;
162 B2DVector
getNormalizedPerpendicular( const B2DVector
& rVec
)
164 B2DVector
aPerpendicular(rVec
);
165 aPerpendicular
.normalize();
166 const double aTemp(-aPerpendicular
.getY());
167 aPerpendicular
.setY(aPerpendicular
.getX());
168 aPerpendicular
.setX(aTemp
);
169 return aPerpendicular
;
172 B2DVector
operator*( const B2DHomMatrix
& rMat
, const B2DVector
& rVec
)
174 B2DVector
aRes( rVec
);
178 B2VectorContinuity
getContinuity(const B2DVector
& rBackVector
, const B2DVector
& rForwardVector
)
180 if(rBackVector
.equalZero() || rForwardVector
.equalZero())
182 return CONTINUITY_NONE
;
185 if(fTools::equal(rBackVector
.getX(), -rForwardVector
.getX()) && fTools::equal(rBackVector
.getY(), -rForwardVector
.getY()))
187 // same direction and same length -> C2
188 return CONTINUITY_C2
;
191 if(areParallel(rBackVector
, rForwardVector
) && rBackVector
.scalar(rForwardVector
) < 0.0)
193 // parallel and opposite direction -> C1
194 return CONTINUITY_C1
;
197 return CONTINUITY_NONE
;
199 } // end of namespace basegfx
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */