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