Doc: CG intro -- mention lily-git.
[lilypond/mpolesky.git] / flower / include / offset.hh
blob3dbab14a927e5a404c6839f6b8046127a5a0a682
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2009 Han-Wen Nienhuys
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.
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.
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/>.
20 #ifndef OFFSET_HH
21 #define OFFSET_HH
23 #include "axis.hh"
24 #include "std-string.hh"
25 #include "real.hh"
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.
33 class Offset
35 public:
36 Real coordinate_a_[NO_AXES];
38 Real &operator [] (Axis i)
40 return coordinate_a_[i];
43 Real operator [] (Axis i) const
45 return coordinate_a_[i];
48 Offset &operator += (Offset o)
50 (*this)[X_AXIS] += o[X_AXIS];
51 (*this)[Y_AXIS] += o[Y_AXIS];
52 return *this;
55 Offset operator - () const
57 Offset o = *this;
59 o[X_AXIS] = -o[X_AXIS];
60 o[Y_AXIS] = -o[Y_AXIS];
61 return o;
64 Offset &operator -= (Offset o)
66 (*this)[X_AXIS] -= o[X_AXIS];
67 (*this)[Y_AXIS] -= o[Y_AXIS];
69 return *this;
72 Offset &scale (Offset o)
74 (*this)[X_AXIS] *= o[X_AXIS];
75 (*this)[Y_AXIS] *= o[Y_AXIS];
77 return *this;
80 Offset &operator /= (Real a)
82 (*this) *= 1/a;
83 return *this;
86 Offset &operator *= (Real a)
88 (*this)[X_AXIS] *= a;
89 (*this)[Y_AXIS] *= a;
91 return *this;
94 Offset (Real ix, Real iy)
96 coordinate_a_[X_AXIS] = ix;
97 coordinate_a_[Y_AXIS] = iy;
100 Offset ()
102 coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0;
105 string to_string () const;
107 Offset &mirror (Axis a)
109 coordinate_a_[a] = -coordinate_a_[a];
110 return *this;
112 Offset direction () const;
113 Offset swapped () const;
115 Real arg () const;
116 Real angle_degrees () const;
117 Real length () const;
118 bool is_sane () const;
119 Offset operator *= (Offset z2);
122 #include "arithmetic-operator.hh"
123 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, +);
124 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, -);
125 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
129 Offset complex_multiply (Offset, Offset);
130 Offset complex_divide (Offset, Offset);
131 Offset complex_exp (Offset);
133 inline Offset
134 Offset::operator *= (Offset z2)
136 *this = complex_multiply (*this, z2);
137 return *this;
140 inline Offset
141 operator * (Real o1, Offset o2)
143 o2 *= o1;
144 return o2;
147 inline Offset
148 operator / (Offset o1, Real a)
150 o1 /= a;
151 return o1;
154 inline Offset
155 operator * (Offset o1, Real o2)
157 o1 *= o2;
158 return o1;
161 inline Offset
162 mirror (Offset o, Axis a)
164 o.mirror (a);
165 return o;
168 inline
169 Real
170 dot_product (Offset o1, Offset o2)
172 return o1[X_AXIS] * o2[X_AXIS] + o1[Y_AXIS] * o2[Y_AXIS];
175 #endif /* OFFSET_HH */