[Python] How do determine inner border points of polygon intersection

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a Python script that will return me all vertice locations (x,y) of inner intersecting edge between a square tile and a closed polygon with n-vertices. I got this far with my code, but am getting wrong inner border vertices. Is there an algorithm of somekind specificaly to determine inside edges, so I know in which direction to search for.

    from shapely.geometry import Polygon, LineString, MultiLineString
    import matplotlib.pyplot as plt
    from matplotlib.patches import Polygon as MplPolygon

    square_coords = [
    (53906250, 35456250), # lt
    (54296875, 35456250), # rt
    (54296875, 35546875), # rb
    (53906250, 35546785) # lb
    ]

    polygon_coords = [
    (54340742, 35518900),
    (53997222, 35518800),
    (53997298, 35183478),
    (54340666, 35183478),
    (54340742, 35518890)
    ]

    square = Polygon(square_coords)
    polygon = Polygon(polygon_coords)

    intersection = square.intersection(polygon)

    fig, ax = plt.subplots()
    ax.set_aspect('equal')

    square_patch = MplPolygon(list(square.exterior.coords), color='blue', alpha=0.4, label='Square')
    ax.add_patch(square_patch)

    polygon_patch = MplPolygon(list(polygon.exterior.coords), color='green', alpha=0.4, label='Polygon')
    ax.add_patch(polygon_patch)

    if not intersection.is_empty:
    if intersection.geom_type == 'Polygon':
    inter_coords = list(intersection.exterior.coords)
    inter_patch = MplPolygon(inter_coords, color='red', alpha=0.6, label='Intersection')
    ax.add_patch(inter_patch)
    elif intersection.geom_type == 'MultiPolygon':
    for part in intersection.geoms:
    inter_coords = list(part.exterior.coords)
    inter_patch = MplPolygon(inter_coords, color='red', alpha=0.6, label='Intersection')
    ax.add_patch(inter_patch)

    polygon_boundary = polygon.boundary
    intersection_boundary = intersection.boundary

    inner_border = polygon_boundary.intersection(intersection_boundary)

    if not inner_border.is_empty:
    if isinstance(inner_border, (LineString, MultiLineString)):
    if isinstance(inner_border, LineString):
    lines = [inner_border]
    else:
    lines = list(inner_border.geoms)

    for line in lines:
    x, y = line.xy
    ax.plot(x, y, color='black', linewidth=2, label='Inner Border')
    ax.scatter([x[0], x[-1]], [y[0], y[-1]], color='purple', s=40, zorder=10)
    ax.text(x[0], y[0], f"({x[0]:.5f}, {y[0]:.5f})", fontsize=7, color='purple')
    ax.text(x[-1], y[-1], f"({x[-1]:.5f}, {y[-1]:.5f})", fontsize=7, color='purple')

    ax.set_title("Polygon-Square Intersection & Inner Border")
    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.grid(True)
    ax.legend()
    plt.tight_layout()
    plt.show()


    Two examples of what I am trying to achieve. My goal is to get all intersecting vertices (x,y) on yellow border.

    [​IMG]

    [​IMG]

    Continue reading...

Compartilhe esta Página