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 atan2d (Real y, Real x)
52 return atan2 (y, x) * (180.0 / M_PI);
56 Offset::angle_degrees () const
58 Real x = coordinate_a_ [X_AXIS];
59 Real y = coordinate_a_ [Y_AXIS];
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.
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
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|
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|
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);
111 euclidian vector length / complex modulus
114 Offset::length () const
116 return hypot (coordinate_a_[X_AXIS], coordinate_a_[Y_AXIS]);
120 Offset::is_sane () const
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]);
129 Offset::direction () const
132 if (isinf (d[X_AXIS]))
134 if (!isinf (d[Y_AXIS]))
135 return Offset ((d[X_AXIS] > 0.0 ? 1.0 : -1.0), 0.0);
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)
141 // The other cases propagate or produce NaN as appropriate.
148 Offset::swapped () const
150 return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
154 offset_directed (Real angle)
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.
162 else if (angle > 180.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.
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.
176 return Offset (sin ((90 - angle) * M_PI/180.0),
177 sin ((180 - angle) * M_PI/180.0));
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));
185 return Offset (sin ((90 + angle) * M_PI/180.0),
186 sin (angle * M_PI/180.0));