]> git.donarmstrong.com Git - lilypond.git/blob - flower/offset.cc
Doc: Web: introduction - added href to OOoLilypond
[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 #ifndef STANDALONE
23 string
24 Offset::to_string () const
25 {
26   string s;
27   s = string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", "
28       + ::to_string (coordinate_a_[Y_AXIS]) + ")";
29   return s;
30 }
31 #endif
32
33 /*
34   free bsd fix by John Galbraith
35 */
36
37 Offset
38 complex_multiply (Offset z1, Offset z2)
39 {
40   Offset z;
41   if (!isinf (z2[Y_AXIS]))
42     {
43       z[X_AXIS] = z1[X_AXIS] * z2[X_AXIS] - z1[Y_AXIS] * z2[Y_AXIS];
44       z[Y_AXIS] = z1[X_AXIS] * z2[Y_AXIS] + z1[Y_AXIS] * z2[X_AXIS];
45     }
46   return z;
47 }
48
49 Offset
50 complex_conjugate (Offset o)
51 {
52   o[Y_AXIS] = -o[Y_AXIS];
53   return o;
54 }
55
56 Offset
57 complex_divide (Offset z1, Offset z2)
58 {
59   z2 = complex_conjugate (z2);
60   Offset z = complex_multiply (z1, z2);
61   z *= 1 / z2.length ();
62   return z;
63 }
64
65 Offset
66 complex_exp (Offset o)
67 {
68   Real s = sin (o[Y_AXIS]);
69   Real c = cos (o[Y_AXIS]);
70
71   Real r = exp (o[X_AXIS]);
72
73   return Offset (r * c, r * s);
74 }
75
76 Real
77 Offset::arg () const
78 {
79   return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
80 }
81
82 Real
83 Offset::angle_degrees () const
84 {
85   return arg () * 180 / M_PI;
86 }
87 /**
88    euclidian vector length / complex modulus
89 */
90 Real
91 Offset::length () const
92 {
93   return hypot (coordinate_a_[X_AXIS], coordinate_a_[Y_AXIS]);
94 }
95
96 bool
97 Offset::is_sane () const
98 {
99   return !isnan (coordinate_a_[X_AXIS])
100          && !isnan (coordinate_a_ [Y_AXIS])
101          && !isinf (coordinate_a_[X_AXIS])
102          && !isinf (coordinate_a_[Y_AXIS]);
103 }
104
105 Offset
106 Offset::direction () const
107 {
108   Offset d = *this;
109   if (isinf (d[X_AXIS]))
110     {
111       if (!isinf (d[Y_AXIS]))
112         return Offset ((d[X_AXIS] > 0.0 ? 1.0 : -1.0), 0.0);
113     }
114   else if (isinf (d[Y_AXIS]))
115     return Offset (0.0, (d[Y_AXIS] > 0.0 ? 1.0 : -1.0));
116   else if (d[X_AXIS] == 0.0 && d[Y_AXIS] == 0.0)
117     return d;
118   // The other cases propagate or produce NaN as appropriate.
119
120   d /= length ();
121   return d;
122 }
123
124 Offset
125 Offset::swapped () const
126 {
127   return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
128 }