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