chiark / gitweb /
bezier.py: trim
[moebius3.git] / bezier.py
1 # Copied from
2 #   /usr/lib/python2.7/dist-packages/matplotlib/bezier.py
3 # in Debian's python-matplotlib 2.0.0+dfsg1-2 (amd64)
4 # and trimmed to have only BezierSegment
5
6 # 1. This LICENSE AGREEMENT is between the Matplotlib Development Team
7 # ("MDT"), and the Individual or Organization ("Licensee") accessing and
8 # otherwise using matplotlib software in source or binary form and its
9 # associated documentation.
10 # .
11 # 2. Subject to the terms and conditions of this License Agreement, MDT
12 # hereby grants Licensee a nonexclusive, royalty-free, world-wide license
13 # to reproduce, analyze, test, perform and/or display publicly, prepare
14 # derivative works, distribute, and otherwise use matplotlib
15 # alone or in any derivative version, provided, however, that MDT's
16 # License Agreement and MDT's notice of copyright, i.e., "Copyright (c)
17 # 2012- Matplotlib Development Team; All Rights Reserved" are retained in
18 # matplotlib  alone or in any derivative version prepared by
19 # Licensee.
20 # .
21 # 3. In the event Licensee prepares a derivative work that is based on or
22 # incorporates matplotlib or any part thereof, and wants to
23 # make the derivative work available to others as provided herein, then
24 # Licensee hereby agrees to include in any such work a brief summary of
25 # the changes made to matplotlib .
26 # .
27 # 4. MDT is making matplotlib available to Licensee on an "AS
28 # IS" basis.  MDT MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
29 # IMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, MDT MAKES NO AND
30 # DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
31 # FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF MATPLOTLIB
32 # WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.
33 # .
34 # 5. MDT SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF MATPLOTLIB
35 #  FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR
36 # LOSS AS A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING
37 # MATPLOTLIB , OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF
38 # THE POSSIBILITY THEREOF.
39 # .
40 # 6. This License Agreement will automatically terminate upon a material
41 # breach of its terms and conditions.
42 # .
43 # 7. Nothing in this License Agreement shall be deemed to create any
44 # relationship of agency, partnership, or joint venture between MDT and
45 # Licensee.  This License Agreement does not grant permission to use MDT
46 # trademarks or trade name in a trademark sense to endorse or promote
47 # products or services of Licensee, or any third party.
48 # .
49 # 8. By copying, installing or otherwise using matplotlib ,
50 # Licensee agrees to be bound by the terms and conditions of this License
51 # Agreement.
52
53 import six
54 import numpy as np
55 import warnings
56
57 class BezierSegment(object):
58     """
59     A simple class of a 2-dimensional bezier segment
60     """
61
62     # Higher order bezier lines can be supported by simplying adding
63     # corresponding values.
64     _binom_coeff = {1: np.array([1., 1.]),
65                     2: np.array([1., 2., 1.]),
66                     3: np.array([1., 3., 3., 1.])}
67
68     def __init__(self, control_points):
69         """
70         *control_points* : location of contol points. It needs have a
71          shpae of n * 2, where n is the order of the bezier line. 1<=
72          n <= 3 is supported.
73         """
74         _o = len(control_points)
75         self._orders = np.arange(_o)
76         _coeff = BezierSegment._binom_coeff[_o - 1]
77
78         _control_points = np.asarray(control_points)
79         xx = _control_points[:, 0]
80         yy = _control_points[:, 1]
81
82         self._px = xx * _coeff
83         self._py = yy * _coeff
84
85     def point_at_t(self, t):
86         "evaluate a point at t"
87         one_minus_t_powers = np.power(1. - t, self._orders)[::-1]
88         t_powers = np.power(t, self._orders)
89
90         tt = one_minus_t_powers * t_powers
91         _x = sum(tt * self._px)
92         _y = sum(tt * self._py)
93
94         return _x, _y