chiark / gitweb /
bezier.py: Make N-dimensional
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 4 Nov 2017 17:25:06 +0000 (17:25 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 4 Nov 2017 17:33:33 +0000 (17:33 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
bezier.py

index 4a08c6b65a90daf77e8642f3bc17c97358518801..55423d0e61f7d392006ce7ce6ea5e2ceac0bfd71 100644 (file)
--- a/bezier.py
+++ b/bezier.py
@@ -58,7 +58,7 @@ import warnings
 
 class BezierSegment(object):
     """
-    A simple class of a 2-dimensional bezier segment
+    A simple class of a N-dimensional bezier segment
     """
 
     # Higher order bezier lines can be supported by simplying adding
@@ -70,19 +70,19 @@ class BezierSegment(object):
     def __init__(self, control_points):
         """
         *control_points* : location of contol points. It needs have a
-         shpae of n * 2, where n is the order of the bezier line. 1<=
-         n <= 3 is supported.
+         shpae of n * D, where n is the order of the bezier line
+         and D is the dimension (the length of each control point
+         tuple). 1<= n <= 3 is supported.
         """
         _o = len(control_points)
+        dim = len(control_points[0])
         self._orders = np.arange(_o)
         _coeff = BezierSegment._binom_coeff[_o - 1]
 
         _control_points = np.asarray(control_points)
-        xx = _control_points[:, 0]
-        yy = _control_points[:, 1]
+        xyz = [_control_points[:, i] for i in range(0, dim)]
 
-        self._px = xx * _coeff
-        self._py = yy * _coeff
+        self._pxyz = [xx * _coeff for xx in xyz]
 
     def point_at_t(self, t):
         "evaluate a point at t"
@@ -90,7 +90,6 @@ class BezierSegment(object):
         t_powers = np.power(t, self._orders)
 
         tt = one_minus_t_powers * t_powers
-        _x = sum(tt * self._px)
-        _y = sum(tt * self._py)
+        _xyz = [sum(tt * px) for px in self._pxyz]
 
-        return _x, _y
+        return tuple(_xyz)