# 具有Matplotlib楔形的Shapely多边形的交集

from shapely.geometry import LineString
from matplotlib.patches import Wedge
from matplotlib import pyplot as plt
from descartes.patch import PolygonPatch

width = 5
rich = 1

0, 360, 3)

fig, ax = plt.subplots()

ax.plot(0, 0, 'xr')
plt.autoscale()

coords = [
[0, 0],
[0, 1],
[0, 2],
[1, 2],
[2, 2]
]

stick = LineString(coords)

stick_patch = PolygonPatch(stick.buffer(0.5))

x, y = stick.xy
ax.plot(x, y, 'r-', zorder=1)

plt.show()

area = stick.buffer(0.5).intersection(circle_patch).area

PS必须是环形，而不是圆形

import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from shapely.geometry import LineString, Point

outer_circle = Point(0, 0).buffer(4)
inner_circle = Point(0, 0).buffer(1)
wedge = outer_circle.difference(inner_circle)

stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)

intersection = buffered_stick.intersection(wedge)

wedge_patch = PolygonPatch(wedge)
stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')

fig, ax = plt.subplots()
plt.autoscale()

If, for some reason, this is not possible, and you have to work with the Matplotlib's Wedge, then I can think of two ways to get its intersection area with Shapely's polygon. In both of them, I convert the patches to Shapely polygons first. You probably can't get intersection area using only Matplotlib.

1) Using .get_path() method on the Matplotlib's patch from which you can extract vertices as a NumPy array and convert it to a Shapely polygon using asPolygon:

import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from matplotlib.patches import Wedge
from shapely.geometry import asPolygon, LineString

wedge_patch = Wedge(center=(0, 0),
r=4,
theta1=0,
theta2=360,
width=3)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)

wedge_path = wedge_patch.get_path()
wedge_polygon = asPolygon(wedge_path.vertices).buffer(0)
intersection = buffered_stick.intersection(wedge_polygon)

stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')

fig, ax = plt.subplots()
plt.autoscale()

Note the buffer(0) which I apply to the wedge polygon. This is a common trick in Shapely to make a valid polygon out of an invalid. In your answer you do something similar when removing zeros from ring_coords.

2）通过访问Wedge属性centerrwidth，并使用它们来重新创建多边形：

import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from matplotlib.patches import Wedge
from shapely.geometry import LineString, Point

wedge_patch = Wedge(center=(0, 0),
r=4,
theta1=0,
theta2=360,
width=3)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)

outer_circle = Point(wedge_patch.center).buffer(wedge_patch.r)
inner_circle = Point(wedge_patch.center).buffer(wedge_patch.r - wedge_patch.width)
wedge_polygon = outer_circle.difference(inner_circle)
intersection = buffered_stick.intersection(wedge_polygon)

stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')

fig, ax = plt.subplots()
plt.autoscale()

>>> intersection.area
3.3774012986988513  # 1st case
3.3823210603713694  # 2nd case and the original without Matplotlib

0 条评论