{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "b99cd770",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "c4d47278",
            "cell_type": "markdown",
            "source": "Convex hull of a random set of points:\n",
            "metadata": {}
        },
        {
            "id": "95619224",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.spatial import ConvexHull, convex_hull_plot_2d\nimport numpy as np\nrng = np.random.default_rng()\npoints = rng.random((30, 2))   # 30 random points in 2-D\nhull = ConvexHull(points)",
            "outputs": []
        },
        {
            "id": "beb38098",
            "cell_type": "markdown",
            "source": "Plot it:\n",
            "metadata": {}
        },
        {
            "id": "7a9411a1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nplt.plot(points[:,0], points[:,1], 'o')\nfor simplex in hull.simplices:\n    plt.plot(points[simplex, 0], points[simplex, 1], 'k-')",
            "outputs": []
        },
        {
            "id": "e5674bfb",
            "cell_type": "markdown",
            "source": "We could also have directly used the vertices of the hull, which\nfor 2-D are guaranteed to be in counterclockwise order:\n",
            "metadata": {}
        },
        {
            "id": "34d26189",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r--', lw=2)\nplt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'ro')\nplt.show()",
            "outputs": []
        },
        {
            "id": "65122b70",
            "cell_type": "markdown",
            "source": "Facets visible from a point:\n\nCreate a square and add a point above the square.\n",
            "metadata": {}
        },
        {
            "id": "c87e0ccc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "generators = np.array([[0.2, 0.2],\n                       [0.2, 0.4],\n                       [0.4, 0.4],\n                       [0.4, 0.2],\n                       [0.3, 0.6]])",
            "outputs": []
        },
        {
            "id": "3ebc0070",
            "cell_type": "markdown",
            "source": "Call ConvexHull with the QG option. QG4 means\ncompute the portions of the hull not including\npoint 4, indicating the facets that are visible\nfrom point 4.\n",
            "metadata": {}
        },
        {
            "id": "5a9bc40e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hull = ConvexHull(points=generators,\n                  qhull_options='QG4')",
            "outputs": []
        },
        {
            "id": "5dc29487",
            "cell_type": "markdown",
            "source": "The \"good\" array indicates which facets are\nvisible from point 4.\n",
            "metadata": {}
        },
        {
            "id": "9543da01",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(hull.simplices)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "    [[1 0]\n     [1 2]\n     [3 0]\n     [3 2]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "be546a2a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(hull.good)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "    [False  True False False]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "92573bae",
            "cell_type": "markdown",
            "source": "Now plot it, highlighting the visible facets.\n",
            "metadata": {}
        },
        {
            "id": "73239c6d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig = plt.figure()\nax = fig.add_subplot(1,1,1)\nfor visible_facet in hull.simplices[hull.good]:\n    ax.plot(hull.points[visible_facet, 0],\n            hull.points[visible_facet, 1],\n            color='violet',\n            lw=6)\nconvex_hull_plot_2d(hull, ax=ax)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "    <Figure size 640x480 with 1 Axes> # may vary"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5f26aca9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.show()",
            "outputs": []
        }
    ]
}