Get the style color and number just once
[LibreOffice.git] / basegfx / source / vector / b2dvector.cxx
blob1f696237fecf87e4f48021b972d72f88663c46fd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
24 namespace basegfx
26 B2DVector& B2DVector::normalize()
28 double fLen(std::hypot(mnX, mnY));
30 if(!fTools::equalZero(fLen))
32 const double fOne(1.0);
34 if(!fTools::equal(fOne, fLen))
36 mnX /= fLen;
37 mnY /= fLen;
40 else
42 mnX = 0.0;
43 mnY = 0.0;
46 return *this;
49 double B2DVector::getLength() const
51 if(fTools::equalZero(mnX))
53 return fabs(mnY);
55 else if(fTools::equalZero(mnY))
57 return fabs(mnX);
60 return hypot( mnX, mnY );
63 double B2DVector::angle( const B2DVector& rVec ) const
65 return atan2(mnX * rVec.getY() - mnY * rVec.getX(),
66 mnX * rVec.getX() + mnY * rVec.getY());
69 const B2DVector& B2DVector::getEmptyVector()
71 return static_cast<const B2DVector&>( B2DTuple::getEmptyTuple() );
74 B2DVector& B2DVector::operator*=( const B2DHomMatrix& rMat )
76 const double fTempX( rMat.get(0,0)*mnX +
77 rMat.get(0,1)*mnY );
78 const double fTempY( rMat.get(1,0)*mnX +
79 rMat.get(1,1)*mnY );
80 mnX = fTempX;
81 mnY = fTempY;
83 return *this;
86 B2DVector& B2DVector::setLength(double fLen)
88 double fLenNow(std::hypot(mnX, mnY));
90 if(!fTools::equalZero(fLenNow))
92 const double fOne(1.0);
94 if(!fTools::equal(fOne, fLenNow))
96 fLen /= fLenNow;
99 mnX *= fLen;
100 mnY *= fLen;
103 return *this;
106 bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB )
108 const double fValA(rVecA.getX() * rVecB.getY());
109 const double fValB(rVecA.getY() * rVecB.getX());
111 return fTools::equal(fValA, fValB);
114 B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB )
116 double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
118 if(fTools::equalZero(fVal))
120 return B2VectorOrientation::Neutral;
123 if(fVal > 0.0)
125 return B2VectorOrientation::Positive;
127 else
129 return B2VectorOrientation::Negative;
133 B2DVector getPerpendicular( const B2DVector& rNormalizedVec )
135 B2DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
136 return aPerpendicular;
139 B2DVector getNormalizedPerpendicular( const B2DVector& rVec )
141 B2DVector aPerpendicular(rVec);
142 aPerpendicular.normalize();
143 const double aTemp(-aPerpendicular.getY());
144 aPerpendicular.setY(aPerpendicular.getX());
145 aPerpendicular.setX(aTemp);
146 return aPerpendicular;
149 B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec )
151 B2DVector aRes( rVec );
152 aRes *= rMat;
153 return aRes;
156 B2VectorContinuity getContinuity(const B2DVector& rBackVector, const B2DVector& rForwardVector )
158 if(rBackVector.equalZero() || rForwardVector.equalZero())
160 return B2VectorContinuity::NONE;
163 if(fTools::equal(rBackVector.getX(), -rForwardVector.getX()) && fTools::equal(rBackVector.getY(), -rForwardVector.getY()))
165 // same direction and same length -> C2
166 return B2VectorContinuity::C2;
169 if(areParallel(rBackVector, rForwardVector) && rBackVector.scalar(rForwardVector) < 0.0)
171 // parallel and opposite direction -> C1
172 return B2VectorContinuity::C1;
175 return B2VectorContinuity::NONE;
177 } // end of namespace basegfx
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */