将一个矩形分成 NxM 个矩形并在 Python 中对它们进行编号
Dividing a rectangle into NxM rectangle and numbering them in Python
所以我有一个大小为 104x68 的矩形,并将该矩形分成 36 个相等的部分。图像中的数字代表每个小矩形的坐标位置。
现在我想给每个矩形编号,如下图所示:
即
For coordinate location x = [0, 17.3] and y = [0, 11.3] will be number 1 box
For coordinate location x = (17.3, 34.6] and y = [0, 11.3] will be number 2 box
....
For coordinate location x = (86.6, 104] and y = (56.6, 68] will be number 36 box
在对每个框进行编号后,我希望:给定一个值,例如 (54,35),程序将生成该特定值的框号,对于给定值 (54, 35),框号将是22.
在Python中解决这个问题的优化方法是什么?有人可以帮忙吗?
不是最优化的方式(您可以使用 itertools
),但一个相当紧凑的方式就是使用 numpy.arange
。假设这是您的实际任务,我不明白为什么该方法需要比这更优化。
import numpy as np
SMALL_NUM = 1e-5
def range_pairs(max_value, divisions):
division_length = max_value/divisions
range_values = list(np.arange(0, max_value+SMALL_NUM, division_length))
return [(range_values[i], range_values[i+1]) for i in range(divisions)]
i = 0
for y in range_pairs(68, 6):
for x in range_pairs(104, 6):
i += 1
print(f"For coordinate location x = {x} and y = {y} will be number {i} box")
对于更大的任务,考虑itertools
,同时考虑pre-calculating下面的range_pairs
,或者在range_pairs
函数上放一个lru_cache
。
在下文中,您可以通过输入所需的参数来获取矩形编号。 weight
和 height
显示矩形的尺寸(在您的示例中,width = 104, height = 68
)。 xpartition
和 ypartition
参数显示每个维度的划分数(在您的示例中,36 等分表示 xpartition = 6, ypartition = 6
)。 xinput
和 yinput
是查询点的维度,需要知道其在指定分区中的矩形数(在您的示例中,xinput = 54, yinput = 35
)。
import math
def get_rect_num(width, height, xpartition, ypartition, xinput, yinput):
x_step = width / xpartition
y_step = height / ypartition
x = math.ceil((xinput if xinput > 0 else 0.5) / x_step) # handle border cases as well
y = math.ceil((yinput if yinput > 0 else 0.5) / y_step) # handle border cases as well
return (y-1) * xpartition + x
get_rect_num(104, 68, 6, 6, 54, 35)
#> 22
基于四个主要操作,上述计算的时间复杂度在Theta(1)
。
所以我有一个大小为 104x68 的矩形,并将该矩形分成 36 个相等的部分。图像中的数字代表每个小矩形的坐标位置。
现在我想给每个矩形编号,如下图所示:
即
For coordinate location x = [0, 17.3] and y = [0, 11.3] will be number 1 box
For coordinate location x = (17.3, 34.6] and y = [0, 11.3] will be number 2 box
....
For coordinate location x = (86.6, 104] and y = (56.6, 68] will be number 36 box
在对每个框进行编号后,我希望:给定一个值,例如 (54,35),程序将生成该特定值的框号,对于给定值 (54, 35),框号将是22.
在Python中解决这个问题的优化方法是什么?有人可以帮忙吗?
不是最优化的方式(您可以使用 itertools
),但一个相当紧凑的方式就是使用 numpy.arange
。假设这是您的实际任务,我不明白为什么该方法需要比这更优化。
import numpy as np
SMALL_NUM = 1e-5
def range_pairs(max_value, divisions):
division_length = max_value/divisions
range_values = list(np.arange(0, max_value+SMALL_NUM, division_length))
return [(range_values[i], range_values[i+1]) for i in range(divisions)]
i = 0
for y in range_pairs(68, 6):
for x in range_pairs(104, 6):
i += 1
print(f"For coordinate location x = {x} and y = {y} will be number {i} box")
对于更大的任务,考虑itertools
,同时考虑pre-calculating下面的range_pairs
,或者在range_pairs
函数上放一个lru_cache
。
在下文中,您可以通过输入所需的参数来获取矩形编号。 weight
和 height
显示矩形的尺寸(在您的示例中,width = 104, height = 68
)。 xpartition
和 ypartition
参数显示每个维度的划分数(在您的示例中,36 等分表示 xpartition = 6, ypartition = 6
)。 xinput
和 yinput
是查询点的维度,需要知道其在指定分区中的矩形数(在您的示例中,xinput = 54, yinput = 35
)。
import math
def get_rect_num(width, height, xpartition, ypartition, xinput, yinput):
x_step = width / xpartition
y_step = height / ypartition
x = math.ceil((xinput if xinput > 0 else 0.5) / x_step) # handle border cases as well
y = math.ceil((yinput if yinput > 0 else 0.5) / y_step) # handle border cases as well
return (y-1) * xpartition + x
get_rect_num(104, 68, 6, 6, 54, 35)
#> 22
基于四个主要操作,上述计算的时间复杂度在Theta(1)
。