]> git.donarmstrong.com Git - lilypond.git/blob - flower/offset.cc
* flower/include/std-string.hh:
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "offset.hh"
10
11
12 #ifndef STANDALONE
13 string
14 Offset::to_string () const
15 {
16   string s;
17   s = string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", "
18     + ::to_string (coordinate_a_[Y_AXIS]) + ")";
19   return s;
20 }
21 #endif
22
23 bool
24 isinf_b (Real r)
25 {
26   return (fabs (r) > 1e20);
27 }
28
29 /*
30   free bsd fix by John Galbraith
31 */
32
33 Offset
34 complex_multiply (Offset z1, Offset z2)
35 {
36   Offset z;
37   if (!isinf_b (z2[Y_AXIS]))
38     {
39       z[X_AXIS] = z1[X_AXIS] * z2[X_AXIS] - z1[Y_AXIS] * z2[Y_AXIS];
40       z[Y_AXIS] = z1[X_AXIS] * z2[Y_AXIS] + z1[Y_AXIS] * z2[X_AXIS];
41     }
42   return z;
43 }
44
45 Offset
46 complex_conjugate (Offset o)
47 {
48   o[Y_AXIS] = -o[Y_AXIS];
49   return o;
50 }
51
52 Offset
53 complex_divide (Offset z1, Offset z2)
54 {
55   z2 = complex_conjugate (z2);
56   Offset z = complex_multiply (z1, z2);
57   z *= 1 / z2.length ();
58   return z;
59 }
60
61 Offset
62 complex_exp (Offset o)
63 {
64   Real s = sin (o[Y_AXIS]);
65   Real c = cos (o[Y_AXIS]);
66
67   Real r = exp (o[X_AXIS]);
68
69   return Offset (r * c, r * s);
70 }
71
72 Real
73 Offset::arg () const
74 {
75   return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
76 }
77
78 /**
79    euclidian vector length / complex modulus
80 */
81 Real
82 Offset::length () const
83 {
84   return sqrt (sqr (coordinate_a_[X_AXIS])
85                     + sqr (coordinate_a_[Y_AXIS]));
86 }
87
88 bool
89 Offset::is_sane () const
90 {
91   return !isnan (coordinate_a_[X_AXIS])
92     && !isnan (coordinate_a_ [Y_AXIS])
93     && !isinf (coordinate_a_[X_AXIS]) 
94     && !isnan (coordinate_a_[Y_AXIS]);
95 }