# Finding percentage of intersection in polygon

For two given polygons generated in this code, how do I find the percentage of intersection on the rectangle? [The polygon was created using the Alphashape function]

(Linked to Finding points of intersection in polygon? and similar to this question answered: Returning percentage of area of polygon intersecting another polygon using shapely but have not used Shapely to generate polygon for the distribution)

Code implementation:

``````import sys
from descartes import PolygonPatch
import matplotlib.pyplot as plt
import alphashape
import numpy as np
x = np.random.uniform(41.03641664650488, 59.06406419360736, 1000)
y = np.random.uniform(100.96028140425065, 118.84533708184047, 1000)
test = np.vstack((x,y)).T
alpha_shape = alphashape.alphashape(test, 0.)
fig = plt.figure(1, figsize=(12, 5))
ax.scatter(*zip(*test))
circle = plt.Circle((50,100), 4, color='r', fill=True)
plt.autoscale()
plt.show()
``````

Alphashape returns a Shapely polygon, so if you convert the Matplotlib circle into a Polygon you can take advantage of Shapely's intersection function.

``````circle = plt.Circle((50,100), 4, color='r', fill=True)
circle_basepath = circle.get_path()

# Matplotlib's get_path on the circle just gives a circle with radius 1 at the origin,
# so circle_basepath needs to be transformed in the same way as your original Circle
circle_transform = circle.get_transform()
circle_path = circle_transform.transform_path(circle_basepath)

# convert to a Shapely polygon
circle_polygon = asPolygon(circle_path.vertices).buffer(0)

# create intersection and calculate percentage intersection based on areas
intersection = alpha_shape.intersection(circle_polygon)
percent_area = intersection.area / alpha_shape.area * 100
``````