Fix InstrumentSwitch grob definition.
[lilypond.git] / flower / include / offset.hh
blob80053a51b6546f71a0172d86580fcb475dd78e4c
1 /*
2 offset.hh -- part of GNU LilyPond
4 (c) 1996--2007 Han-Wen Nienhuys
5 */
7 #ifndef OFFSET_HH
8 #define OFFSET_HH
10 #include "axis.hh"
11 #include "std-string.hh"
12 #include "real.hh"
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.
20 class Offset
22 public:
23 Real coordinate_a_[NO_AXES];
25 Real &operator [] (Axis i)
27 return coordinate_a_[i];
30 Real operator [] (Axis i) const
32 return coordinate_a_[i];
35 Offset &operator += (Offset o)
37 (*this)[X_AXIS] += o[X_AXIS];
38 (*this)[Y_AXIS] += o[Y_AXIS];
39 return *this;
42 Offset operator - () const
44 Offset o = *this;
46 o[X_AXIS] = -o[X_AXIS];
47 o[Y_AXIS] = -o[Y_AXIS];
48 return o;
51 Offset &operator -= (Offset o)
53 (*this)[X_AXIS] -= o[X_AXIS];
54 (*this)[Y_AXIS] -= o[Y_AXIS];
56 return *this;
59 Offset &scale (Offset o)
61 (*this)[X_AXIS] *= o[X_AXIS];
62 (*this)[Y_AXIS] *= o[Y_AXIS];
64 return *this;
67 Offset &operator /= (Real a)
69 (*this) *= 1/a;
70 return *this;
73 Offset &operator *= (Real a)
75 (*this)[X_AXIS] *= a;
76 (*this)[Y_AXIS] *= a;
78 return *this;
81 Offset (Real ix, Real iy)
83 coordinate_a_[X_AXIS] = ix;
84 coordinate_a_[Y_AXIS] = iy;
87 Offset ()
89 coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0;
92 string to_string () const;
94 Offset &mirror (Axis a)
96 coordinate_a_[a] = -coordinate_a_[a];
97 return *this;
99 Offset direction () const;
100 Offset swapped () const;
102 Real arg () const;
103 Real angle_degrees () const;
104 Real length () const;
105 bool is_sane () const;
106 Offset operator *= (Offset z2);
109 #include "arithmetic-operator.hh"
110 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, +);
111 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, -);
112 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
116 Offset complex_multiply (Offset, Offset);
117 Offset complex_divide (Offset, Offset);
118 Offset complex_exp (Offset);
120 inline Offset
121 Offset::operator *= (Offset z2)
123 *this = complex_multiply (*this, z2);
124 return *this;
127 inline Offset
128 operator * (Real o1, Offset o2)
130 o2 *= o1;
131 return o2;
134 inline Offset
135 operator / (Offset o1, Real a)
137 o1 /= a;
138 return o1;
141 inline Offset
142 operator * (Offset o1, Real o2)
144 o1 *= o2;
145 return o1;
148 inline Offset
149 mirror (Offset o, Axis a)
151 o.mirror (a);
152 return o;
155 inline
156 Real
157 dot_product (Offset o1, Offset o2)
159 return o1[X_AXIS] * o2[X_AXIS] + o1[Y_AXIS] * o2[Y_AXIS];
162 #endif /* OFFSET_HH */