xref: /trunk/main/basegfx/source/vector/b2dvector.cxx (revision 18e39f1b61b4f1cbad5dde1ff548849e2cddffe7)
109dbbe93SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
309dbbe93SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
409dbbe93SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
509dbbe93SAndrew Rist  * distributed with this work for additional information
609dbbe93SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
709dbbe93SAndrew Rist  * to you under the Apache License, Version 2.0 (the
809dbbe93SAndrew Rist  * "License"); you may not use this file except in compliance
909dbbe93SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1109dbbe93SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1309dbbe93SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1409dbbe93SAndrew Rist  * software distributed under the License is distributed on an
1509dbbe93SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1609dbbe93SAndrew Rist  * KIND, either express or implied.  See the License for the
1709dbbe93SAndrew Rist  * specific language governing permissions and limitations
1809dbbe93SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2009dbbe93SAndrew Rist  *************************************************************/
2109dbbe93SAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
24cdf0e10cSrcweir #include <basegfx/vector/b2dvector.hxx>
25cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
26cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir namespace basegfx
29cdf0e10cSrcweir {
normalize()30cdf0e10cSrcweir     B2DVector& B2DVector::normalize()
31cdf0e10cSrcweir     {
32cdf0e10cSrcweir         double fLen(scalar(*this));
33cdf0e10cSrcweir 
34cdf0e10cSrcweir         if(fTools::equalZero(fLen))
35cdf0e10cSrcweir         {
36cdf0e10cSrcweir             mfX = 0.0;
37cdf0e10cSrcweir             mfY = 0.0;
38cdf0e10cSrcweir         }
39cdf0e10cSrcweir         else
40cdf0e10cSrcweir         {
41cdf0e10cSrcweir             const double fOne(1.0);
42cdf0e10cSrcweir 
43cdf0e10cSrcweir             if(!fTools::equal(fOne, fLen))
44cdf0e10cSrcweir             {
45cdf0e10cSrcweir                 fLen = sqrt(fLen);
46cdf0e10cSrcweir 
47cdf0e10cSrcweir                 if(!fTools::equalZero(fLen))
48cdf0e10cSrcweir                 {
49cdf0e10cSrcweir                     mfX /= fLen;
50cdf0e10cSrcweir                     mfY /= fLen;
51cdf0e10cSrcweir                 }
52cdf0e10cSrcweir             }
53cdf0e10cSrcweir         }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         return *this;
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
operator =(const B2DTuple & rVec)58cdf0e10cSrcweir     B2DVector& B2DVector::operator=( const B2DTuple& rVec )
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         mfX = rVec.getX();
61cdf0e10cSrcweir         mfY = rVec.getY();
62cdf0e10cSrcweir         return *this;
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
getLength() const65cdf0e10cSrcweir     double B2DVector::getLength() const
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         if(fTools::equalZero(mfX))
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             return fabs(mfY);
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir         else if(fTools::equalZero(mfY))
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             return fabs(mfX);
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         return hypot( mfX, mfY );
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
scalar(const B2DVector & rVec) const79cdf0e10cSrcweir     double B2DVector::scalar( const B2DVector& rVec ) const
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         return((mfX * rVec.mfX) + (mfY * rVec.mfY));
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
cross(const B2DVector & rVec) const84cdf0e10cSrcweir     double B2DVector::cross( const B2DVector& rVec ) const
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         return(mfX * rVec.getY() - mfY * rVec.getX());
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
angle(const B2DVector & rVec) const89cdf0e10cSrcweir     double B2DVector::angle( const B2DVector& rVec ) const
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         return atan2(mfX * rVec.getY() - mfY * rVec.getX(),
92cdf0e10cSrcweir             mfX * rVec.getX() + mfY * rVec.getY());
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
getEmptyVector()95cdf0e10cSrcweir     const B2DVector& B2DVector::getEmptyVector()
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         return (const B2DVector&) B2DTuple::getEmptyTuple();
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
operator *=(const B2DHomMatrix & rMat)100cdf0e10cSrcweir     B2DVector& B2DVector::operator*=( const B2DHomMatrix& rMat )
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         const double fTempX( rMat.get(0,0)*mfX +
103cdf0e10cSrcweir                             rMat.get(0,1)*mfY );
104cdf0e10cSrcweir         const double fTempY( rMat.get(1,0)*mfX +
105cdf0e10cSrcweir                             rMat.get(1,1)*mfY );
106cdf0e10cSrcweir         mfX = fTempX;
107cdf0e10cSrcweir         mfY = fTempY;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         return *this;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
setLength(double fLen)112cdf0e10cSrcweir     B2DVector& B2DVector::setLength(double fLen)
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         double fLenNow(scalar(*this));
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         if(!fTools::equalZero(fLenNow))
117cdf0e10cSrcweir         {
1181c346908SArmin Le Grand             const double fOne(1.0);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             if(!fTools::equal(fOne, fLenNow))
121cdf0e10cSrcweir             {
122cdf0e10cSrcweir                 fLen /= sqrt(fLenNow);
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             mfX *= fLen;
126cdf0e10cSrcweir             mfY *= fLen;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         return *this;
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
isNormalized() const132cdf0e10cSrcweir     bool B2DVector::isNormalized() const
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         const double fOne(1.0);
135cdf0e10cSrcweir         const double fScalar(scalar(*this));
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         return fTools::equal(fOne, fScalar);
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
areParallel(const B2DVector & rVecA,const B2DVector & rVecB)140cdf0e10cSrcweir     bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         const double fValA(rVecA.getX() * rVecB.getY());
143cdf0e10cSrcweir         const double fValB(rVecA.getY() * rVecB.getX());
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         return fTools::equal(fValA, fValB);
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
getOrientation(const B2DVector & rVecA,const B2DVector & rVecB)148cdf0e10cSrcweir     B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         if(fTools::equalZero(fVal))
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             return ORIENTATION_NEUTRAL;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         if(fVal > 0.0)
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             return ORIENTATION_POSITIVE;
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir         else
162cdf0e10cSrcweir         {
163cdf0e10cSrcweir             return ORIENTATION_NEGATIVE;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
getPerpendicular(const B2DVector & rNormalizedVec)167cdf0e10cSrcweir     B2DVector getPerpendicular( const B2DVector& rNormalizedVec )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         B2DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
170cdf0e10cSrcweir         return aPerpendicular;
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
getNormalizedPerpendicular(const B2DVector & rVec)173cdf0e10cSrcweir     B2DVector getNormalizedPerpendicular( const B2DVector& rVec )
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         B2DVector aPerpendicular(rVec);
176cdf0e10cSrcweir         aPerpendicular.normalize();
177cdf0e10cSrcweir         const double aTemp(-aPerpendicular.getY());
178cdf0e10cSrcweir         aPerpendicular.setY(aPerpendicular.getX());
179cdf0e10cSrcweir         aPerpendicular.setX(aTemp);
180cdf0e10cSrcweir         return aPerpendicular;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
operator *(const B2DHomMatrix & rMat,const B2DVector & rVec)183cdf0e10cSrcweir     B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         B2DVector aRes( rVec );
186cdf0e10cSrcweir         return aRes*=rMat;
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
getContinuity(const B2DVector & rBackVector,const B2DVector & rForwardVector)189cdf0e10cSrcweir     B2VectorContinuity getContinuity(const B2DVector& rBackVector, const B2DVector& rForwardVector )
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         if(rBackVector.equalZero() || rForwardVector.equalZero())
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             return CONTINUITY_NONE;
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         if(fTools::equal(rBackVector.getX(), -rForwardVector.getX()) && fTools::equal(rBackVector.getY(), -rForwardVector.getY()))
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             // same direction and same length -> C2
199cdf0e10cSrcweir             return CONTINUITY_C2;
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         if(areParallel(rBackVector, rForwardVector) && rBackVector.scalar(rForwardVector) < 0.0)
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir             // parallel and opposite direction -> C1
205cdf0e10cSrcweir             return CONTINUITY_C1;
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         return CONTINUITY_NONE;
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir } // end of namespace basegfx
211cdf0e10cSrcweir 
212*18e39f1bSmseidel /* vim: set noet sw=4 ts=4: */
213