How to Calculate the Intersection Area of Two 2D Polyline Shapes #901
Closed
zhuochengs
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, Thanks a lot first!
I have an application scenario where I need to calculate whether two shapes intersect and the area of the intersecting part。
Here is my code:
`
dxfPath = 'D:\test.dxf'
dxfNewPath = 'D:\testNew.dxf'
doc = ezdxf.readfile(dxfPath)
msp = doc.modelspace()
count = 0
p1 = []
p2 = []
for entity in msp.query("LWPOLYLINE POLYLINE"):
count += 1
path = make_path(entity)
vertices = Vec2.list(path.flattening(0.01))
if len(p1) == 0:
p1 = vertices
else:
p2 = vertices
polygon = shapely.geometry.Polygon(vertices)
area = polygon.area
perimeter = polygon.length
print(f"Polygon #{count}: Area = {area:.3f}, Perimeter = {perimeter:.3f}")
print(f"Total number of polygons encountered: {count}")
res = []
newP = []
INTERSECTION_POINTS = "INTERSECTION_POINTS"
res = intersect_polylines_2d(p1, p2, -10)
if len(res) == 0:
print('intersect_polylines_2d: 0')
else:
for i in p1:
# if (round(i.x, 0) == -673 or round(i.x, 0) == -467) and round(i.y, 0) == -266:
# print(i)
if is_point_in_polygon_2d(i, p2, -10) > -1:
newP.append(i)
for j in p2:
if is_point_in_polygon_2d(j, p1, -10) > -1:
newP.append(j)
for r in res:
newP.append(r)
newP.sort()
npolygon = shapely.geometry.Polygon(newP)
print('npolygon.area:',npolygon.area)
nP = msp.add_polyline2d(newP)
nP.close(True)
doc.saveas(dxfNewPath)
`
But the area obtained by the above code is not accurate, is it because my points cannot form a complete path?
Beta Was this translation helpful? Give feedback.
All reactions