xref: /trunk/main/basegfx/source/vector/b2ivector.cxx (revision 09dbbe93)
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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basegfx.hxx"
26 #include <basegfx/vector/b2ivector.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/numeric/ftools.hxx>
29 
30 namespace basegfx
31 {
operator =(const::basegfx::B2ITuple & rVec)32 	B2IVector& B2IVector::operator=( const ::basegfx::B2ITuple& rVec )
33 	{
34 		mnX = rVec.getX();
35 		mnY = rVec.getY();
36 		return *this;
37 	}
38 
39 
getLength() const40 	double B2IVector::getLength() const
41 	{
42 		return hypot( mnX, mnY );
43 	}
44 
scalar(const B2IVector & rVec) const45 	double B2IVector::scalar( const B2IVector& rVec ) const
46 	{
47 		return((mnX * rVec.mnX) + (mnY * rVec.mnY));
48 	}
49 
cross(const B2IVector & rVec) const50 	double B2IVector::cross( const B2IVector& rVec ) const
51 	{
52 		return(mnX * rVec.getY() - mnY * rVec.getX());
53 	}
54 
angle(const B2IVector & rVec) const55 	double B2IVector::angle( const B2IVector& rVec ) const
56 	{
57 		return atan2(double( mnX * rVec.getY() - mnY * rVec.getX()),
58 			double( mnX * rVec.getX() + mnY * rVec.getY()));
59 	}
60 
getEmptyVector()61 	const B2IVector& B2IVector::getEmptyVector()
62 	{
63 		return (const B2IVector&) ::basegfx::B2ITuple::getEmptyTuple();
64 	}
65 
operator *=(const B2DHomMatrix & rMat)66 	B2IVector& B2IVector::operator*=( const B2DHomMatrix& rMat )
67 	{
68 		mnX = fround( rMat.get(0,0)*mnX +
69                       rMat.get(0,1)*mnY );
70 		mnY = fround( rMat.get(1,0)*mnX +
71                       rMat.get(1,1)*mnY );
72 
73 		return *this;
74 	}
75 
setLength(double fLen)76 	B2IVector& B2IVector::setLength(double fLen)
77 	{
78 		double fLenNow(scalar(*this));
79 
80 		if(!::basegfx::fTools::equalZero(fLenNow))
81 		{
82 			const double fOne(10.0);
83 
84 			if(!::basegfx::fTools::equal(fOne, fLenNow))
85 			{
86 				fLen /= sqrt(fLenNow);
87 			}
88 
89 			mnX = fround( mnX*fLen );
90 			mnY = fround( mnY*fLen );
91 		}
92 
93 		return *this;
94 	}
95 
areParallel(const B2IVector & rVecA,const B2IVector & rVecB)96 	bool areParallel( const B2IVector& rVecA, const B2IVector& rVecB )
97 	{
98 		double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
99 		return ::basegfx::fTools::equalZero(fVal);
100 	}
101 
getOrientation(const B2IVector & rVecA,const B2IVector & rVecB)102 	B2VectorOrientation getOrientation( const B2IVector& rVecA, const B2IVector& rVecB )
103 	{
104 		double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
105 
106 		if(fVal > 0.0)
107 		{
108 			return ORIENTATION_POSITIVE;
109 		}
110 
111 		if(fVal < 0.0)
112 		{
113 			return ORIENTATION_NEGATIVE;
114 		}
115 
116 		return ORIENTATION_NEUTRAL;
117 	}
118 
getPerpendicular(const B2IVector & rNormalizedVec)119 	B2IVector getPerpendicular( const B2IVector& rNormalizedVec )
120 	{
121 		B2IVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
122 		return aPerpendicular;
123 	}
124 
operator *(const B2DHomMatrix & rMat,const B2IVector & rVec)125 	B2IVector operator*( const B2DHomMatrix& rMat, const B2IVector& rVec )
126 	{
127 		B2IVector aRes( rVec );
128 		return aRes*=rMat;
129 	}
130 
getContinuity(const B2IVector & rBackVector,const B2IVector & rForwardVector)131 	B2VectorContinuity getContinuity(const B2IVector& rBackVector, const B2IVector& rForwardVector )
132 	{
133 		B2VectorContinuity eRetval(CONTINUITY_NONE);
134 
135 		if(!rBackVector.equalZero() && !rForwardVector.equalZero())
136 		{
137 			const B2IVector aInverseForwardVector(-rForwardVector.getX(), -rForwardVector.getY());
138 
139 			if(rBackVector == aInverseForwardVector)
140 			{
141 				// same direction and same length -> C2
142 				eRetval = CONTINUITY_C2;
143 			}
144 			else if(areParallel(rBackVector, aInverseForwardVector))
145 			{
146 				// same direction -> C1
147 				eRetval = CONTINUITY_C1;
148 			}
149 		}
150 
151 		return eRetval;
152 	}
153 } // end of namespace basegfx
154 
155 // eof
156