]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier.cc
Merge commit 'origin'
[lilypond.git] / lily / bezier.cc
index 77f3949992cbf1fb6eaa380245d190aa3da3ec47..b341d73754b35ccf5828c9f2372ea65847e5878c 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include "bezier.hh"
@@ -14,21 +14,6 @@ Real binomial_coefficient_3[] = {
   1, 3, 3, 1
 };
 
-Real
-binomial_coefficient (Real over, int under)
-{
-  Real x = 1.0;
-
-  while (under)
-    {
-      x *= over / Real (under);
-
-      over -= 1.0;
-      under--;
-    }
-  return x;
-}
-
 void
 scale (vector<Offset> *array, Real x, Real y)
 {
@@ -132,7 +117,7 @@ Bezier::curve_point (Real t) const
 }
 
 /*
-  Cache binom(3,j) t^j (1-t)^{3-j}
+  Cache binom (3, j) t^j (1-t)^{3-j}
 */
 struct Polynomial_cache {
   Polynomial terms_[4];
@@ -280,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;
+  }
+}