X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fbezier.cc;h=b341d73754b35ccf5828c9f2372ea65847e5878c;hb=cb01be4028886109ecc4772234452965ce7dfb66;hp=3712b9213ef91c26ab1d5fb74d0fb56127dc5c06;hpb=38d7d319eabc906e82fb42002678c6d42a23b6f7;p=lilypond.git diff --git a/lily/bezier.cc b/lily/bezier.cc index 3712b9213e..b341d73754 100644 --- a/lily/bezier.cc +++ b/lily/bezier.cc @@ -265,3 +265,52 @@ Bezier::reverse () b2.control_[CONTROL_COUNT - i - 1] = control_[i]; *this = b2; } + + +/* + Subdivide a bezier at T into LEFT_PART and RIGHT_PART + using deCasteljau's algorithm. +*/ +void +Bezier::subdivide (Real t, Bezier *left_part, Bezier *right_part) const +{ + Offset p[CONTROL_COUNT][CONTROL_COUNT]; + + for (int i = 0; i < CONTROL_COUNT ; i++) + p[i][CONTROL_COUNT - 1 ] = control_[i]; + for (int j = CONTROL_COUNT - 2; j >= 0 ; j--) + for (int i = 0; i < CONTROL_COUNT -1; i++) + p[i][j] = p[i][j+1] + t * (p[i+1][j+1] - p[i][j+1]); + for (int i = 0; i < CONTROL_COUNT; i++) + { + left_part->control_[i]=p[0][CONTROL_COUNT - 1 - i]; + right_part->control_[i]=p[i][i]; + } +} + +/* + Extract a portion of a bezier from T_MIN to T_MAX +*/ + +Bezier +Bezier::extract (Real t_min, Real t_max) const +{ + if ((t_min < 0) || (t_max) > 1) + programming_error + ("bezier extract arguments outside of limits: curve may have bad shape"); + if (t_min >= t_max) + programming_error + ("lower bezier extract value not less than upper value: curve may have bad shape"); + Bezier bez1, bez2, bez3, bez4; + if (t_min == 0.0) + bez2 = *this; + else + subdivide (t_min, &bez1, &bez2); + if (t_max == 1.0) + return bez2; + else + { + bez2.subdivide ((t_max-t_min)/(1-t_min), &bez3, &bez4); + return bez3; + } +}