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