From 666d65dca43a905b977603d0942bfa4869435578 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 4 Nov 2017 17:25:06 +0000 Subject: [PATCH] bezier.py: Make N-dimensional Signed-off-by: Ian Jackson --- bezier.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/bezier.py b/bezier.py index 4a08c6b..55423d0 100644 --- 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) -- 2.30.2