xref: /trunk/main/basegfx/source/vector/b2ivector.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
30 #include <basegfx/vector/b2ivector.hxx>
31 #include <basegfx/matrix/b2dhommatrix.hxx>
32 #include <basegfx/numeric/ftools.hxx>
33 
34 namespace basegfx
35 {
36     B2IVector& B2IVector::operator=( const ::basegfx::B2ITuple& rVec )
37     {
38         mnX = rVec.getX();
39         mnY = rVec.getY();
40         return *this;
41     }
42 
43 
44     double B2IVector::getLength() const
45     {
46         return hypot( mnX, mnY );
47     }
48 
49     double B2IVector::scalar( const B2IVector& rVec ) const
50     {
51         return((mnX * rVec.mnX) + (mnY * rVec.mnY));
52     }
53 
54     double B2IVector::cross( const B2IVector& rVec ) const
55     {
56         return(mnX * rVec.getY() - mnY * rVec.getX());
57     }
58 
59     double B2IVector::angle( const B2IVector& rVec ) const
60     {
61         return atan2(double( mnX * rVec.getY() - mnY * rVec.getX()),
62             double( mnX * rVec.getX() + mnY * rVec.getY()));
63     }
64 
65     const B2IVector& B2IVector::getEmptyVector()
66     {
67         return (const B2IVector&) ::basegfx::B2ITuple::getEmptyTuple();
68     }
69 
70     B2IVector& B2IVector::operator*=( const B2DHomMatrix& rMat )
71     {
72         mnX = fround( rMat.get(0,0)*mnX +
73                       rMat.get(0,1)*mnY );
74         mnY = fround( rMat.get(1,0)*mnX +
75                       rMat.get(1,1)*mnY );
76 
77         return *this;
78     }
79 
80     B2IVector& B2IVector::setLength(double fLen)
81     {
82         double fLenNow(scalar(*this));
83 
84         if(!::basegfx::fTools::equalZero(fLenNow))
85         {
86             const double fOne(10.0);
87 
88             if(!::basegfx::fTools::equal(fOne, fLenNow))
89             {
90                 fLen /= sqrt(fLenNow);
91             }
92 
93             mnX = fround( mnX*fLen );
94             mnY = fround( mnY*fLen );
95         }
96 
97         return *this;
98     }
99 
100     bool areParallel( const B2IVector& rVecA, const B2IVector& rVecB )
101     {
102         double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
103         return ::basegfx::fTools::equalZero(fVal);
104     }
105 
106     B2VectorOrientation getOrientation( const B2IVector& rVecA, const B2IVector& rVecB )
107     {
108         double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
109 
110         if(fVal > 0.0)
111         {
112             return ORIENTATION_POSITIVE;
113         }
114 
115         if(fVal < 0.0)
116         {
117             return ORIENTATION_NEGATIVE;
118         }
119 
120         return ORIENTATION_NEUTRAL;
121     }
122 
123     B2IVector getPerpendicular( const B2IVector& rNormalizedVec )
124     {
125         B2IVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
126         return aPerpendicular;
127     }
128 
129     B2IVector operator*( const B2DHomMatrix& rMat, const B2IVector& rVec )
130     {
131         B2IVector aRes( rVec );
132         return aRes*=rMat;
133     }
134 
135     B2VectorContinuity getContinuity(const B2IVector& rBackVector, const B2IVector& rForwardVector )
136     {
137         B2VectorContinuity eRetval(CONTINUITY_NONE);
138 
139         if(!rBackVector.equalZero() && !rForwardVector.equalZero())
140         {
141             const B2IVector aInverseForwardVector(-rForwardVector.getX(), -rForwardVector.getY());
142 
143             if(rBackVector == aInverseForwardVector)
144             {
145                 // same direction and same length -> C2
146                 eRetval = CONTINUITY_C2;
147             }
148             else if(areParallel(rBackVector, aInverseForwardVector))
149             {
150                 // same direction -> C1
151                 eRetval = CONTINUITY_C1;
152             }
153         }
154 
155         return eRetval;
156     }
157 } // end of namespace basegfx
158 
159 // eof
160