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"
15 Formula of the bezier 3-spline
17 sum_{j=0}^3 (3 over j) z_j (1-t)^(3-j) t^j
25 Bezier::get_other_coordinate (Axis a, Real x) const
27 Axis other = Axis ((a +1)%NO_AXES);
28 Array<Real> ts = solve_point (a, x);
30 Offset c = curve_point (ts[0]);
31 assert (fabs (c[a] - x) < 1e-8);
37 binomial_coefficient (Real over , int under)
43 x *= over / Real (under);
52 Bezier::curve_point (Real t)const
55 Real one_min_tj = (1-t)*(1-t)*(1-t);
58 for (int j=0 ; j < 4; j++)
60 o += control_[j] * binomial_coefficient (3, j)
61 * pow (t,j) * pow (1-t, 3-j);
68 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t))< 1e-8);
69 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t))< 1e-8);
77 Bezier::polynomial (Axis a)const
80 for (int j=0; j <= 3; j++)
83 * Polynomial::power (j , Polynomial (0,1))*
84 Polynomial::power (3 - j, Polynomial (1,-1))*
85 binomial_coefficient(3, j);
92 Remove all numbers outside [0,1] from SOL
95 filter_solutions (Array<Real> sol)
97 for (int i = sol.size (); i--;)
98 if (sol[i] < 0 || sol[i] >1)
104 find t such that derivative is proportional to DERIV
107 Bezier::solve_derivative (Offset deriv)const
109 Polynomial xp=polynomial (X_AXIS);
110 Polynomial yp=polynomial (Y_AXIS);
114 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
116 return filter_solutions (combine.solve ());
121 Find t such that curve_point (t)[AX] == COORDINATE
124 Bezier::solve_point (Axis ax, Real coordinate) const
126 Polynomial p(polynomial (ax));
127 p.coefs_[0] -= coordinate;
129 Array<Real> sol (p.solve ());
130 return filter_solutions (sol);
134 Bezier::extent (Axis a)const
136 int o = (a+1)%NO_AXES;
140 Array<Real> sols (solve_derivative (d));
143 for (int i= sols.size (); i--;)
145 Offset o (curve_point (sols[i]));
146 iv.unite (Interval (o[a],o[a]));
152 Bezier::flip (Axis a)
154 for (int i = CONTROL_COUNT; i--;)
155 control_[i][a] = - control_[i][a];
159 Bezier::rotate (Real phi)
161 Offset rot (complex_exp (Offset (0, phi)));
162 for (int i = 0; i < CONTROL_COUNT; i++)
163 control_[i] = complex_multiply (rot, control_[i]);
167 Bezier::translate (Offset o)
169 for (int i = 0; i < CONTROL_COUNT; i++)
174 Bezier::check_sanity () const
176 for (int i=0; i < CONTROL_COUNT; i++)
177 assert (!isnan (control_[i].length ())
178 && !isinf (control_[i].length ()));
185 for (int i =0; i < CONTROL_COUNT; i++)
186 b2.control_[CONTROL_COUNT-i-1] = control_[i];