]> git.donarmstrong.com Git - lilypond.git/blob - flower/offset.cc
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / flower / offset.cc
1 /*
2   offset.cc -- implement Offset
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "offset.hh"
10
11 #ifndef STANDALONE
12 string
13 Offset::to_string () const
14 {
15   string s;
16   s = string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", "
17     + ::to_string (coordinate_a_[Y_AXIS]) + ")";
18   return s;
19 }
20 #endif
21
22 bool
23 isinf_b (Real r)
24 {
25   return (fabs (r) > 1e20);
26 }
27
28 /*
29   free bsd fix by John Galbraith
30 */
31
32 Offset
33 complex_multiply (Offset z1, Offset z2)
34 {
35   Offset z;
36   if (!isinf_b (z2[Y_AXIS]))
37     {
38       z[X_AXIS] = z1[X_AXIS] * z2[X_AXIS] - z1[Y_AXIS] * z2[Y_AXIS];
39       z[Y_AXIS] = z1[X_AXIS] * z2[Y_AXIS] + z1[Y_AXIS] * z2[X_AXIS];
40     }
41   return z;
42 }
43
44 Offset
45 complex_conjugate (Offset o)
46 {
47   o[Y_AXIS] = -o[Y_AXIS];
48   return o;
49 }
50
51 Offset
52 complex_divide (Offset z1, Offset z2)
53 {
54   z2 = complex_conjugate (z2);
55   Offset z = complex_multiply (z1, z2);
56   z *= 1 / z2.length ();
57   return z;
58 }
59
60 Offset
61 complex_exp (Offset o)
62 {
63   Real s = sin (o[Y_AXIS]);
64   Real c = cos (o[Y_AXIS]);
65
66   Real r = exp (o[X_AXIS]);
67
68   return Offset (r * c, r * s);
69 }
70
71 Real
72 Offset::arg () const
73 {
74   return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
75 }
76
77 Real
78 Offset::angle_degrees () const
79 {
80   return arg () * 180 / M_PI;
81 }
82 /**
83    euclidian vector length / complex modulus
84 */
85 Real
86 Offset::length () const
87 {
88   return sqrt (sqr (coordinate_a_[X_AXIS])
89                     + sqr (coordinate_a_[Y_AXIS]));
90 }
91
92 bool
93 Offset::is_sane () const
94 {
95   return !isnan (coordinate_a_[X_AXIS])
96     && !isnan (coordinate_a_ [Y_AXIS])
97     && !isinf (coordinate_a_[X_AXIS]) 
98     && !isnan (coordinate_a_[Y_AXIS]);
99 }
100
101 Offset
102 Offset::direction () const
103 {
104   Offset d = *this;
105   d /= length (); 
106   return d;
107 }