import numpy as np

import vtk

import matplotlib.pyplot as plt

import matplotlib

matplotlib.use('TkAgg')



def create_vtk_polygon(polygon):

"""Create a real vtkPolygon from a 2D list of (x, y) coordinates."""

vtk_points = vtk.vtkPoints()

for x, y in polygon:

vtk_points.InsertNextPoint(x, y, 0)

vtk_polygon = vtk.vtkPolygon()

vtk_polygon.GetPoints().DeepCopy(vtk_points)

vtk_polygon.GetPointIds().SetNumberOfIds(len(polygon))

for i in range(len(polygon)):

vtk_polygon.GetPointIds().SetId(i, i)

return vtk_polygon



# VTK’s API has been a bit inconsistent between releases, and unfortunately the Python wrapper

# for vtkLine.Intersection() is different than the C++ signature,

# and also different from the pure vtkLine.IntersectWithLine() method.

def robust_vtk_intersections(polygon, y_line):

vtk_poly = create_vtk_polygon(polygon)

n = len(polygon)

intersections = []

tol = 1e-8

for i in range(n):

x1, y1, _ = vtk_poly.GetPoints().GetPoint(i)

x2, y2, _ = vtk_poly.GetPoints().GetPoint((i+1)%n)



p0 = (x1 - 1e4, y_line, 0.0)

p1 = (x2 + 1e4, y_line, 0.0)

edge_p0 = (x1, y1, 0.0)

edge_p1 = (x2, y2, 0.0)



line = vtk.vtkLine()

t = [0.0]

x = [0.0, 0.0, 0.0]

pcoords = [0.0, 0.0, 0.0]

subId = [0]



result = line.IntersectWithLine(edge_p0, edge_p1, tol, t, x, pcoords, subId)

if result == 1:

# t is the parametric distance on the edge

if 0 <= t[0] <= 1:

intersections.append(x[0])



if y1 == y_line:

y_prev = polygon[i-1][1]

y_next = y2

if (y_prev - y_line) * (y_next - y_line) < 0:

intersections.append(x1)

return sorted(set(intersections))















def all_intersections(polygon, y_line):

"""Naive version: includes edge/vertex tangents (not for real infill!)"""

xs = []

n = len(polygon)

for i in range(n):

p1 = polygon[i]

p2 = polygon[(i+1)%n]

y1, y2 = p1[1], p2[1]

x1, x2 = p1[0], p2[0]

if (y1 - y_line) * (y2 - y_line) <= 0:

if y1 == y2: continue # ignore horizontal edge on line

x = x1 + (x2 - x1) * (y_line - y1) / (y2 - y1)

xs.append(x)

return sorted(xs)



def plot_vtk_vs_naive(polygons, y_line):

fig, axes = plt.subplots(1, 2, figsize=(18, 7))

for ax, method, title in zip(

axes,

[all_intersections, robust_vtk_intersections],

["Naive (all_intersections)", "Robust (Real vtkPolygon & vtkLine)"]

):

colors = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6']

for idx, poly in enumerate(polygons):

poly_np = np.array(poly)

ax.plot(*np.append(poly_np, [poly_np[0]], axis=0).T, '-', color=colors[idx], lw=2)

xs = method(poly, y_line)

ys = [y_line]*len(xs)

ax.plot(xs, ys, 'ro', markersize=8)

ax.axhline(y_line, color='k', ls='--')

ax.set_title(title)

ax.set_aspect('equal')

ax.grid(True)

plt.show()



# Your polygons and y_line

polygons = [

[(2,1), (4,1), (4,4), (2,4)],

[(6,1), (9,1), (7.5,3), (9,5), (6,5)],

[(11,1), (13,1), (13,4), (12.5,5), (11,4)],

[(2,7), (3,8), (4,8), (11,8), (10,2), (10,6), (5,6), (5,4)],

[(15,4), (15,8), (25,8), (25,4), (23,7), (21,4), (19,7)],

[(17,1), (18,5), (19,2), (23,4), (23,1)],

[(1,0), (1,9), (26,9), (26,0)],

]

y_line = 4



plot_vtk_vs_naive(polygons, y_line)







    result = line.IntersectWithLine(edge_p0, edge_p1, tol, t, x, pcoords, subId)

             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

TypeError: IntersectWithLine argument 4: must be real number, not list