Python 2.7 在python中将基于正方形的多边形拆分为更小的正方形区域

Python 2.7 在python中将基于正方形的多边形拆分为更小的正方形区域,python-2.7,Python 2.7,我在坐标网格中有一个类似这样的多边形: [(0,0)、(0,1)、(0,2)、(0,3)、(1,0)、(1,1)、(1,2)、(1,3)、(2,2)、(2,3)] 在Python2.7中,是否有任何方法可以将该多边形划分为任意边长的较小正方形区域(作为参数传递)?基本上我需要的是把正方形的多边形分成更小的正方形区域 任何帮助都将不胜感激 这可以通过迭代坐标列表并确定是否能够生成正方形(所有内部坐标都在坐标列表中)来完成 下面的代码将遍历列表中的坐标,并检查它们是否可以变成完整的正方形。它假设坐标

我在坐标网格中有一个类似这样的多边形: [(0,0)、(0,1)、(0,2)、(0,3)、(1,0)、(1,1)、(1,2)、(1,3)、(2,2)、(2,3)]

在Python2.7中,是否有任何方法可以将该多边形划分为任意边长的较小正方形区域(作为参数传递)?基本上我需要的是把正方形的多边形分成更小的正方形区域


任何帮助都将不胜感激

这可以通过迭代坐标列表并确定是否能够生成正方形(所有内部坐标都在坐标列表中)来完成

下面的代码将遍历列表中的坐标,并检查它们是否可以变成完整的正方形。它假设坐标列表先按x排序,然后按y排序(其中坐标是(x,y)对)

它的工作原理是将新值添加到网格列表并在其中进行迭代

返回2个1x1正方形的网格示例:

[(0,0),(0,1),(0,2),
 (1,0),(1,1),(1,2)]
功能:

import math
import numpy

def getArray(grid):
    n = numpy.zeros((grid[-1][0]+1, grid[-1][1]+1))
    for (y,x) in grid:
        n[y, x] = 1
    return n

# Determines if the new point is within the bounds
def getBoundingSquare(newCoord, npArr):
    try:
        if npArr[int(math.floor(newCoord[0])),int(math.floor(newCoord[1]))] == 1 and \
        npArr[int(math.floor(newCoord[0])),int(math.ceil(newCoord[1]))] == 1 and \
        npArr[int(math.ceil(newCoord[0])),int(math.floor(newCoord[1]))] == 1 and \
        npArr[int(math.ceil(newCoord[0])),int(math.ceil(newCoord[1]))] == 1:
            return 1
        else:
            return 0
    except IndexError:
        return 0

# Creates the new points using the desired side length
def interpolator(grid, side_length):
    startCorner = grid[0]
    endCorner = grid[-1]
    npArr = getArray(grid)
    newGrid = []
    if side_length < 1:
        exprY = int((endCorner[0]+1)*1//side_length-1)
        exprX = int((endCorner[1]+1)*1//side_length-1)
    else:
        exprY = int((endCorner[0]+1))
        exprX = int((endCorner[1]+1))
    for y in range(startCorner[0], exprY):
        for x in range(startCorner[1], exprX):
            newCoord = (y*side_length+startCorner[0], x*side_length+startCorner[1])
            newCoord2 = (float(y+startCorner[0]), float(x+startCorner[1]))
            if getBoundingSquare(newCoord, npArr):
                newGrid.append(newCoord)
            if getBoundingSquare(newCoord2, npArr) and newCoord2 not in newGrid:
                newGrid.append(newCoord2)
    newGrid.sort()
    return newGrid

def subdivide(grid, side_length):
    grid = interpolator(grid, float(side_length))
    subSquares = []
    while len(grid) >= 4:
        sy, sx = grid[0]
        if (sy+side_length, sx+side_length) in grid:
            square = []
            for y in range(2):
                for x in range(2):
                    if (sy+y*side_length, sx+x*side_length) in grid:
                        square.append((sy+y*side_length, sx+x*side_length))
                        if not(y == 1 or x == 1):
                            grid.remove((sy+y*side_length, sx+x*side_length))

            if square not in subSquares and (len(square) == (side_length+1)**2 or len(square) == 4):
                subSquares.append(square)
            (startY, startX) = square[0]
            (endY, endX) = square[-1]
            counter = 0
            while counter < len(grid):
                item = grid[counter]
                if (item[0] < endY and item[1] < endX):
                    grid.remove(item)
                else:
                    counter += 1
        else:
            grid.pop(0)
    allowed = 0
    for item in grid:
        for square in subSquares:
            if item in square:
                allowed += 1
                continue
    if len(grid) > allowed:
        print 'Could not divide entire polygon'
    for square in subSquares:
        print square
    return subSquares
导入数学
进口numpy
def getArray(网格):
n=numpy.zero((网格[-1][0]+1,网格[-1][1]+1))
对于网格中的(y,x):
n[y,x]=1
返回n
#确定新点是否在边界内
def getBoundingSquare(纽科德,npArr):
尝试:
如果npArr[int(数学层(newCoord[0])),int(数学层(newCoord[1]))]==1和\
npArr[int(math.floor(newCoord[0]))、int(math.ceil(newCoord[1]))]==1和\
npArr[int(math.ceil(newCoord[0]))、int(math.floor(newCoord[1]))]==1和\
npArr[int(math.ceil(newCoord[0]))、int(math.ceil(newCoord[1]))]==1:
返回1
其他:
返回0
除索引器外:
返回0
#使用所需的边长创建新点
def插值器(网格、边长):
startCorner=grid[0]
endCorner=栅格[-1]
npArr=getArray(网格)
newGrid=[]
如果边长<1:
exprY=int((端角[0]+1)*1//side_length-1)
exprX=int((端角[1]+1)*1//side_length-1)
其他:
exprY=int((端点角[0]+1))
exprX=int((端角[1]+1))
对于范围内的y(startCorner[0],exprY):
对于范围内的x(startCorner[1],exprX):
newCoord=(y*边长+起始角[0],x*边长+起始角[1])
newCoord2=(浮点(y+startCorner[0]),浮点(x+startCorner[1]))
如果getBoundingSquare(newCoord,npArr):
追加(newCoord)
如果getBoundingSquare(newCoord2,npArr)和newCoord2不在newGrid中:
追加(newCoord2)
newGrid.sort()
返回新网格
def细分(网格、边长):
栅格=插值器(栅格、浮点(边长))
子正方形=[]
当len(网格)>=4时:
sy,sx=网格[0]
如果网格中有(sy+边长,sx+边长):
正方形=[]
对于范围(2)内的y:
对于范围(2)内的x:
如果网格中有(sy+y*边长,sx+x*边长):
正方形追加((sy+y*边长,sx+x*边长))
如果不是(y==1或x==1):
网格。移除((sy+y*侧长,sx+x*侧长))
如果正方形不在子正方形中且(len(square)=(边长+1)**2或len(square)==4):
子正方形附加(正方形)
(startY,startX)=平方[0]
(endY,endX)=平方[-1]
计数器=0
当计数器允许:
打印“无法分割整个多边形”
对于子正方形中的正方形:
印刷方格
返回子正方形

这不会返回重叠的正方形。这需要安装
numpy

这可以通过迭代坐标列表并确定是否能够生成正方形来完成(所有内部坐标都在坐标列表中)

下面的代码将遍历列表中的坐标,并检查它们是否可以变成完整的正方形。它假设坐标列表先按x排序,然后按y排序(其中坐标是(x,y)对)

它的工作原理是将新值添加到网格列表并在其中进行迭代

返回2个1x1正方形的网格示例:

[(0,0),(0,1),(0,2),
 (1,0),(1,1),(1,2)]
功能:

import math
import numpy

def getArray(grid):
    n = numpy.zeros((grid[-1][0]+1, grid[-1][1]+1))
    for (y,x) in grid:
        n[y, x] = 1
    return n

# Determines if the new point is within the bounds
def getBoundingSquare(newCoord, npArr):
    try:
        if npArr[int(math.floor(newCoord[0])),int(math.floor(newCoord[1]))] == 1 and \
        npArr[int(math.floor(newCoord[0])),int(math.ceil(newCoord[1]))] == 1 and \
        npArr[int(math.ceil(newCoord[0])),int(math.floor(newCoord[1]))] == 1 and \
        npArr[int(math.ceil(newCoord[0])),int(math.ceil(newCoord[1]))] == 1:
            return 1
        else:
            return 0
    except IndexError:
        return 0

# Creates the new points using the desired side length
def interpolator(grid, side_length):
    startCorner = grid[0]
    endCorner = grid[-1]
    npArr = getArray(grid)
    newGrid = []
    if side_length < 1:
        exprY = int((endCorner[0]+1)*1//side_length-1)
        exprX = int((endCorner[1]+1)*1//side_length-1)
    else:
        exprY = int((endCorner[0]+1))
        exprX = int((endCorner[1]+1))
    for y in range(startCorner[0], exprY):
        for x in range(startCorner[1], exprX):
            newCoord = (y*side_length+startCorner[0], x*side_length+startCorner[1])
            newCoord2 = (float(y+startCorner[0]), float(x+startCorner[1]))
            if getBoundingSquare(newCoord, npArr):
                newGrid.append(newCoord)
            if getBoundingSquare(newCoord2, npArr) and newCoord2 not in newGrid:
                newGrid.append(newCoord2)
    newGrid.sort()
    return newGrid

def subdivide(grid, side_length):
    grid = interpolator(grid, float(side_length))
    subSquares = []
    while len(grid) >= 4:
        sy, sx = grid[0]
        if (sy+side_length, sx+side_length) in grid:
            square = []
            for y in range(2):
                for x in range(2):
                    if (sy+y*side_length, sx+x*side_length) in grid:
                        square.append((sy+y*side_length, sx+x*side_length))
                        if not(y == 1 or x == 1):
                            grid.remove((sy+y*side_length, sx+x*side_length))

            if square not in subSquares and (len(square) == (side_length+1)**2 or len(square) == 4):
                subSquares.append(square)
            (startY, startX) = square[0]
            (endY, endX) = square[-1]
            counter = 0
            while counter < len(grid):
                item = grid[counter]
                if (item[0] < endY and item[1] < endX):
                    grid.remove(item)
                else:
                    counter += 1
        else:
            grid.pop(0)
    allowed = 0
    for item in grid:
        for square in subSquares:
            if item in square:
                allowed += 1
                continue
    if len(grid) > allowed:
        print 'Could not divide entire polygon'
    for square in subSquares:
        print square
    return subSquares
导入数学
进口numpy
def getArray(网格):
n=numpy.zero((网格[-1][0]+1,网格[-1][1]+1))
对于网格中的(y,x):
n[y,x]=1
返回n
#确定新点是否在边界内
def getBoundingSquare(纽科德,npArr):
尝试:
如果npArr[int(数学层(newCoord[0])),int(数学层(newCoord[1]))]==1和\
npArr[int(math.floor(newCoord[0]))、int(math.ceil(newCoord[1]))]==1和\
npArr[int(math.ceil(newCoord[0]))、int(math.floor(newCoord[1]))]==1和\
npArr[int(math.ceil(newCoord[0]))、int(math.ceil(newCoord[1]))]==1:
返回1
其他:
返回0
除索引器外:
返回0
#使用所需的边长创建新点
def插值器(网格、边长):
startCorner=grid[0]
endCorner=栅格[-1]
npArr=getArray(网格)
newGrid=[]
如果边长<1:
exprY=int((端角[0]+1)*1//side_length-1)
exprX=int((端角[1]+1)*1//side_length-1)
其他:
exprY=int((端点角[0]+1))
exprX=int((端角[1]+1))
对于范围内的y(startCorner[0],exprY):
对于范围内的x(startCorner[1],exprX):
newCoord=(y*边长+起始角[0],x*边长+起始角[1])
newCoord2=(浮动(y+startCorner[0]),浮动