2

如何在python中栅格化多边形?例如我有这个多边形:

[(20, 13), (21, 12), (20, 12), (19, 12), (21, 13)]

这是一个边界,我需要在其中找到所有点(图块)。如何在没有任何外部包的情况下使用 python 做到这一点?谢谢。

4

1 回答 1

0

快捷方式:

def raster(poly):
    for index in range(len(poly) - 1):
        t1 = poly[index]
        t2 = poly[index + 1]
        xdef = t1[0] - t2[0]
        ydef = t1[1] - t2[1]
        if abs(xdef) > 1:
            lo = min(t1[0], t2[0])
            hi = max(t1[0], t2[0])
            j = lo
            while(j <= hi):
                new = (j, t1[1])
                poly.append(new)
                j += 1
        if abs(ydef) > 1:
            lo = min(t1[1], t2[1])
            hi = max(t1[1], t2[1])
            j = lo
            while(j <= hi):
                new = (t1[0], j)
                poly.append(new)
                j += 1
    return poly
于 2014-01-24T19:37:05.497 回答