LibreOffice Module basegfx (master) 1
b2dvector.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
23
24namespace basegfx
25{
27 {
28 double fLen(scalar(*this));
29
30 if(fTools::equalZero(fLen))
31 {
32 mfX = 0.0;
33 mfY = 0.0;
34 }
35 else
36 {
37 const double fOne(1.0);
38
39 if(!fTools::equal(fOne, fLen))
40 {
41 fLen = sqrt(fLen);
42
43 if(!fTools::equalZero(fLen))
44 {
45 mfX /= fLen;
46 mfY /= fLen;
47 }
48 }
49 }
50
51 return *this;
52 }
53
54 double B2DVector::getLength() const
55 {
57 {
58 return fabs(mfY);
59 }
60 else if(fTools::equalZero(mfY))
61 {
62 return fabs(mfX);
63 }
64
65 return hypot( mfX, mfY );
66 }
67
68 double B2DVector::angle( const B2DVector& rVec ) const
69 {
70 return atan2(mfX * rVec.getY() - mfY * rVec.getX(),
71 mfX * rVec.getX() + mfY * rVec.getY());
72 }
73
75 {
76 return static_cast<const B2DVector&>( B2DTuple::getEmptyTuple() );
77 }
78
80 {
81 const double fTempX( rMat.get(0,0)*mfX +
82 rMat.get(0,1)*mfY );
83 const double fTempY( rMat.get(1,0)*mfX +
84 rMat.get(1,1)*mfY );
85 mfX = fTempX;
86 mfY = fTempY;
87
88 return *this;
89 }
90
92 {
93 double fLenNow(scalar(*this));
94
95 if(!fTools::equalZero(fLenNow))
96 {
97 const double fOne(1.0);
98
99 if(!fTools::equal(fOne, fLenNow))
100 {
101 fLen /= sqrt(fLenNow);
102 }
103
104 mfX *= fLen;
105 mfY *= fLen;
106 }
107
108 return *this;
109 }
110
111 bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB )
112 {
113 const double fValA(rVecA.getX() * rVecB.getY());
114 const double fValB(rVecA.getY() * rVecB.getX());
115
116 return fTools::equal(fValA, fValB);
117 }
118
120 {
121 double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
122
123 if(fTools::equalZero(fVal))
124 {
126 }
127
128 if(fVal > 0.0)
129 {
131 }
132 else
133 {
135 }
136 }
137
138 B2DVector getPerpendicular( const B2DVector& rNormalizedVec )
139 {
140 B2DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
141 return aPerpendicular;
142 }
143
145 {
146 B2DVector aPerpendicular(rVec);
147 aPerpendicular.normalize();
148 const double aTemp(-aPerpendicular.getY());
149 aPerpendicular.setY(aPerpendicular.getX());
150 aPerpendicular.setX(aTemp);
151 return aPerpendicular;
152 }
153
154 B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec )
155 {
156 B2DVector aRes( rVec );
157 aRes *= rMat;
158 return aRes;
159 }
160
161 B2VectorContinuity getContinuity(const B2DVector& rBackVector, const B2DVector& rForwardVector )
162 {
163 if(rBackVector.equalZero() || rForwardVector.equalZero())
164 {
166 }
167
168 if(fTools::equal(rBackVector.getX(), -rForwardVector.getX()) && fTools::equal(rBackVector.getY(), -rForwardVector.getY()))
169 {
170 // same direction and same length -> C2
172 }
173
174 if(areParallel(rBackVector, rForwardVector) && rBackVector.scalar(rForwardVector) < 0.0)
175 {
176 // parallel and opposite direction -> C1
178 }
179
181 }
182} // end of namespace basegfx
183
184/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
static BASEGFX_DLLPUBLIC const B2DTuple & getEmptyTuple()
Definition: b2dtuple.cxx:26
Base Point class with two double values.
Definition: b2dvector.hxx:40
double angle(const B2DVector &rVec) const
Calculate the Angle with another 2D Vector.
Definition: b2dvector.cxx:68
B2DVector & normalize()
Normalize this 2D Vector.
Definition: b2dvector.cxx:26
double scalar(const B2DVector &rVec) const
Calculate the Scalar with another 2D Vector.
Definition: b2dvector.hxx:134
B2DVector & operator*=(const B2DVector &rPnt)
*=operator to allow usage from B2DVector, too
Definition: b2dvector.hxx:81
B2DVector & setLength(double fLen)
Set the length of this 2D Vector.
Definition: b2dvector.cxx:91
double getLength() const
Calculate the length of this 2D Vector.
Definition: b2dvector.cxx:54
static const B2DVector & getEmptyVector()
Definition: b2dvector.cxx:74
bool equalZero() const
Definition: Tuple2D.hxx:95
TYPE getX() const
Get X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:63
void setY(TYPE fY)
Set Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:72
TYPE getY() const
Get Y-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:66
void setX(TYPE fX)
Set X-Coordinate of 2D Tuple.
Definition: Tuple2D.hxx:69
bool equalZero(const T &rfVal)
Compare against small value.
Definition: ftools.hxx:156
bool equal(T const &rfValA, T const &rfValB)
Definition: ftools.hxx:169
B2VectorContinuity
Descriptor for the mathematical continuity of two 2D Vectors.
Definition: b2enums.hxx:41
@ C1
mathematically negative oriented
@ C2
mathematically neutral, thus parallel
bool areParallel(const B2DVector &rVecA, const B2DVector &rVecB)
Test two vectors which need not to be normalized for parallelism.
Definition: b2dvector.cxx:111
B2VectorOrientation getOrientation(const B2DVector &rVecA, const B2DVector &rVecB)
Calculate the orientation to another 2D Vector.
Definition: b2dvector.cxx:119
B2VectorOrientation
Descriptor for the mathematical orientations of two 2D Vectors.
Definition: b2enums.hxx:27
@ Positive
mathematically positive oriented
@ Neutral
mathematically neutral, thus parallel
@ Negative
mathematically negative oriented
B2DVector getNormalizedPerpendicular(const B2DVector &rVec)
Calculate a perpendicular 2D Vector to the given one, normalize the given one as preparation.
Definition: b2dvector.cxx:144
B2VectorContinuity getContinuity(const B2DVector &rBackVector, const B2DVector &rForwardVector)
Test continuity between given vectors.
Definition: b2dvector.cxx:161
B2DVector getPerpendicular(const B2DVector &rNormalizedVec)
Calculate a perpendicular 2D Vector to the given one.
Definition: b2dvector.cxx:138
B2DPoint operator*(const ::basegfx::B2DHomMatrix &rMat, const B2DPoint &rPoint)
Definition: b2dpoint.cxx:43