Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何解决视线检查产生的错误?_Python 3.x_Algorithm_Shortest Path_Path Finding - Fatal编程技术网

Python 3.x 如何解决视线检查产生的错误?

Python 3.x 如何解决视线检查产生的错误?,python-3.x,algorithm,shortest-path,path-finding,Python 3.x,Algorithm,Shortest Path,Path Finding,我正在使用视线算法解决路径规划问题,我在这里检查它的工作,但如下图所示,视线检查给出了错误的结果。黑色平铺是通过视线检查的路径,功能还应检查(1,1)上的平铺,但不是。如何修复这些错误 python代码 import numpy as np import matplotlib.pyplot as plt grid = [[0,0,0,0,0,0,0,0], [0,1,1,0,0,1,1,0], [0,0,0,0,0,0,0,0], [0,1,1

我正在使用视线算法解决路径规划问题,我在这里检查它的工作,但如下图所示,视线检查给出了错误的结果。黑色平铺是通过视线检查的路径,功能还应检查(1,1)上的平铺,但不是。如何修复这些错误

python代码

import numpy as np
import matplotlib.pyplot as plt

grid = [[0,0,0,0,0,0,0,0],
        [0,1,1,0,0,1,1,0],
        [0,0,0,0,0,0,0,0],
        [0,1,1,0,0,1,1,0],
        [0,0,0,0,0,0,0,0]]

start, goal = (0,0), (3,2)

def lineOfSight(p, s):
    x0 = p[0]
    y0 = p[1]
    x1 = s[0]
    y1 = s[1]

    dy = y1 - y0
    dx = x1 - x0
    f = 0

    if dy < 0:
        dy = -dy
        sy = -1
    else:
        sy = 1
    if dx < 0:
        dx = -dx
        sx = -1
    else:
        sx = 1

    result = []
    if dx >= dy:
        while x0 != x1:
            f = f + dy
            i1 = x0 + int((sx-1)/2)
            i2 = y0 + int((sy-1)/2)
            if f >= dx:
                result.append((i1,i2))
                y0 = y0 + sy
                f = f - dx
            if f != 0:
                result.append((i1,i2))
            if dy == 0:
                result.append((i1, y0))
                result.append((i1, y0-1))
            x0 = x0 + sx
    else:
        while y0 != y1:
            f = f + dx
            i1 = x0 + int((sx-1)/2)
            i2 = y0 + int((sy-1)/2)
            if f >= dy:
                result.append((i1, i2))
                x0 = x0 + sx
                f = f - dy
            if f != 0:
                print((i1, i2))
            if dx == 0:
                result.append((x0, i2))
                result.append((x0-1, i2))
            y0 = y0 + sy
    return result

check = lineOfSight(start,goal)

def getSquare(point):
    x = [point[1], point[1]+1]
    y0 = [-point[0], -point[0]]
    y1 = [-(point[0]+1), -(point[0]+1)]
    return (x,y0,y1)

checked = []
for xy in check:
    checked.append(getSquare(xy))

plt.plot(start[1], -start[0], 'sb', label="Start Point")
plt.plot(goal[1], -goal[0], 'sg', label="Goal Point")

maxRow = len(grid)
maxCol = len(grid[0])
listRow = [-i for i in range(maxRow+1)]
listCol = [i for i in range(maxCol+1)]
xHorizontal, yHorizontal = np.meshgrid(listCol, [[0],[-maxRow]])
yVertical, xVertical = np.meshgrid(listRow, [[0],[maxCol]])
plt.plot(xHorizontal, yHorizontal, color='black')
plt.plot(xVertical, yVertical, color='black')

for square in checked:
    x, y0, y1 = square
    plt.fill_between(x,y0,y1, color='black')

plt.plot([start[1], goal[1]], [-start[0], -goal[0]], '-g')

plt.axis('equal')
plt.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
网格=[[0,0,0,0,0,0,0,0],
[0,1,1,0,0,1,1,0],
[0,0,0,0,0,0,0,0],
[0,1,1,0,0,1,1,0],
[0,0,0,0,0,0,0,0]]
开始,目标=(0,0),(3,2)
def视线(p,s):
x0=p[0]
y0=p[1]
x1=s[0]
y1=s[1]
dy=y1-y0
dx=x1-x0
f=0
如果dy<0:
dy=-dy
sy=-1
其他:
sy=1
如果dx<0:
dx=-dx
sx=-1
其他:
sx=1
结果=[]
如果dx>=dy:
而x0!=x1:
f=f+dy
i1=x0+int((sx-1)/2)
i2=y0+int((sy-1)/2)
如果f>=dx:
result.append((i1,i2))
y0=y0+sy
f=f-dx
如果f!=0:
result.append((i1,i2))
如果dy==0:
结果追加((i1,y0))
结果追加((i1,y0-1))
x0=x0+sx
其他:
而y0!=y1:
f=f+dx
i1=x0+int((sx-1)/2)
i2=y0+int((sy-1)/2)
如果f>=dy:
result.append((i1,i2))
x0=x0+sx
f=f-dy
如果f!=0:
打印((i1,i2))
如果dx==0:
结果追加((x0,i2))
结果追加((x0-1,i2))
y0=y0+sy
返回结果
检查=视线(起点、终点)
def getSquare(点):
x=[点[1],点[1]+1]
y0=[-点[0],-点[0]]
y1=[-(点[0]+1),-(点[0]+1)]
返回(x、y0、y1)
选中=[]
对于检查中的xy:
选中。追加(getSquare(xy))
plt.plot(开始[1],-start[0],'sb',label=“起点”)
plt.plot(目标[1],-goal[0],'sg',label=“目标点”)
maxRow=len(网格)
maxCol=len(网格[0])
listRow=[-i表示范围内的i(maxRow+1)]
listCol=[i代表范围内的i(maxCol+1)]
xHorizontal,yHorizontal=np.meshgrid(listCol,[[0],-maxRow]]
yVertical,xVertical=np.meshgrid(listRow,[[0],[maxCol]])
plt.绘图(X水平、Y水平、颜色='黑色')
plt.绘图(X垂直、Y垂直、颜色='黑色')
对于已签入的方形:
x、 y0,y1=平方
在(x,y0,y1,color='black'之间填充
plt.plot([start[1],goal[1]],[-start[0],-goal[0]],'-g'))
plt.轴(“相等”)
plt.show()

请注意,如果您的线与某个正方形接触或不接触,可能会出现转角情况,这取决于您的定义。所以首先要弄清楚“内部”的概念。从这一点出发,你可以很容易地使用在短轴上运行的Bresenham风格的算法,即与你的实现相反的算法。@Vroomfondel,亲爱的,我没有理解你的意思。在你可以前进之前,你需要准确地说出一个正方形何时是路径的一部分。是否存在真正的内部点,即当线与封闭边界的关系成立时?但是,像(0,1)->(3,1)这样完全在边界上运行的路径呢?哪些方块将成为路径的一部分,线上方或下方的方块?