Python-根据X和Y计算网格及其中所有可能的节点

问题描述 投票:0回答:1

嗨,所以这可能是数学问题,它困扰着我哈哈,我无法弄清楚e_e

所以我的程序接收了X1, Y1, X2, Y2(基本上是一个Box)我正在尝试将盒子分成5x5

像这样,enter image description here左上角是X1 Y1,左下角是X2 Y2。

所有红色点都是我要查找的节点

这是我计算给定X1 Y1 X2 Y2的值的代码

x1 = searchBox[0][0]
y1 = searchBox[0][1]
x2 = searchBox[1][0]
y2 = searchBox[1][1]

xIncrement = round((x2 - x1) / 5)
yIncrement = round((y2 - y1) / 5) 


newX = x1
newY = y1
while newX < x2 and newY < y2:
    newX = newX + xIncrement
    newY = newY + yIncrement

    pyautogui.click(newX, newY)

pyautogui.click()用于单击Pain女士,在这里我可以在图像上绘制红点。

这是我得到的结果:(enter image description here

有人可以帮我这个忙吗?我不太擅长数学问题:(

谢谢!

python math grid
1个回答
0
投票

代码段的问题是您要同时遍历x和y坐标,因此只能得到对角线。

我认为这里没有嵌套循环的方法。我附上了一个使用numpy的示例,该示例将您的点坐标绘制为矩阵:

import numpy as np

# lower left corner:
x0 = 0.
y0 = 0.
# upper right corner point:
x1 = 1.
y1 = 1.

n_x = 5
n_y = 5

points_x = np.zeros((n_x+1,n_y+1))
points_y = np.zeros((n_x+1,n_y+1))

for ii in range(0,n_x+1):
    for jj in range(0,n_y+1):
        points_x[ii,jj] = ( x1 - x0 ) / n_x*ii
        points_y[ii,jj] = ( y1 - y0 ) / n_y*jj

print "x-coordinates: \n"        
print points_x
print "y-coordinates: \n"
print points_y

将产生以下输出:

x-coordinates: 

[[ 0.   0.   0.   0.   0.   0. ]
 [ 0.2  0.2  0.2  0.2  0.2  0.2]
 [ 0.4  0.4  0.4  0.4  0.4  0.4]
 [ 0.6  0.6  0.6  0.6  0.6  0.6]
 [ 0.8  0.8  0.8  0.8  0.8  0.8]
 [ 1.   1.   1.   1.   1.   1. ]]
y-coordinates: 

[[ 0.   0.2  0.4  0.6  0.8  1. ]
 [ 0.   0.2  0.4  0.6  0.8  1. ]
 [ 0.   0.2  0.4  0.6  0.8  1. ]
 [ 0.   0.2  0.4  0.6  0.8  1. ]
 [ 0.   0.2  0.4  0.6  0.8  1. ]
 [ 0.   0.2  0.4  0.6  0.8  1. ]]
© www.soinside.com 2019 - 2024. All rights reserved.