
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
What's the fastest way of checking if a point is inside a polygon in Python?
First, we will create a polygon using the mplPath.Path method and to check whether a given point is in the polygon or not, we will use the method, poly_path.contains_point.
Steps
Create a list of points to make the polygon.
Create a new path with the given vertices and codes, using mplPath.Path().
Check if point (200, 100) exists in the polygon or not, using contains_point() method. Return whether the (closed) path contains the given point. => True
Check if point (1200, 1000) exists in the polygon or not, using contains_point() method. Return whether the (closed) path contains the given point. => False
Example
import matplotlib.path as mplPath import numpy as np poly = [190, 50, 500, 310] poly_path = mplPath.Path(np.array([[190, 50], [50, 500], [500, 310], [310, 190]])) point = (200, 100) print(point, " is in polygon: ", poly_path.contains_point(point)) point = (1200, 1000) print(point, " is in polygon: ", poly_path.contains_point(point))
Output
(200, 100) is in polygon: True (1200, 1000) is in polygon: False
- Related Articles
- Check if a given point lies inside a Polygon
- What's the fastest way to split a text file using Python?
- Which is the fastest way to get a column's maximum value in MySQL?
- What is the fastest way to learn Python with real-time examples?
- What is the fastest way to insert a large number of rows into a MySQL table?
- Check if a point lies on or inside a rectangle in Python
- Is s circle a polygon?
- Program to check given point in inside or boundary of given polygon or not in python
- Fastest way of updating in MongoDB is update() or save()?
- Check if a point is inside, outside or on the ellipse in C++
- Check if a point is inside, outside or on the parabola in C++
- Find if a point lies inside a Circle in C++
- What is the fastest way to update the whole document (all fields) in MongoDB?
- What is best way to check if a list is empty in Python?
- Checking for convex polygon in JavaScript

Advertisements