]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/offset.hh
* flower/include/std-string.hh: String/std::string compatibility:
[lilypond.git] / flower / include / offset.hh
1 /*
2   offset.hh -- part of GNU LilyPond
3
4   (c) 1996--2006 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 "string.hh"
13 #include "real.hh"
14
15 class Offset;
16 Offset complex_multiply (Offset, Offset);
17 Offset complex_divide (Offset, Offset);
18 Offset complex_exp (Offset);
19
20 /*
21   This is a mixture a 2D vector. Sometimes it can
22   also be convenient to think of 2D vectors as complex numbers
23   (ie. x + i y). The naming of some methods reflects that.
24 */
25 class Offset
26 {
27 public:
28   Real coordinate_a_[NO_AXES];
29
30   Real &operator [] (Axis i)
31   {
32     return coordinate_a_[i];
33   }
34
35   Real operator [] (Axis i) const
36   {
37     return coordinate_a_[i];
38   }
39
40   Offset &operator += (Offset o)
41   {
42     (*this)[X_AXIS] += o[X_AXIS];
43     (*this)[Y_AXIS] += o[Y_AXIS];
44     return *this;
45   }
46
47   Offset operator - () const
48   {
49     Offset o = *this;
50
51     o[X_AXIS] = -o[X_AXIS];
52     o[Y_AXIS] = -o[Y_AXIS];
53     return o;
54   }
55
56   Offset &operator -= (Offset o)
57   {
58     (*this)[X_AXIS] -= o[X_AXIS];
59     (*this)[Y_AXIS] -= o[Y_AXIS];
60
61     return *this;
62   }
63
64   Offset &scale (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 &operator /= (Real a)
73   {
74     (*this) *= 1/a;
75     return *this;
76   }
77
78   Offset &operator *= (Real a)
79   {
80     (*this)[X_AXIS] *= a;
81     (*this)[Y_AXIS] *= a;
82
83     return *this;
84   }
85
86   Offset (Real ix, Real iy)
87   {
88     coordinate_a_[X_AXIS] = ix;
89     coordinate_a_[Y_AXIS] = iy;
90   }
91
92   Offset ()
93   {
94     coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0;
95   }
96
97   Std_string to_string () const;
98
99   Offset &mirror (Axis a)
100   {
101     coordinate_a_[a] = -coordinate_a_[a];
102     return *this;
103   }
104
105   Real arg () const;
106   Real length () const;
107   bool is_sane () const;
108   Offset operator *= (Offset z2)
109   {
110     *this = complex_multiply (*this, z2);
111     return *this;
112   }
113 };
114
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