]> git.donarmstrong.com Git - lilypond.git/blob - flower/offset.cc
Web-ja: update introduction
[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 static inline Real
50 atan2d (Real y, Real x)
51 {
52   return atan2 (y, x) * (180.0 / M_PI);
53 }
54
55 Real
56 Offset::angle_degrees () const
57 {
58   Real x = coordinate_a_ [X_AXIS];
59   Real y = coordinate_a_ [Y_AXIS];
60
61   // We keep in the vicinity of multiples of 45 degrees here: this is
62   // where straightforward angles for straightforward angular
63   // relations are most expected.  The factors of 2 employed in the
64   // comparison are not really perfect for that: sqrt(2)+1 would be
65   // the factor giving exact windows of 45 degrees rather than what we
66   // have here.  It's just that 2 is likely to generate nicer code
67   // than 2.4 and the exact handover does not really matter.
68   //
69   // Comparisons here are chosen appropriately to let infinities end
70   // up in their "exact" branch.  As opposed to the normal atan2
71   // function behavior, this makes "competing" infinities result in
72   // NAN angles.
73   if (y < 0.0)
74     {
75       if (2*x < -y)
76         if (-x > -2*y)          // x < 0, y < 0, |x| > |2y|
77           return -180 + atan2d (-y, -x);
78         else if (-2*x >= -y)    // x < 0, y < 0, |y| < |2x| <= |4y|
79           return -135 + atan2d (x - y, -y - x);
80         else                    // y < 0, |y| >= |2x|
81           return -90 + atan2d (x, -y);
82       else if (x <= -2*y)       // x > 0, y < 0, |y| <= |2x| < |4y|
83         return -45 + atan2d (x + y, x - y);
84       // Drop through for y < 0, x > |2y|
85     }
86   else if (y > 0.0)
87     {
88       if (2*x < y)
89         if (-x > 2*y)           // x < 0, y >= 0, |x| > |2y|
90           return 180 - atan2d (y, -x);
91         else if (-2*x >= y)     // x < 0, y >= 0, |y| < |2x| <= |4y|
92           return 135 - atan2d (x + y, y - x);
93         else                    // y >= 0, |y| >= |2x|
94           return 90 - atan2d (x, y);
95       else if (x <= 2*y)        // x >= 0, y >= 0, |y| < |2x| < |4y|
96         return 45 - atan2d (x - y, x + y);
97       // Drop through for y > 0, x > |2y|
98     }
99   else
100     // we return 0 for (0,0).  NAN would be an option but is a
101     // nuisance for getting back to rectangular coordinates.  Strictly
102     // speaking, this argument would be just as valid for (+inf.0,
103     // +inf.0), but then infinities are already an indication of a
104     // problem in LilyPond.
105     return (x < 0.0) ? 180 : 0;
106   return atan2d (y, x);
107 }
108
109
110 /**
111    euclidian vector length / complex modulus
112 */
113 Real
114 Offset::length () const
115 {
116   return hypot (coordinate_a_[X_AXIS], coordinate_a_[Y_AXIS]);
117 }
118
119 bool
120 Offset::is_sane () const
121 {
122   return !isnan (coordinate_a_[X_AXIS])
123          && !isnan (coordinate_a_ [Y_AXIS])
124          && !isinf (coordinate_a_[X_AXIS])
125          && !isinf (coordinate_a_[Y_AXIS]);
126 }
127
128 Offset
129 Offset::direction () const
130 {
131   Offset d = *this;
132   if (isinf (d[X_AXIS]))
133     {
134       if (!isinf (d[Y_AXIS]))
135         return Offset ((d[X_AXIS] > 0.0 ? 1.0 : -1.0), 0.0);
136     }
137   else if (isinf (d[Y_AXIS]))
138     return Offset (0.0, (d[Y_AXIS] > 0.0 ? 1.0 : -1.0));
139   else if (d[X_AXIS] == 0.0 && d[Y_AXIS] == 0.0)
140     return d;
141   // The other cases propagate or produce NaN as appropriate.
142
143   d /= length ();
144   return d;
145 }
146
147 Offset
148 Offset::swapped () const
149 {
150   return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
151 }
152
153 Offset
154 offset_directed (Real angle)
155 {
156   if (angle <= -360.0 || angle >= 360.0)
157     angle = fmod (angle, 360.0);
158   // Now |angle| < 360.0, and the absolute size is not larger than
159   // before, so we haven't lost precision.
160   if (angle <= -180.0)
161     angle += 360.0;
162   else if (angle > 180.0)
163     angle -= 360.0;
164   // Now -180.0 < angle <= 180.0 and we still haven't lost precision.
165   // We don't work with angles greater than 45 degrees absolute in
166   // order to minimize how rounding errors of M_PI/180 affect the
167   // result.  That way, at least angles that are a multiple of 90
168   // degree deliver the expected results.
169   //
170   // Sign of the sine is chosen to avoid -0.0 in results.  This
171   // version delivers exactly equal magnitude on x/y for odd multiples
172   // of 45 degrees at the cost of losing some less obvious invariants.
173
174   if (angle > 0)
175     if (angle > 90)
176       return Offset (sin ((90 - angle) * M_PI/180.0),
177                      sin ((180 - angle) * M_PI/180.0));
178     else
179       return Offset (sin ((90 - angle) * M_PI/180.0),
180                      sin (angle * M_PI/180.0));
181   else if (angle < -90)
182     return Offset (sin ((90 + angle) * M_PI/180.0),
183                    sin ((-180 - angle) * M_PI/180.0));
184   else
185     return Offset (sin ((90 + angle) * M_PI/180.0),
186                    sin (angle * M_PI/180.0));
187 }