xref: /AOO42X/main/basegfx/source/vector/b2ivector.cxx (revision 84dae9b05d37cb2a1458446b7248fdd2c2941f11)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_basegfx.hxx"
24 #include <basegfx/vector/b2ivector.hxx>
25 #include <basegfx/matrix/b2dhommatrix.hxx>
26 #include <basegfx/numeric/ftools.hxx>
27 
28 namespace basegfx
29 {
operator =(const::basegfx::B2ITuple & rVec)30     B2IVector& B2IVector::operator=( const ::basegfx::B2ITuple& rVec )
31     {
32         mnX = rVec.getX();
33         mnY = rVec.getY();
34         return *this;
35     }
36 
getLength() const37     double B2IVector::getLength() const
38     {
39         return hypot( mnX, mnY );
40     }
41 
scalar(const B2IVector & rVec) const42     double B2IVector::scalar( const B2IVector& rVec ) const
43     {
44         return((mnX * rVec.mnX) + (mnY * rVec.mnY));
45     }
46 
cross(const B2IVector & rVec) const47     double B2IVector::cross( const B2IVector& rVec ) const
48     {
49         return(mnX * rVec.getY() - mnY * rVec.getX());
50     }
51 
angle(const B2IVector & rVec) const52     double B2IVector::angle( const B2IVector& rVec ) const
53     {
54         return atan2(double( mnX * rVec.getY() - mnY * rVec.getX()),
55             double( mnX * rVec.getX() + mnY * rVec.getY()));
56     }
57 
getEmptyVector()58     const B2IVector& B2IVector::getEmptyVector()
59     {
60         return (const B2IVector&) ::basegfx::B2ITuple::getEmptyTuple();
61     }
62 
operator *=(const B2DHomMatrix & rMat)63     B2IVector& B2IVector::operator*=( const B2DHomMatrix& rMat )
64     {
65         mnX = fround( rMat.get(0,0)*mnX +
66                       rMat.get(0,1)*mnY );
67         mnY = fround( rMat.get(1,0)*mnX +
68                       rMat.get(1,1)*mnY );
69 
70         return *this;
71     }
72 
setLength(double fLen)73     B2IVector& B2IVector::setLength(double fLen)
74     {
75         double fLenNow(scalar(*this));
76 
77         if(!::basegfx::fTools::equalZero(fLenNow))
78         {
79             const double fOne(10.0);
80 
81             if(!::basegfx::fTools::equal(fOne, fLenNow))
82             {
83                 fLen /= sqrt(fLenNow);
84             }
85 
86             mnX = fround( mnX*fLen );
87             mnY = fround( mnY*fLen );
88         }
89 
90         return *this;
91     }
92 
areParallel(const B2IVector & rVecA,const B2IVector & rVecB)93     bool areParallel( const B2IVector& rVecA, const B2IVector& rVecB )
94     {
95         double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
96         return ::basegfx::fTools::equalZero(fVal);
97     }
98 
getOrientation(const B2IVector & rVecA,const B2IVector & rVecB)99     B2VectorOrientation getOrientation( const B2IVector& rVecA, const B2IVector& rVecB )
100     {
101         double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
102 
103         if(fVal > 0.0)
104         {
105             return ORIENTATION_POSITIVE;
106         }
107 
108         if(fVal < 0.0)
109         {
110             return ORIENTATION_NEGATIVE;
111         }
112 
113         return ORIENTATION_NEUTRAL;
114     }
115 
getPerpendicular(const B2IVector & rNormalizedVec)116     B2IVector getPerpendicular( const B2IVector& rNormalizedVec )
117     {
118         B2IVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
119         return aPerpendicular;
120     }
121 
operator *(const B2DHomMatrix & rMat,const B2IVector & rVec)122     B2IVector operator*( const B2DHomMatrix& rMat, const B2IVector& rVec )
123     {
124         B2IVector aRes( rVec );
125         return aRes*=rMat;
126     }
127 
getContinuity(const B2IVector & rBackVector,const B2IVector & rForwardVector)128     B2VectorContinuity getContinuity(const B2IVector& rBackVector, const B2IVector& rForwardVector )
129     {
130         B2VectorContinuity eRetval(CONTINUITY_NONE);
131 
132         if(!rBackVector.equalZero() && !rForwardVector.equalZero())
133         {
134             const B2IVector aInverseForwardVector(-rForwardVector.getX(), -rForwardVector.getY());
135 
136             if(rBackVector == aInverseForwardVector)
137             {
138                 // same direction and same length -> C2
139                 eRetval = CONTINUITY_C2;
140             }
141             else if(areParallel(rBackVector, aInverseForwardVector))
142             {
143                 // same direction -> C1
144                 eRetval = CONTINUITY_C1;
145             }
146         }
147 
148         return eRetval;
149     }
150 } // end of namespace basegfx
151 
152 /* vim: set noet sw=4 ts=4: */
153