2 bezier.cc -- implement Bezier and Bezier_bow
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "polynomial.hh"
14 binomial_coefficient (Real over , int under)
20 x *= over / Real (under);
29 flip (Array<Offset>* arr_p, Axis a)
32 // for (int i = c.size (); i--;)
33 for (int i = 0; i < arr_p->size (); i++)
34 (*arr_p)[i][a] = - (*arr_p)[i][a];
38 rotate (Array<Offset>* arr_p, Real phi)
40 Offset rot (complex_exp (Offset (0, phi)));
41 for (int i = 0; i < arr_p->size (); i++)
42 (*arr_p)[i] = complex_multiply (rot, (*arr_p)[i]);
46 translate (Array<Offset>* arr_p, Offset o)
48 for (int i = 0; i < arr_p->size (); i++)
54 Formula of the bezier 3-spline
56 sum_{j=0}^3 (3 over j) z_j (1-t)^(3-j) t^j
60 : control_ (CONTROL_COUNT)
65 Bezier::get_other_coordinate (Axis a, Real x) const
67 Axis other = Axis ((a +1)%NO_AXES);
68 Array<Real> ts = solve_point (a, x);
70 Offset c = curve_point (ts[0]);
71 assert (fabs (c[a] - x) < 1e-8);
78 Bezier::curve_point (Real t)const
81 Real one_min_tj = (1-t)*(1-t)*(1-t);
84 for (int j=0 ; j < 4; j++)
86 o += control_[j] * binomial_coefficient (3, j)
87 * pow (t,j) * pow (1-t, 3-j);
94 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t))< 1e-8);
95 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t))< 1e-8);
103 Bezier::polynomial (Axis a)const
106 for (int j=0; j <= 3; j++)
109 * Polynomial::power (j , Polynomial (0,1))*
110 Polynomial::power (3 - j, Polynomial (1,-1))*
111 binomial_coefficient(3, j);
118 Remove all numbers outside [0,1] from SOL
121 filter_solutions (Array<Real> sol)
123 for (int i = sol.size (); i--;)
124 if (sol[i] < 0 || sol[i] >1)
130 find t such that derivative is proportional to DERIV
133 Bezier::solve_derivative (Offset deriv)const
135 Polynomial xp=polynomial (X_AXIS);
136 Polynomial yp=polynomial (Y_AXIS);
140 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
142 return filter_solutions (combine.solve ());
147 Find t such that curve_point (t)[AX] == COORDINATE
150 Bezier::solve_point (Axis ax, Real coordinate) const
152 Polynomial p(polynomial (ax));
153 p.coefs_[0] -= coordinate;
155 Array<Real> sol (p.solve ());
156 return filter_solutions (sol);
160 Bezier::extent (Axis a)const
162 int o = (a+1)%NO_AXES;
166 Array<Real> sols (solve_derivative (d));
169 for (int i= sols.size (); i--;)
171 Offset o (curve_point (sols[i]));
172 iv.unite (Interval (o[a],o[a]));
178 Bezier::flip (Axis a)
180 ::flip (&control_, a);
184 Bezier::rotate (Real phi)
186 ::rotate (&control_, phi);
190 Bezier::translate (Offset o)
192 ::translate (&control_, o);
196 Bezier::assert_sanity () const
198 for (int i=0; i < CONTROL_COUNT; i++)
199 assert (!isnan (control_[i].length ())
200 && !isinf (control_[i].length ()));
207 for (int i =0; i < CONTROL_COUNT; i++)
208 b2.control_[CONTROL_COUNT-i-1] = control_[i];