首页 > 代码库 > 区域的个数(坐标离散化)

区域的个数(坐标离散化)

始终觉得秋叶拓哉书上那段代码,没有起到离散化的作用啊?估计是我智障吧。。。肯定是。

compress 后的 X,Y 坐标绘制的 field 和原来的地图一样啊。到底为森么呢?到底为森么呢?


import pprint

def compress( li1, li2, size, nums ):
    vec  = []
    vec1 = []
    
    for i in xrange( nums ):
        for d in xrange( -1, 2 ):
            temp1, temp2 = li1[i] + d, li2[i] + d
            if 1 <= temp1 <= size:
                vec.append( temp1 )
            if 1 <= temp2 <= size:
                vec.append( temp2 )

    vec.sort()
    
    for v in vec:
        if len( vec1 ) == 0:
            vec1.append( v )
        else:
            if vec1[-1] != v:
                vec1.append( v )

    li1[:] = map( lambda x: vec1.index( x ), li1 )
    li2[:] = map( lambda x: vec1.index( x ), li2 )

    return len( vec1 )


X1 = [1, 1,  4,  9, 10]
X2 = [6, 10, 4,  9, 10]
Y1 = [4, 8,  1,  1, 6]
Y2 = [4, 8,  10, 5, 10]

W = H = 10
N = 5

field = [ [ 0 for w in xrange( W ) ] for h in xrange( H ) ]

compress( X1, X2, W, N )
compress( Y1, Y2, H, N )

pprint.pprint( zip( zip( X1, Y1 ), zip( X2, Y2 ) ) )

for i in xrange( N ):
    for y in xrange( Y1[i], Y2[i] + 1 ):
        for x in xrange( X1[i], X2[i] + 1 ):
            field[y][x] = 1

for i in xrange( W ):
    for j in xrange( H ):
        print field[i][j],
    print


区域的个数(坐标离散化)