sketchingpy.bezier_util

Utility to build bezier curves.

License:

BSD

 1"""Utility to build bezier curves.
 2
 3License:
 4    BSD
 5"""
 6import typing
 7
 8bezier_available = False
 9
10try:
11    import bezier  # type: ignore
12    import numpy
13    bezier_available = True
14except:
15    pass
16
17
18class BezierMaker:
19    """Wrapper around the bezier library to generate points along a bezier curve."""
20
21    def __init__(self):
22        """Create a new curve with no points."""
23        if not bezier_available:
24            raise RuntimeError('Please pip install bezier to use bezier curves.')
25
26        self._points = []
27
28    def add_point(self, x: float, y: float):
29        """Add a point to this curve.
30
31        Add a point to this curve. If it is the first or last point, it is start and destination
32        respectively. Otherwise, it is a control point.
33
34        Args:
35            x: The x position of the point.
36            y: The y position of the point.
37        """
38        self._points.append((x, y))
39
40    def get_points(self, num_points: int) -> typing.List[typing.Tuple[float, float]]:
41        """Get a series of points within the curve.
42
43        Args:
44            num_points: The number of points to return.
45
46        Returns:
47            List of coordinates along curve.
48        """
49        if len(self._points) == 0:
50            raise RuntimeError('Curve without points.')
51
52        input_array = numpy.transpose(numpy.array(self._points))
53        degree = len(self._points) - 1
54        curve = bezier.Curve(input_array, degree=degree)
55
56        input_point_ids = range(0, num_points)
57        input_fracs = map(lambda x: x / (num_points - 1.0), input_point_ids)
58        numpy_points = map(lambda x: curve.evaluate(x), input_fracs)
59        simple_points = map(lambda x: (float(x[0]), float(x[1])), numpy_points)
60        simple_points_realized = list(simple_points)
61
62        return simple_points_realized
bezier_available = True
class BezierMaker:
19class BezierMaker:
20    """Wrapper around the bezier library to generate points along a bezier curve."""
21
22    def __init__(self):
23        """Create a new curve with no points."""
24        if not bezier_available:
25            raise RuntimeError('Please pip install bezier to use bezier curves.')
26
27        self._points = []
28
29    def add_point(self, x: float, y: float):
30        """Add a point to this curve.
31
32        Add a point to this curve. If it is the first or last point, it is start and destination
33        respectively. Otherwise, it is a control point.
34
35        Args:
36            x: The x position of the point.
37            y: The y position of the point.
38        """
39        self._points.append((x, y))
40
41    def get_points(self, num_points: int) -> typing.List[typing.Tuple[float, float]]:
42        """Get a series of points within the curve.
43
44        Args:
45            num_points: The number of points to return.
46
47        Returns:
48            List of coordinates along curve.
49        """
50        if len(self._points) == 0:
51            raise RuntimeError('Curve without points.')
52
53        input_array = numpy.transpose(numpy.array(self._points))
54        degree = len(self._points) - 1
55        curve = bezier.Curve(input_array, degree=degree)
56
57        input_point_ids = range(0, num_points)
58        input_fracs = map(lambda x: x / (num_points - 1.0), input_point_ids)
59        numpy_points = map(lambda x: curve.evaluate(x), input_fracs)
60        simple_points = map(lambda x: (float(x[0]), float(x[1])), numpy_points)
61        simple_points_realized = list(simple_points)
62
63        return simple_points_realized

Wrapper around the bezier library to generate points along a bezier curve.

BezierMaker()
22    def __init__(self):
23        """Create a new curve with no points."""
24        if not bezier_available:
25            raise RuntimeError('Please pip install bezier to use bezier curves.')
26
27        self._points = []

Create a new curve with no points.

def add_point(self, x: float, y: float):
29    def add_point(self, x: float, y: float):
30        """Add a point to this curve.
31
32        Add a point to this curve. If it is the first or last point, it is start and destination
33        respectively. Otherwise, it is a control point.
34
35        Args:
36            x: The x position of the point.
37            y: The y position of the point.
38        """
39        self._points.append((x, y))

Add a point to this curve.

Add a point to this curve. If it is the first or last point, it is start and destination respectively. Otherwise, it is a control point.

Arguments:
  • x: The x position of the point.
  • y: The y position of the point.
def get_points(self, num_points: int) -> List[Tuple[float, float]]:
41    def get_points(self, num_points: int) -> typing.List[typing.Tuple[float, float]]:
42        """Get a series of points within the curve.
43
44        Args:
45            num_points: The number of points to return.
46
47        Returns:
48            List of coordinates along curve.
49        """
50        if len(self._points) == 0:
51            raise RuntimeError('Curve without points.')
52
53        input_array = numpy.transpose(numpy.array(self._points))
54        degree = len(self._points) - 1
55        curve = bezier.Curve(input_array, degree=degree)
56
57        input_point_ids = range(0, num_points)
58        input_fracs = map(lambda x: x / (num_points - 1.0), input_point_ids)
59        numpy_points = map(lambda x: curve.evaluate(x), input_fracs)
60        simple_points = map(lambda x: (float(x[0]), float(x[1])), numpy_points)
61        simple_points_realized = list(simple_points)
62
63        return simple_points_realized

Get a series of points within the curve.

Arguments:
  • num_points: The number of points to return.
Returns:

List of coordinates along curve.