]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/offset.hh
use _degrees to signify functions taking degree angles.
[lilypond.git] / flower / include / offset.hh
1 /*
2   offset.hh -- part of GNU LilyPond
3
4   (c) 1996--2007 Han-Wen Nienhuys
5 */
6
7 #ifndef OFFSET_HH
8 #define OFFSET_HH
9
10 #include "axis.hh"
11 #include "std-string.hh"
12 #include "real.hh"
13
14
15 /*
16   This is a mixture a 2D vector. Sometimes it can
17   also be convenient to think of 2D vectors as complex numbers
18   (ie. x + i y). The naming of some methods reflects that.
19 */
20 class Offset
21 {
22 public:
23   Real coordinate_a_[NO_AXES];
24
25   Real &operator [] (Axis i)
26   {
27     return coordinate_a_[i];
28   }
29
30   Real operator [] (Axis i) const
31   {
32     return coordinate_a_[i];
33   }
34
35   Offset &operator += (Offset o)
36   {
37     (*this)[X_AXIS] += o[X_AXIS];
38     (*this)[Y_AXIS] += o[Y_AXIS];
39     return *this;
40   }
41
42   Offset operator - () const
43   {
44     Offset o = *this;
45
46     o[X_AXIS] = -o[X_AXIS];
47     o[Y_AXIS] = -o[Y_AXIS];
48     return o;
49   }
50
51   Offset &operator -= (Offset o)
52   {
53     (*this)[X_AXIS] -= o[X_AXIS];
54     (*this)[Y_AXIS] -= o[Y_AXIS];
55
56     return *this;
57   }
58
59   Offset &scale (Offset o)
60   {
61     (*this)[X_AXIS] *= o[X_AXIS];
62     (*this)[Y_AXIS] *= o[Y_AXIS];
63
64     return *this;
65   }
66
67   Offset &operator /= (Real a)
68   {
69     (*this) *= 1/a;
70     return *this;
71   }
72
73   Offset &operator *= (Real a)
74   {
75     (*this)[X_AXIS] *= a;
76     (*this)[Y_AXIS] *= a;
77
78     return *this;
79   }
80
81   Offset (Real ix, Real iy)
82   {
83     coordinate_a_[X_AXIS] = ix;
84     coordinate_a_[Y_AXIS] = iy;
85   }
86
87   Offset ()
88   {
89     coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0;
90   }
91
92   string to_string () const;
93
94   Offset &mirror (Axis a)
95   {
96     coordinate_a_[a] = -coordinate_a_[a];
97     return *this;
98   }
99   Offset direction () const;
100   
101   Real arg () const;
102   Real angle_degrees () const;
103   Real length () const;
104   bool is_sane () const;
105   Offset operator *= (Offset z2);
106 };
107
108 #include "arithmetic-operator.hh"
109 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, +);
110 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, -);
111 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
112
113
114
115 Offset complex_multiply (Offset, Offset);
116 Offset complex_divide (Offset, Offset);
117 Offset complex_exp (Offset);
118
119 inline Offset
120 Offset::operator *= (Offset z2)
121 {
122   *this = complex_multiply (*this, z2);
123   return *this;
124 }
125
126 inline Offset
127 operator * (Real o1, Offset o2)
128 {
129   o2 *= o1;
130   return o2;
131 }
132
133 inline Offset
134 operator / (Offset o1, Real a)
135 {
136   o1 /= a;
137   return o1;
138 }
139
140 inline Offset
141 operator * (Offset o1, Real o2)
142 {
143   o1 *= o2;
144   return o1;
145 }
146
147 inline Offset
148 mirror (Offset o, Axis a)
149 {
150   o.mirror (a);
151   return o;
152 }
153
154 inline
155 Real
156 dot_product (Offset o1, Offset o2)
157 {
158   return o1[X_AXIS] * o2[X_AXIS] + o1[Y_AXIS] * o2[Y_AXIS];
159 }
160
161 #endif /* OFFSET_HH */
162