summaryrefslogtreecommitdiff
path: root/flower
diff options
context:
space:
mode:
authorDavid Kastrup <dak@gnu.org>2016-08-29 01:13:15 +0200
committerDavid Kastrup <dak@gnu.org>2016-09-05 19:07:09 +0200
commit1cab9ca99b60c249f4f813746e91070dbd745a4f (patch)
treeb5ff9f83ba06c26ef07e4fcc4d1edc059ab138ca /flower
parent929f0b6293823fa87153a50501fbe609afebcfa2 (diff)
Issue 4961/2: Add offset_directed (Real)
This converts an angle in degrees into a unit vector.
Diffstat (limited to 'flower')
-rw-r--r--flower/include/offset.hh1
-rw-r--r--flower/offset.cc36
2 files changed, 37 insertions, 0 deletions
diff --git a/flower/include/offset.hh b/flower/include/offset.hh
index 8f395398b7..c6c166cc42 100644
--- a/flower/include/offset.hh
+++ b/flower/include/offset.hh
@@ -126,6 +126,7 @@ IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
Offset complex_multiply (Offset, Offset);
Offset complex_divide (Offset, Offset);
Offset complex_exp (Offset);
+Offset offset_directed (Real);
inline Offset
Offset::operator *= (Offset z2)
diff --git a/flower/offset.cc b/flower/offset.cc
index b7f5df4026..cdb63f3f86 100644
--- a/flower/offset.cc
+++ b/flower/offset.cc
@@ -182,3 +182,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));
+}