3. polygon
¶
Module for defining the class related to a Polygon and to verify what points are inside it.
-
class
polygon.
Polygon
(coordinates)[source]¶ Bases:
object
Creates an instance of an object that defines a two dimension polygon.
Polygon(coordinates)
-
coordinates
¶ Coordinates of vertices of the polygon.
Type: (n, 2) numpy.ndarray
Note
The class Polygon requires numpy and matplotlib.
Examples
>>> from numpy import array >>> from pybimstab.polygon import Polygon >>> coords = array([[0. , 1. , 1. , 0. ], [0. , 0. , 1.5, 1.5]]) >>> polygon = Polygon(coordinates=coords) >>> polygon.__dict__ {'coordinates': array([[0. , 1. , 1. , 0. ], [0. , 0. , 1.5, 1.5]])}
-
isinside
(x, y, meshgrid=False, want2plot=False)[source]¶ Method to know if the point(s) (x, y) is inside of the instanced polygon.
x
andy
could be iterable structures, even, they could define a meshgrid.Parameters: - x (int, float or (n, ) numpy.ndarray) – abscissa of the point(s) to check if is/are inside the polygon.
- y (int, float or (n, ) numpy.ndarray) – ordinate of the point(s) to check if is/are inside the polygon.
- meshgrid (bool) – variable to creck if
x
andy
define a grid. The default value isFalse
. - want2plot (bool) – variable to creck if a plot is wanted.
The default value is
False
.
Returns: Array of boolean values where True means that the point is inside and False to points that is outside of the polygon.
Return type: numpy.ndarray
Note
The method
isinside
does not work properly for points on the boundaries of the polygon.Examples
>>> from numpy import array >>> from pybimstab.polygon import Polygon >>> coords = array([[0. , 1. , 1. , 0. ], [0. , 0. , 1.5, 1.5]]) >>> x, y = 0.5, 2 >>> polygon = Polygon(coordinates=coords) >>> polygon.isinside(x=x, y=y, meshgrid=False, want2plot=True) array([True], dtype=bool)
>>> from numpy import array >>> from pybimstab.polygon import Polygon >>> coords = array([[0, 1, 1, 0.], [0, 0, 1.5, 1.5]]) >>> x = array([0.3, 0.5, 0.7, 1.2, 1.0]) >>> y = array([0.6, 1., 1.4, 2.4, 0]) >>> polygon = Polygon(coordinates=coords) >>> polygon.isinside(x=x, y=y, meshgrid=False, want2plot=True) array([True, True, True, False, False], dtype=bool)
>>> from numpy import array >>> from pybimstab.polygon import Polygon >>> coords = array([[0, 1, 1, 0.], [0, 0, 1.5, 1.5]]) >>> x = array([0.3, 0.5, 0.7, 1.2, 1.0]) >>> y = array([0.6, 1., 1.4, 2.4, 0]) >>> polygon = Polygon(coordinates=coords) >>> polygon.isinside(x=x, y=y, meshgrid=True, want2plot=True) array([[ True, True, True, False, True], [ True, True, True, False, True], [ True, True, True, False, True], [False, False, False, False, False], [False, False, False, False, False]], dtype=bool)
>>> import numpy as np >>> from pybimstab.polygon import Polygon >>> xC = [np.cos(theta) for theta in np.linspace(0, 2*np.pi, 100)] >>> yC = [np.sin(theta) for theta in np.linspace(0, 2*np.pi, 100)] >>> coords = np.array([xC, yC]) >>> np.random.seed(123) >>> x = np.random.uniform(-1, 1, 100) >>> y = np.random.uniform(-1, 1, 100) >>> polygon = Polygon(coordinates=coords) >>> polygon.isinside(x=x, y=y, meshgrid=False, want2plot=True);
-