]> git.donarmstrong.com Git - lilypond.git/blob - flower/offset.cc
Revert "Issue 4550 (2/2) Avoid "using namespace std;" in included files"
[lilypond.git] / flower / offset.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "offset.hh"
21
22 using std::string;
23
24 #ifndef STANDALONE
25 string
26 Offset::to_string () const
27 {
28   string s;
29   s = string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", "
30       + ::to_string (coordinate_a_[Y_AXIS]) + ")";
31   return s;
32 }
33 #endif
34
35 /*
36   free bsd fix by John Galbraith
37 */
38
39 Offset
40 complex_multiply (Offset z1, Offset z2)
41 {
42   Offset z;
43   if (!isinf (z2[Y_AXIS]))
44     {
45       z[X_AXIS] = z1[X_AXIS] * z2[X_AXIS] - z1[Y_AXIS] * z2[Y_AXIS];
46       z[Y_AXIS] = z1[X_AXIS] * z2[Y_AXIS] + z1[Y_AXIS] * z2[X_AXIS];
47     }
48   return z;
49 }
50
51 Offset
52 complex_conjugate (Offset o)
53 {
54   o[Y_AXIS] = -o[Y_AXIS];
55   return o;
56 }
57
58 Offset
59 complex_divide (Offset z1, Offset z2)
60 {
61   z2 = complex_conjugate (z2);
62   Offset z = complex_multiply (z1, z2);
63   z *= 1 / z2.length ();
64   return z;
65 }
66
67 Offset
68 complex_exp (Offset o)
69 {
70   Real s = sin (o[Y_AXIS]);
71   Real c = cos (o[Y_AXIS]);
72
73   Real r = exp (o[X_AXIS]);
74
75   return Offset (r * c, r * s);
76 }
77
78 Real
79 Offset::arg () const
80 {
81   return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
82 }
83
84 Real
85 Offset::angle_degrees () const
86 {
87   return arg () * 180 / M_PI;
88 }
89 /**
90    euclidian vector length / complex modulus
91 */
92 Real
93 Offset::length () const
94 {
95   return hypot (coordinate_a_[X_AXIS], coordinate_a_[Y_AXIS]);
96 }
97
98 bool
99 Offset::is_sane () const
100 {
101   return !isnan (coordinate_a_[X_AXIS])
102          && !isnan (coordinate_a_ [Y_AXIS])
103          && !isinf (coordinate_a_[X_AXIS])
104          && !isinf (coordinate_a_[Y_AXIS]);
105 }
106
107 Offset
108 Offset::direction () const
109 {
110   Offset d = *this;
111   if (isinf (d[X_AXIS]))
112     {
113       if (!isinf (d[Y_AXIS]))
114         return Offset ((d[X_AXIS] > 0.0 ? 1.0 : -1.0), 0.0);
115     }
116   else if (isinf (d[Y_AXIS]))
117     return Offset (0.0, (d[Y_AXIS] > 0.0 ? 1.0 : -1.0));
118   else if (d[X_AXIS] == 0.0 && d[Y_AXIS] == 0.0)
119     return d;
120   // The other cases propagate or produce NaN as appropriate.
121
122   d /= length ();
123   return d;
124 }
125
126 Offset
127 Offset::swapped () const
128 {
129   return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
130 }