]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/offset.hh
* configure.in: Test for and accept lmodern if EC fonts not found.
[lilypond.git] / flower / include / offset.hh
1 /*
2   offset.hh -- part of GNU LilyPond
3
4   (c) 1996--2004 Han-Wen Nienhuys
5 */
6
7 #ifndef OFFSET_HH
8 #define OFFSET_HH
9
10 #include "axes.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 /*
19
20 This is a mixture a 2D vector. Sometimes it can
21 also be convenient to think of 2D vectors as complex numbers
22 (ie. x + i y). The naming of some methods reflects that.
23
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)[X_AXIS] *= a;
75     (*this)[Y_AXIS] *= a;
76
77     return *this;
78   }
79       
80   Offset (Real ix , Real iy) 
81   {
82     coordinate_a_[X_AXIS] =ix;
83     coordinate_a_[Y_AXIS] =iy;    
84   }
85
86   Offset () 
87   {
88     coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS]= 0.0;
89   }
90
91   String to_string () const;
92
93   Offset& mirror (Axis a)
94   {
95     coordinate_a_[a] = - coordinate_a_[a];
96     return *this;
97   }
98   
99   Real arg () const;
100   Real length () const;
101
102   Offset operator *= (Offset z2) 
103   {
104     *this = complex_multiply (*this, z2);
105     return *this;
106   }
107
108 };
109
110 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, +);
111 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, -);
112 IMPLEMENT_ARITHMETIC_OPERATOR (Offset, *);
113
114 inline Offset
115 operator* (Real o1, Offset o2)
116 {
117   o2 *= o1;
118   return o2;
119 }
120
121 inline Offset
122 operator* (Offset o1, Real o2)
123 {
124   o1 *= o2;
125   return o1;
126 }
127
128 inline Offset
129 mirror (Offset o, Axis a)
130 {
131   o.mirror (a);
132   return o;
133 }
134
135 inline
136 Real
137 dot_product  (Offset o1, Offset o2)
138 {
139   return o1[X_AXIS] * o2[X_AXIS] + o1[Y_AXIS] * o2[Y_AXIS];
140 }
141
142
143 #endif /* OFFSET_HH */
144
145