xref: /AOO42X/main/basegfx/inc/basegfx/vector/b2ivector.hxx (revision 9bce9b0d387299c68bd81d539e1478357a103de5)
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 #ifndef _BGFX_VECTOR_B2IVECTOR_HXX
25 #define _BGFX_VECTOR_B2IVECTOR_HXX
26 
27 #include <basegfx/tuple/b2ituple.hxx>
28 #include <basegfx/vector/b2enums.hxx>
29 #include <basegfx/basegfxdllapi.h>
30 
31 namespace basegfx
32 {
33     // predeclaration
34     class B2DHomMatrix;
35 
36     /** Base Point class with two sal_Int32 values
37 
38         This class derives all operators and common handling for
39         a 2D data class from B2ITuple. All necessary extensions
40         which are special for 2D Vectors are added here.
41 
42         @see B2ITuple
43     */
44     class BASEGFX_DLLPUBLIC B2IVector : public ::basegfx::B2ITuple
45     {
46     public:
47         /** Create a 2D Vector
48 
49             The vector is initialized to (0, 0)
50         */
B2IVector()51         B2IVector()
52         :   B2ITuple()
53         {}
54 
55         /** Create a 2D Vector
56 
57             @param nX
58             This parameter is used to initialize the X-coordinate
59             of the 2D Vector.
60 
61             @param nY
62             This parameter is used to initialize the Y-coordinate
63             of the 2D Vector.
64         */
B2IVector(sal_Int32 nX,sal_Int32 nY)65         B2IVector(sal_Int32 nX, sal_Int32 nY)
66         :   B2ITuple(nX, nY)
67         {}
68 
69         /** Create a copy of a 2D Vector
70 
71             @param rVec
72             The 2D Vector which will be copied.
73         */
B2IVector(const B2IVector & rVec)74         B2IVector(const B2IVector& rVec)
75         :   B2ITuple(rVec)
76         {}
77 
78         /** constructor with tuple to allow copy-constructing
79             from B2ITuple-based classes
80         */
B2IVector(const::basegfx::B2ITuple & rTuple)81         B2IVector(const ::basegfx::B2ITuple& rTuple)
82         :   B2ITuple(rTuple)
83         {}
84 
~B2IVector()85         ~B2IVector()
86         {}
87 
88         /** *=operator to allow usage from B2IVector, too
89         */
operator *=(const B2IVector & rPnt)90         B2IVector& operator*=( const B2IVector& rPnt )
91         {
92             mnX *= rPnt.mnX;
93             mnY *= rPnt.mnY;
94             return *this;
95         }
96 
97         /** *=operator to allow usage from B2IVector, too
98         */
operator *=(sal_Int32 t)99         B2IVector& operator*=(sal_Int32 t)
100         {
101             mnX *= t;
102             mnY *= t;
103             return *this;
104         }
105 
106         /** assignment operator to allow assigning the results
107             of B2ITuple calculations
108         */
109         B2IVector& operator=( const ::basegfx::B2ITuple& rVec );
110 
111         /** Calculate the length of this 2D Vector
112 
113             @return The Length of the 2D Vector
114         */
115         double getLength() const;
116 
117         /** Set the length of this 2D Vector
118 
119             @param fLen
120             The to be achieved length of the 2D Vector
121         */
122         B2IVector& setLength(double fLen);
123 
124         /** Calculate the Scalar with another 2D Vector
125 
126             @param rVec
127             The second 2D Vector
128 
129             @return
130             The Scalar value of the two involved 2D Vectors
131         */
132         double scalar( const B2IVector& rVec ) const;
133 
134         /** Calculate the length of the cross product with another 2D Vector
135 
136             In 2D, returning an actual vector does not make much
137             sense here. The magnitude, although, can be readily
138             used for tasks such as angle calculations, since for
139             the returned value, the following equation holds:
140             retVal = getLength(this)*getLength(rVec)*sin(theta),
141             with theta being the angle between the two vectors.
142 
143             @param rVec
144             The second 2D Vector
145 
146             @return
147             The length of the cross product of the two involved 2D Vectors
148         */
149         double cross( const B2IVector& rVec ) const;
150 
151         /** Calculate the Angle with another 2D Vector
152 
153             @param rVec
154             The second 2D Vector
155 
156             @return
157             The Angle value of the two involved 2D Vectors in -pi/2 < return < pi/2
158         */
159         double angle( const B2IVector& rVec ) const;
160 
161         /** Transform vector by given transformation matrix.
162 
163             Since this is a vector, translational components of the
164             matrix are disregarded.
165         */
166         B2IVector& operator*=( const B2DHomMatrix& rMat );
167 
168         static const B2IVector& getEmptyVector();
169     };
170 
171     // external operators
172     //////////////////////////////////////////////////////////////////////////
173 
174     /** Calculate the orientation to another 2D Vector
175 
176         @param rVecA
177         The first 2D Vector
178 
179         @param rVecB
180         The second 2D Vector
181 
182         @return
183         The mathematical Orientation of the two involved 2D Vectors
184     */
185     BASEGFX_DLLPUBLIC B2VectorOrientation getOrientation( const B2IVector& rVecA, const B2IVector& rVecB );
186 
187     /** Calculate a perpendicular 2D Vector to the given one
188 
189         @param rVec
190         The source 2D Vector
191 
192         @return
193         A 2D Vector perpendicular to the one given in parameter rVec
194     */
195     BASEGFX_DLLPUBLIC B2IVector getPerpendicular( const B2IVector& rVec );
196 
197     /** Test two vectors which need not to be normalized for parallelism
198 
199         @param rVecA
200         The first 2D Vector
201 
202         @param rVecB
203         The second 2D Vector
204 
205         @return
206         bool if the two values are parallel. Also true if
207         one of the vectors is empty.
208     */
209     BASEGFX_DLLPUBLIC bool areParallel( const B2IVector& rVecA, const B2IVector& rVecB );
210 
211     /** Transform vector by given transformation matrix.
212 
213         Since this is a vector, translational components of the
214         matrix are disregarded.
215     */
216     BASEGFX_DLLPUBLIC B2IVector operator*( const B2DHomMatrix& rMat, const B2IVector& rVec );
217 
218     /** Test continuity between given vectors.
219 
220         The two given vectors are assumed to describe control points on a
221         common point. Calculate if there is a continuity between them.
222     */
223     BASEGFX_DLLPUBLIC B2VectorContinuity getContinuity( const B2IVector& rBackVector, const B2IVector& rForwardVector );
224 
225 } // end of namespace basegfx
226 
227 #endif /* _BGFX_VECTOR_B2IVECTOR_HXX */
228