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