check if coordinates are overlapping using numpy, matplotlib
I need to check whether a square is overlapping with a defined polygon
Yes, it can be easily done using shapely as below
from shapely.geometry.polygon import Polygon
from shapely.geometry import box
minx,miny,maxx,maxy=0,0,1,1
b = box(minx,miny,maxx,maxy)
polygon = Polygon([(230,478),(500,478),(432,154),(308,154)])
print(polygon.contains(b))
Any alternate ways to achieve the same results using NumPy,matplotlib?
(fyi:shapely is not approved to use)
You can (almost) do that with a few more steps. This runnable code shows all the steps with resulting plot.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
vts = np.array([(230,478),(500,478),(432,154),(308,154)])
pgon = patches.Polygon(vts, color="blue", alpha=0.5)
# 2 points defining a box
point1 = (300,357) # upper-right point: (xmax,ymax)
point2 = (250,250) # lower-left point: (xmin,ymin)
# plot the polygon patch
fig,ax = plt.subplots()
ax.add_patch(pgon)
# check if the 2 points are inside the polygon patch?
pts_inside = pgon.contains_points(ax.transData.transform([point1, point2]))
print(pts_inside) # get [ True False] output
# plot points
ax.scatter(point1[0],point1[1], color="red", zorder=6) # point1 inside
ax.scatter(point2[0],point2[1], color="green", zorder=6) # point2 outside
plt.show()
From the above code, you get
pts_inside
which contains vaues: [ True, False]. That means only point 1 is inside the polygon. If you get [True, True], both points are inside the polygon, but the rectangle box defined by the points may not.