2 bezier.cc -- implement Bezier and Bezier_bow
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "libc-extension.hh"
15 #include "polynomial.hh"
18 binomial_coefficient (Real over , int under)
24 x *= over / Real (under);
33 scale (Array<Offset>* array, Real x , Real y)
35 for (int i = 0; i < array->size (); i++)
37 (*array)[i][X_AXIS] = x* (*array)[i][X_AXIS];
38 (*array)[i][Y_AXIS] = y* (*array)[i][Y_AXIS];
43 rotate (Array<Offset>* array, Real phi)
45 Offset rot (complex_exp (Offset (0, phi)));
46 for (int i = 0; i < array->size (); i++)
47 (*array)[i] = complex_multiply (rot, (*array)[i]);
51 translate (Array<Offset>* array, Offset o)
53 for (int i = 0; i < array->size (); i++)
59 Formula of the bezier 3-spline
61 sum_{j=0}^3 (3 over j) z_j (1-t)^ (3-j) t^j
64 A is the axis of X coordinate.
68 Bezier::get_other_coordinate (Axis a, Real x) const
70 Axis other = Axis ((a +1)%NO_AXES);
71 Array<Real> ts = solve_point (a, x);
75 programming_error ("No solution found for Bezier intersection.");
79 Offset c = curve_point (ts[0]);
81 if (fabs (c[a] - x) > 1e-8)
82 programming_error ("Bezier intersection not correct?");
89 Bezier::curve_point (Real t)const
92 Real one_min_tj = (1-t)* (1-t)* (1-t);
95 for (int j=0 ; j < 4; j++)
97 o += control_[j] * binomial_coefficient (3, j)
98 * pow (t,j) * pow (1-t, 3-j);
106 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t))< 1e-8);
107 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t))< 1e-8);
115 Bezier::polynomial (Axis a)const
118 for (int j=0; j <= 3; j++)
120 p += (control_[j][a] * binomial_coefficient (3, j))
121 * Polynomial::power (j , Polynomial (0,1))*
122 Polynomial::power (3 - j, Polynomial (1,-1));
129 Remove all numbers outside [0,1] from SOL
132 filter_solutions (Array<Real> sol)
134 for (int i = sol.size (); i--;)
135 if (sol[i] < 0 || sol[i] >1)
141 find t such that derivative is proportional to DERIV
144 Bezier::solve_derivative (Offset deriv)const
146 Polynomial xp=polynomial (X_AXIS);
147 Polynomial yp=polynomial (Y_AXIS);
151 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
153 return filter_solutions (combine.solve ());
158 Find t such that curve_point (t)[AX] == COORDINATE
161 Bezier::solve_point (Axis ax, Real coordinate) const
163 Polynomial p (polynomial (ax));
164 p.coefs_[0] -= coordinate;
166 Array<Real> sol (p.solve ());
167 return filter_solutions (sol);
171 Compute the bounding box dimensions in direction of A.
174 Bezier::extent (Axis a)const
176 int o = (a+1)%NO_AXES;
180 Array<Real> sols (solve_derivative (d));
183 for (int i= sols.size (); i--;)
185 Offset o (curve_point (sols[i]));
186 iv.unite (Interval (o[a],o[a]));
196 Bezier::scale (Real x, Real y)
198 for (int i = CONTROL_COUNT; i--;)
200 control_[i][X_AXIS] = x * control_[i][X_AXIS];
201 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
206 Bezier::rotate (Real phi)
208 Offset rot (complex_exp (Offset (0, phi)));
209 for (int i = 0; i < CONTROL_COUNT; i++)
210 control_[i] = complex_multiply (rot, control_[i]);
214 Bezier::translate (Offset o)
216 for (int i = 0; i < CONTROL_COUNT; i++)
221 Bezier::assert_sanity () const
223 for (int i=0; i < CONTROL_COUNT; i++)
224 assert (!isnan (control_[i].length ())
225 && !isinf (control_[i].length ()));
232 for (int i =0; i < CONTROL_COUNT; i++)
233 b2.control_[CONTROL_COUNT-i-1] = control_[i];