Search code examples
pythonnumpyscipycomputational-geometry

Generating equidistance points along the boundary of a polygon but CW/CCW


Suppose I have the vertices of a polygon and they are all oriented CCW. I wish to generate n equidistance points along the boundary of this polygon. Does anyone know of any existing package that does this, and if not, an algorithm one can use? I am working in Python. For example, here is what I would like if the polygon in question is a rectangle:

enter image description here


Solution

  • shapely:

    import shapely.geometry as sg
    import shapely.affinity as sa
    import matplotlib.pyplot as P
    import numpy as np
    
    n = 7
    k = 11
    ori = sg.Point([0,0])
    p = [sg.Point([0,1])]
    for j in range(1,n):
        p.append(sa.rotate(p[-1],360/n,origin=ori))
    ngon = sg.Polygon(p)
    P.figure()
    P.plot(*ngon.exterior.xy,"-k")
    P.scatter(*np.transpose([ngon.exterior.interpolate(t).xy for t in np.linspace(
        0,ngon.length,k,False)])[0])
    P.axis("equal");P.box("off");P.axis("off")
    P.show(block=0)
    

    enter image description here