]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/offset.hh
Run `make grand-replace'.
[lilypond.git] / flower / include / offset.hh
1 /*
2   offset.hh -- part of GNU LilyPond
3
4   (c) 1996--2008 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   Offset swapped () const;
101   
102   Real arg () const;
103   Real angle_degrees () const;
104   Real length () const;
105   bool is_sane () const;
106   Offset operator *= (Offset z2);
107 };
108
109 #include "arithmetic-operator.hh"
110 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, +);
111 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, -);
112 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
113
114
115
116 Offset complex_multiply (Offset, Offset);
117 Offset complex_divide (Offset, Offset);
118 Offset complex_exp (Offset);
119
120 inline Offset
121 Offset::operator *= (Offset z2)
122 {
123   *this = complex_multiply (*this, z2);
124   return *this;
125 }
126
127 inline Offset
128 operator * (Real o1, Offset o2)
129 {
130   o2 *= o1;
131   return o2;
132 }
133
134 inline Offset
135 operator / (Offset o1, Real a)
136 {
137   o1 /= a;
138   return o1;
139 }
140
141 inline Offset
142 operator * (Offset o1, Real o2)
143 {
144   o1 *= o2;
145   return o1;
146 }
147
148 inline Offset
149 mirror (Offset o, Axis a)
150 {
151   o.mirror (a);
152   return o;
153 }
154
155 inline
156 Real
157 dot_product (Offset o1, Offset o2)
158 {
159   return o1[X_AXIS] * o2[X_AXIS] + o1[Y_AXIS] * o2[Y_AXIS];
160 }
161
162 #endif /* OFFSET_HH */
163