

.. _sphx_glr_auto_examples_plot_convex_hull.py:



===========
Convex Hull
===========

Convex hull of a set of points, representing Dom x-y positions.

Derived from ``scipy.spatial.qhull.pyx``.



.. code-block:: python

    from __future__ import absolute_import, print_function, division

    import numpy as np
    from scipy.spatial import ConvexHull
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D    # noqa

    from km3pipe.hardware import Detector
    from km3pipe.math import Polygon
    import km3pipe.style
    km3pipe.style.use("km3pipe")

    filename = "data/orca_115strings_av23min20mhorizontal_18OMs_alt9mvertical_v1.detx"    # noqa
    detector = Detector(filename)
    xy = detector.xy_positions
    hull = ConvexHull(xy)





.. rst-class:: sphx-glr-script-out

 Out::

    Loading style definitions from '/Users/tamasgal/Dev/km3pipe/km3pipe/kp-data/stylelib/km3pipe.mplstyle'
    Detector: Parsing the DETX header
    Detector: Reading PMT information...
    Detector: Done.


Plot it:



.. code-block:: python


    plt.plot(xy[:, 0], xy[:, 1], 'o')
    for simplex in hull.simplices:
        plt.plot(xy[simplex, 0], xy[simplex, 1], 'k-')




.. image:: /auto_examples/images/sphx_glr_plot_convex_hull_001.png
    :align: center




We could also have directly used the vertices of the hull, which
for 2-D are guaranteed to be in counterclockwise order:



.. code-block:: python


    plt.plot(xy[hull.vertices, 0], xy[hull.vertices, 1], 'r--', lw=2)
    plt.plot(xy[hull.vertices[0], 0], xy[hull.vertices[0], 1], 'ro')
    plt.show()




.. image:: /auto_examples/images/sphx_glr_plot_convex_hull_002.png
    :align: center




Now let's draw a polygon inside, and see which points are contained.



.. code-block:: python


    poly_vertices = np.array([
        (-60, 120),
        (80, 120),
        (110, 60),
        (110, -30),
        (70, -110),
        (-70, -110),
        (-90, -70),
        (-90, 60),
    ])
    poly = Polygon(poly_vertices)
    contain_mask = poly.contains(xy)







and color them accordingly



.. code-block:: python


    plt.clf()
    plt.plot(xy[contain_mask, 0], xy[contain_mask, 1], 'yo')
    plt.plot(xy[~contain_mask, 0], xy[~contain_mask, 1], 'bo')
    plt.plot(poly_vertices[:, 0], poly_vertices[:, 1], 'k-')
    plt.show()




.. image:: /auto_examples/images/sphx_glr_plot_convex_hull_003.png
    :align: center




plot the same in 3D because why not?



.. code-block:: python


    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(xy[:, 0], xy[:, 1], 90, 'yo')
    ax.scatter(xy[:, 0], xy[:, 1], -90, 'bo')
    ax.plot(poly_vertices[:, 0], poly_vertices[:, 1], 90, 'k-')
    for simplex in hull.simplices:
        ax.plot(xy[simplex, 0], xy[simplex, 1], -90, 'k-')
    plt.show()



.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /auto_examples/images/sphx_glr_plot_convex_hull_004.png
            :scale: 47

    *

      .. image:: /auto_examples/images/sphx_glr_plot_convex_hull_005.png
            :scale: 47




**Total running time of the script:** ( 0 minutes  3.908 seconds)



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_convex_hull.py <plot_convex_hull.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_convex_hull.ipynb <plot_convex_hull.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
