How do I find the line segments formed by the meeting of two sides of two polygons?

48 Views Asked by At

I need a function that, given two or more polygons, can divide the sides into the intersections of the sides as in the figure, how would I do it?

An example would be the meeting of two sides [(0.0, 0.0), (0.0, 10.0)] [(0.0, 5.0), (0.0, 7.0)] which should result in 3 segments " [[(0.0, 0.0), (0.0, 5.0)], [(0.0, 5.0), (0.0, 7.0)], [(0.0, 7.0), (0.0, 10.0)]] "

enter image description here

I tried with two false polygons with only 1 side for the test, but without success.


from shapely.geometry import LineString

def encontrar_segmentos_de_reta(poligono1, poligono2):
    segmentos = []
    for i in range(len(poligono1)):
        p1 = poligono1[i]
        p2 = poligono1[(i + 1) % len(poligono1)]
        for j in range(len(poligono2)):
            q1 = poligono2[j]
            q2 = poligono2[(j + 1) % len(poligono2)]
            if intersecta(p1, p2, q1, q2):
                intersecao = encontrar_intersecao(p1, p2, q1, q2)
                if intersecao:
                    segmentos.append([p1, intersecao])
                    segmentos.append([intersecao, p2])
                    segmentos.append([q1, intersecao])
                    segmentos.append([intersecao, q2])
    return segmentos

def intersecta(p1, p2, q1, q2):
    line1 = LineString([p1, p2])
    line2 = LineString([q1, q2])
    return line1.intersects(line2)

def encontrar_intersecao(p1, p2, q1, q2):
    line1 = LineString([p1, p2])
    line2 = LineString([q1, q2])
    intersection = line1.intersection(line2)
    if intersection.is_empty:
        return None
    elif intersection.geom_type == 'Point':
        return list(intersection.coords)
    elif intersection.geom_type == 'LineString':
        return list(intersection.coords)[0]  # Use o primeiro ponto da linha

def RemoverRepetidos(paredes):
    novo_paredes = []
    for segmento in paredes:
        if segmento[0]!=segmento[1]:
            novo_paredes.append(segmento)

    return novo_paredes


# Definindo os polígonos de entrada
poligono1 = [(0.0, 0.0), (0.0, 10.0)]
poligono2 = [(0.0, 5.0), (0.0, 7.0)]

# Encontrar segmentos de reta formados pela interseção dos lados dos polígonos
segmentos = encontrar_segmentos_de_reta(poligono1, poligono2)
segmentos = RemoverRepetidos(segmentos)
print("Segmentos de reta formados:", segmentos)
1

There are 1 best solutions below

0
Pieter On

I don't quite understand your code or your example, but I think (hope?) the following code sample shows (the principle of) how you can accomplish what you need:

from matplotlib import pyplot as plt
import matplotlib.colors as mcolors
import shapely
from shapely.plotting import plot_line, plot_polygon

poly1 = shapely.box(2, 0, 4, 3)
poly2 = shapely.box(0, 1, 2, 2)

lines = []
# Intersecting lines
intersecting_lines = poly1.boundary.intersection(poly2.boundary)
lines.extend(shapely.get_parts(shapely.line_merge(intersecting_lines)))

# Non intersecting boundaries
lines.extend(
    shapely.get_parts(shapely.line_merge(poly1.boundary.difference(intersecting_lines)))
)
lines.extend(
    shapely.get_parts(shapely.line_merge(poly2.boundary.difference(intersecting_lines)))
)

# Plot
fig, ax = plt.subplots(ncols=2, figsize=(15, 15))
plot_polygon(poly1, ax=ax[0], color="red")
plot_polygon(poly2, ax=ax[0])

colors = []
for line, color in zip(lines, mcolors.TABLEAU_COLORS):
    plot_line(line, ax=ax[1], color=color)

plt.show()

Plotted image with input left, output right:

enter image description here