.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/bezier/plot_01_de_casteljau.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_geometry_bezier_plot_01_de_casteljau.py: De Casteljau's algorithm and Bézier curves ================================================ Draws a cubic Bézier curve and the construction lines of de Casteljau's algorithm at t = 0.4: interpolate between neighbouring control points, then between the new points, until one point, the curve point, is left. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.geometry import bezier_curve, de_casteljau .. GENERATED FROM PYTHON SOURCE LINES 17-19 The construction at t = 0.4 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-33 .. code-block:: Python control = np.array([[0.0, 0.0], [1.0, 3.0], [3.5, 3.5], [4.5, 0.5]]) curve = bezier_curve(control, np.linspace(0, 1, 200)) levels = de_casteljau(control, 0.4) fig, ax = plt.subplots() ax.plot(*curve.T, "k", lw=2, label="Bézier curve") for level, color in zip(levels, ("tab:gray", "tab:blue", "tab:green", "tab:red")): ax.plot(*level.T, "o-", color=color, ms=6) ax.plot(*levels[-1][0], "o", color="tab:red", ms=10, label="B(0.4)") ax.set_aspect("equal") ax.legend() ax.set_title("de Casteljau's algorithm") .. image-sg:: /api/gallery/geometry/bezier/images/sphx_glr_plot_01_de_casteljau_001.png :alt: de Casteljau's algorithm :srcset: /api/gallery/geometry/bezier/images/sphx_glr_plot_01_de_casteljau_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, "de Casteljau's algorithm") .. GENERATED FROM PYTHON SOURCE LINES 34-36 The curve stays inside the convex hull of its control points ---------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python print(f"curve x range [{curve[:, 0].min():.3f}, {curve[:, 0].max():.3f}], control x range [0, 4.5]") print(f"curve starts at {curve[0]} and ends at {curve[-1]}: the first and last control points") .. rst-class:: sphx-glr-script-out .. code-block:: none curve x range [0.000, 4.500], control x range [0, 4.5] curve starts at [0. 0.] and ends at [4.5 0.5]: the first and last control points .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.025 seconds) .. _sphx_glr_download_api_gallery_geometry_bezier_plot_01_de_casteljau.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_de_casteljau.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_de_casteljau.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_de_casteljau.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_