]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/offset.cc
Release: bump Welcome versions.
[lilypond.git] / flower / offset.cc
index b7f5df40262c7b67e1fbea794142401c558eba7a..db1654f2d3d517f0ef29b90a3d8750353f0a89d5 100644 (file)
@@ -46,39 +46,6 @@ complex_multiply (Offset z1, Offset z2)
   return z;
 }
 
-Offset
-complex_conjugate (Offset o)
-{
-  o[Y_AXIS] = -o[Y_AXIS];
-  return o;
-}
-
-Offset
-complex_divide (Offset z1, Offset z2)
-{
-  z2 = complex_conjugate (z2);
-  Offset z = complex_multiply (z1, z2);
-  z *= 1 / z2.length ();
-  return z;
-}
-
-Offset
-complex_exp (Offset o)
-{
-  Real s = sin (o[Y_AXIS]);
-  Real c = cos (o[Y_AXIS]);
-
-  Real r = exp (o[X_AXIS]);
-
-  return Offset (r * c, r * s);
-}
-
-Real
-Offset::arg () const
-{
-  return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
-}
-
 static inline Real
 atan2d (Real y, Real x)
 {
@@ -182,3 +149,39 @@ Offset::swapped () const
 {
   return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
 }
+
+Offset
+offset_directed (Real angle)
+{
+  if (angle <= -360.0 || angle >= 360.0)
+    angle = fmod (angle, 360.0);
+  // Now |angle| < 360.0, and the absolute size is not larger than
+  // before, so we haven't lost precision.
+  if (angle <= -180.0)
+    angle += 360.0;
+  else if (angle > 180.0)
+    angle -= 360.0;
+  // Now -180.0 < angle <= 180.0 and we still haven't lost precision.
+  // We don't work with angles greater than 45 degrees absolute in
+  // order to minimize how rounding errors of M_PI/180 affect the
+  // result.  That way, at least angles that are a multiple of 90
+  // degree deliver the expected results.
+  //
+  // Sign of the sine is chosen to avoid -0.0 in results.  This
+  // version delivers exactly equal magnitude on x/y for odd multiples
+  // of 45 degrees at the cost of losing some less obvious invariants.
+
+  if (angle > 0)
+    if (angle > 90)
+      return Offset (sin ((90 - angle) * M_PI/180.0),
+                     sin ((180 - angle) * M_PI/180.0));
+    else
+      return Offset (sin ((90 - angle) * M_PI/180.0),
+                     sin (angle * M_PI/180.0));
+  else if (angle < -90)
+    return Offset (sin ((90 + angle) * M_PI/180.0),
+                   sin ((-180 - angle) * M_PI/180.0));
+  else
+    return Offset (sin ((90 + angle) * M_PI/180.0),
+                   sin (angle * M_PI/180.0));
+}