如何计算最接近点x,y的每个网格框的中心点。这是生成网格的代码。我想保存列表中心的所有中心点,然后找到最近的中心点。这是代码。
from PIL import Image, ImageDraw
if __name__ == '__main__':
height = 600
width = 600
centers=[]
image = Image.new(mode='L', size=(height, width), color=255)
# Draw some lines
draw = ImageDraw.Draw(image)
y_start = 0
y_end = image.height
step_size = int(image.width / 10)
for x in range(0, image.width, step_size):
line = ((x, y_start), (x, y_end))
draw.line(line, fill=128)
x_start = 0
x_end = image.width
for y in range(0, image.height, step_size):
line = ((x_start, y), (x_end, y))
draw.line(line, fill=128)
del draw
image.show()
我发现像这样的质心
for x in range(0, image.width, step_size):
for y in range(0, image.height, step_size):
centers.append([(x+(x+step_size))/2,(y+(y+step_size))/2])
如何找到靠近鼠标点击获得的(x,y)点的质心?
答案 0 :(得分:1)
有人可以改进这一点,但内置you can use the min function用于python。我把它扔在一起:
For i = 1 to LastRow
Command.CommandText = "INSERT INTO [TABLE] [Col1] VALUES ('" & Sheets("Sheet1").Cells(i, 1).Value & "')"
Next i
虽然有点笨拙,但这会在列表# Simulated mouse click
x_click = 542
y_click = 253
find_closest_x = min(centers, key=lambda x_center: abs(x_click - x_center[0]))
find_closest_y = min(centers, key=lambda y_center: abs(y_click - y_center[1]))
closest = find_closest_x[0], find_closest_y[1]
print(closest)
中找到x和y的最接近值
答案 1 :(得分:1)
有关欧几里德距离的同一问题被问到here。在您的情况下,这将是这样的:
<script type="application/javascript" src="bootstrap-wysiwyg.js"></script>