- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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
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))
(200, 100) is in polygon: True (1200, 1000) is in polygon: False
- Check if a given point lies inside a Polygon
- Which is the fastest way to get a column's maximum value in MySQL?
- 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
- 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++
- 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()?
- What is best way to check if a list is empty in Python?
- What is the fastest way to update the whole document (all fields) in MongoDB?
- Which is the fastest implementation of Python