У меня есть код подразделения, который делит прямоугольник на два с помощью ограничительной рамки.
def subdivision(block):
##bounding box
mina = np.min(block, axis=0)
maxa = np.max(block, axis=0)
diff = (maxa - mina) * 0.5
center = mina + diff
corners = np.array([center + [-diff[0], -diff[1]], center + [diff[0], -diff[1]], center + [diff[0], diff[1]],
center + [-diff[0], diff[1]], center + [-diff[0], -diff[1]]])
print(corners)
##divide bounding box from longer line
def calculateDistance(x1, y1, x2, y2):
dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return dist
def midpoint(x1, y1, x2, y2):
subpoint1 = (abs(x1) + abs(x2)) / 2
subpoint2 = (abs(y1) + abs(y2)) / 2
return np.r_[[subpoint1], [subpoint2]]
dist1 = calculateDistance(corners[0][0], corners[0][1], corners[1][0], corners[1][1])
dist2 = calculateDistance(corners[1][0], corners[1][1], corners[2][0], corners[2][1])
if (dist2 == 0):
print(corners[1][1], " ", corners[2][1])
elif (dist1 / dist2) >= 1:
subpoint1 = midpoint(corners[0][0], corners[0][1], corners[1][0], corners[1][1])
subpoint2 = midpoint(corners[2][0], corners[2][1], corners[3][0], corners[3][1])
else:
subpoint1 = midpoint(corners[1][0], corners[1][1], corners[2][0], corners[2][1])
subpoint2 = midpoint(corners[0][0], corners[0][1], corners[3][0], corners[3][1])
# subpoint=np.r_[[subpoint1], [subpoint2]]
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y
if dist1 / dist2 > 1:
inter1 = line_intersection((subpoint1, subpoint2), (block[3], block[2]))
inter2 = line_intersection((subpoint1, subpoint2), (block[0], block[1]))
else:
inter1 = line_intersection((subpoint1, subpoint2), (block[0], block[3]))
inter2 = line_intersection((subpoint1, subpoint2), (block[1], block[2]))
if dist1 / dist2 > 1:
subblockL = np.array([block[0], inter2, inter1, block[3], block[0]])
subblockR = np.array([inter2, block[1], block[2], inter1, inter2])
else:
subblockL = np.array([block[0], block[1], inter2, inter1, block[0]])
subblockR = np.array([inter1, inter2, block[2], block[3], inter1])
return subblockL, subblockR
Я делил деление на l oop до тех пор, пока наименьшая разделенная область не будет близка к 200.
A = np.array([[ 5.13, 6.17],[146.69, 10.89],[153.03, 29.91],[ 1.81, 28.08], [ 5.13, 6.1]])
min_land = 200
def gra(A):
b=[]
c=[]
for i in range(len(A)):
c=[]
for j in range(2):
c.append(A[i][j])
b.append(c)
return b
b=gra(A)
m=1
n=1
t=0
r=0
l_b=[b]
while area(subdivision(l_b[n-1])[m-1])>min_land:
for i in range(t,n):
l_b.append(gra(subdivision(np.array(l_b[i]))[0]))
l_b.append(gra(subdivision(np.array(l_b[i]))[1]))
t=2**r
r=r+1
n=2**r
t=[]
for i in range(len(l_b)):
t.append(area(l_b[i]))
v = []
for j in range(len(l_b)):
if t[j]<min_land:
v.append(t[j])
new_list = [array for array in t if array not in v]
p = len(new_list)
n = math.log(p)/math.log(2)
n = int(n)
s=0
for i in range(n-1):
s = (2**i)+s
s = p-s
s=0
for i in range(n-1):
s = (2**i)+s
s = p-s
l_b
Поскольку область B составляет около 1990 года, а наименьший предел области равен 200, я ожидается 7-8 прямоугольников, но я получаю только 5 вертикально разделенных результатов. Как я могу исправить это, чтобы я мог иметь то, что изображение представляет?
что я получаю прямо сейчас:
что я хочу получить: