]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/offset.hh
Run grand-replace for 2010.
[lilypond.git] / flower / include / offset.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2010 Han-Wen Nienhuys
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef OFFSET_HH
21 #define OFFSET_HH
22
23 #include "axis.hh"
24 #include "std-string.hh"
25 #include "real.hh"
26
27
28 /*
29   This is a mixture a 2D vector. Sometimes it can
30   also be convenient to think of 2D vectors as complex numbers
31   (ie. x + i y). The naming of some methods reflects that.
32 */
33 class Offset
34 {
35 public:
36   Real coordinate_a_[NO_AXES];
37
38   Real &operator [] (Axis i)
39   {
40     return coordinate_a_[i];
41   }
42
43   Real operator [] (Axis i) const
44   {
45     return coordinate_a_[i];
46   }
47
48   Offset &operator += (Offset o)
49   {
50     (*this)[X_AXIS] += o[X_AXIS];
51     (*this)[Y_AXIS] += o[Y_AXIS];
52     return *this;
53   }
54
55   Offset operator - () const
56   {
57     Offset o = *this;
58
59     o[X_AXIS] = -o[X_AXIS];
60     o[Y_AXIS] = -o[Y_AXIS];
61     return o;
62   }
63
64   Offset &operator -= (Offset o)
65   {
66     (*this)[X_AXIS] -= o[X_AXIS];
67     (*this)[Y_AXIS] -= o[Y_AXIS];
68
69     return *this;
70   }
71
72   Offset &scale (Offset o)
73   {
74     (*this)[X_AXIS] *= o[X_AXIS];
75     (*this)[Y_AXIS] *= o[Y_AXIS];
76
77     return *this;
78   }
79
80   Offset &operator /= (Real a)
81   {
82     (*this) *= 1/a;
83     return *this;
84   }
85
86   Offset &operator *= (Real a)
87   {
88     (*this)[X_AXIS] *= a;
89     (*this)[Y_AXIS] *= a;
90
91     return *this;
92   }
93
94   Offset (Real ix, Real iy)
95   {
96     coordinate_a_[X_AXIS] = ix;
97     coordinate_a_[Y_AXIS] = iy;
98   }
99
100   Offset ()
101   {
102     coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0;
103   }
104
105   string to_string () const;
106
107   Offset &mirror (Axis a)
108   {
109     coordinate_a_[a] = -coordinate_a_[a];
110     return *this;
111   }
112   Offset direction () const;
113   Offset swapped () const;
114   
115   Real arg () const;
116   Real angle_degrees () const;
117   Real length () const;
118   bool is_sane () const;
119   Offset operator *= (Offset z2);
120 };
121
122 #include "arithmetic-operator.hh"
123 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, +);
124 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, -);
125 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
126
127
128
129 Offset complex_multiply (Offset, Offset);
130 Offset complex_divide (Offset, Offset);
131 Offset complex_exp (Offset);
132
133 inline Offset
134 Offset::operator *= (Offset z2)
135 {
136   *this = complex_multiply (*this, z2);
137   return *this;
138 }
139
140 inline Offset
141 operator * (Real o1, Offset o2)
142 {
143   o2 *= o1;
144   return o2;
145 }
146
147 inline Offset
148 operator / (Offset o1, Real a)
149 {
150   o1 /= a;
151   return o1;
152 }
153
154 inline Offset
155 operator * (Offset o1, Real o2)
156 {
157   o1 *= o2;
158   return o1;
159 }
160
161 inline Offset
162 mirror (Offset o, Axis a)
163 {
164   o.mirror (a);
165   return o;
166 }
167
168 inline
169 Real
170 dot_product (Offset o1, Offset o2)
171 {
172   return o1[X_AXIS] * o2[X_AXIS] + o1[Y_AXIS] * o2[Y_AXIS];
173 }
174
175 #endif /* OFFSET_HH */
176