用python画出网格图与路线图


用python画出网格图与路线图

 

 

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import MultipleLocator
import copy
import pylab
import random

network = [[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
           [0,1,1,0,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0],
           [0,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0],
           [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0],
           [1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
           [1,1,1,1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0],
           [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
           [1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0],
           [1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0],
           [1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
           [0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,0],
           [0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]]

def return_axis(path):# 将坐标转换为axis
    path_x = []
    path_y = []
    for item in path:
        path_x.append(item[0])
        path_y.append(item[1])
    return path_x, path_y

def draw(network, path):
    n = len(network)
    network_draw = np.zeros((n,n))
    for i in range(n):# 换色
        for j in range(n):
            if network[i][j] == 0:
                network_draw[i][j] = 1
            else:
                network_draw[i][j] = 0
    mat = np.array(network_draw)
    plt.matshow(mat, cmap=plt.cm.gray)# 着色,黑白灰
    x = np.array(range(n))
    y = x
    for item in path:
        pylab.scatter(item[0], item[1],color='green')# 画点
    path_x, path_y = return_axis(path)
    plt.plot(path_x, path_y, 'g-', color='green')# 画折线
    # pylab.xticks(x)
    # pylab.yticks(y)
    # plt.grid()

    plt.show()

def accord(network, point, dx, dy, path):# 下一跳符合矩阵条件,且不在路径中、不为墙
    n = len(network)
    # print(point,dx,dy,path)
    next_x = point[0] + dx
    next_y = point[1] + dy
    # print([next_x, next_y],path,True)
    if next_x >= 0 and next_x < n and next_y >=0 and next_y < n and /
        network[next_x][next_y] != 1 and [next_x, next_y] not in list(path):
        return True
    return False

path = [[0,0],[1,0],[2,0],[3,0],[3,1],[3,2],[3,3],[3,4],[4,4],[5,4],[5,5],
        [5,6],[5,7],[6,7],[7,7],[8,7],[9,7],[10,7],[11,7],[12,7],[13,7],
        [13,8],[13,9],[13,10],[13,11],[13,12],[13,13],[13,14],[14,14],
        [15,14],[15,15],[15,16],[15,17],[15,18],[15,19],[16,19],[17,19],
        [18,19],[19,19]]
draw(network, path)

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/python/281736.html

(0)
上一篇 2022年8月23日 00:20
下一篇 2022年8月23日 00:57

相关推荐

发表回复

登录后才能评论