2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
24 Offset::to_string () const
27 s = string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", "
28 + ::to_string (coordinate_a_[Y_AXIS]) + ")";
34 free bsd fix by John Galbraith
38 complex_multiply (Offset z1, Offset z2)
41 if (!isinf (z2[Y_AXIS]))
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];
50 complex_conjugate (Offset o)
52 o[Y_AXIS] = -o[Y_AXIS];
57 complex_divide (Offset z1, Offset z2)
59 z2 = complex_conjugate (z2);
60 Offset z = complex_multiply (z1, z2);
61 z *= 1 / z2.length ();
66 complex_exp (Offset o)
68 Real s = sin (o[Y_AXIS]);
69 Real c = cos (o[Y_AXIS]);
71 Real r = exp (o[X_AXIS]);
73 return Offset (r * c, r * s);
79 return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
83 atan2d (Real y, Real x)
85 return atan2 (y, x) * (180.0 / M_PI);
89 Offset::angle_degrees () const
91 Real x = coordinate_a_ [X_AXIS];
92 Real y = coordinate_a_ [Y_AXIS];
94 // We keep in the vicinity of multiples of 45 degrees here: this is
95 // where straightforward angles for straightforward angular
96 // relations are most expected. The factors of 2 employed in the
97 // comparison are not really perfect for that: sqrt(2)+1 would be
98 // the factor giving exact windows of 45 degrees rather than what we
99 // have here. It's just that 2 is likely to generate nicer code
100 // than 2.4 and the exact handover does not really matter.
102 // Comparisons here are chosen appropriately to let infinities end
103 // up in their "exact" branch. As opposed to the normal atan2
104 // function behavior, this makes "competing" infinities result in
109 if (-x > -2*y) // x < 0, y < 0, |x| > |2y|
110 return -180 + atan2d (-y, -x);
111 else if (-2*x >= -y) // x < 0, y < 0, |y| < |2x| <= |4y|
112 return -135 + atan2d (x - y, -y - x);
113 else // y < 0, |y| >= |2x|
114 return -90 + atan2d (x, -y);
115 else if (x <= -2*y) // x > 0, y < 0, |y| <= |2x| < |4y|
116 return -45 + atan2d (x + y, x - y);
117 // Drop through for y < 0, x > |2y|
122 if (-x > 2*y) // x < 0, y >= 0, |x| > |2y|
123 return 180 - atan2d (y, -x);
124 else if (-2*x >= y) // x < 0, y >= 0, |y| < |2x| <= |4y|
125 return 135 - atan2d (x + y, y - x);
126 else // y >= 0, |y| >= |2x|
127 return 90 - atan2d (x, y);
128 else if (x <= 2*y) // x >= 0, y >= 0, |y| < |2x| < |4y|
129 return 45 - atan2d (x - y, x + y);
130 // Drop through for y > 0, x > |2y|
133 // we return 0 for (0,0). NAN would be an option but is a
134 // nuisance for getting back to rectangular coordinates. Strictly
135 // speaking, this argument would be just as valid for (+inf.0,
136 // +inf.0), but then infinities are already an indication of a
137 // problem in LilyPond.
138 return (x < 0.0) ? 180 : 0;
139 return atan2d (y, x);
144 euclidian vector length / complex modulus
147 Offset::length () const
149 return hypot (coordinate_a_[X_AXIS], coordinate_a_[Y_AXIS]);
153 Offset::is_sane () const
155 return !isnan (coordinate_a_[X_AXIS])
156 && !isnan (coordinate_a_ [Y_AXIS])
157 && !isinf (coordinate_a_[X_AXIS])
158 && !isinf (coordinate_a_[Y_AXIS]);
162 Offset::direction () const
165 if (isinf (d[X_AXIS]))
167 if (!isinf (d[Y_AXIS]))
168 return Offset ((d[X_AXIS] > 0.0 ? 1.0 : -1.0), 0.0);
170 else if (isinf (d[Y_AXIS]))
171 return Offset (0.0, (d[Y_AXIS] > 0.0 ? 1.0 : -1.0));
172 else if (d[X_AXIS] == 0.0 && d[Y_AXIS] == 0.0)
174 // The other cases propagate or produce NaN as appropriate.
181 Offset::swapped () const
183 return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
187 offset_directed (Real angle)
189 if (angle <= -360.0 || angle >= 360.0)
190 angle = fmod (angle, 360.0);
191 // Now |angle| < 360.0, and the absolute size is not larger than
192 // before, so we haven't lost precision.
195 else if (angle > 180.0)
197 // Now -180.0 < angle <= 180.0 and we still haven't lost precision.
198 // We don't work with angles greater than 45 degrees absolute in
199 // order to minimize how rounding errors of M_PI/180 affect the
200 // result. That way, at least angles that are a multiple of 90
201 // degree deliver the expected results.
203 // Sign of the sine is chosen to avoid -0.0 in results. This
204 // version delivers exactly equal magnitude on x/y for odd multiples
205 // of 45 degrees at the cost of losing some less obvious invariants.
209 return Offset (sin ((90 - angle) * M_PI/180.0),
210 sin ((180 - angle) * M_PI/180.0));
212 return Offset (sin ((90 - angle) * M_PI/180.0),
213 sin (angle * M_PI/180.0));
214 else if (angle < -90)
215 return Offset (sin ((90 + angle) * M_PI/180.0),
216 sin ((-180 - angle) * M_PI/180.0));
218 return Offset (sin ((90 + angle) * M_PI/180.0),
219 sin (angle * M_PI/180.0));