Skip to content

Instantly share code, notes, and snippets.

@kh90909
Created July 13, 2018 22:44
Show Gist options
  • Save kh90909/83f9ba9c554ebff81fa15219af98d50c to your computer and use it in GitHub Desktop.
Save kh90909/83f9ba9c554ebff81fa15219af98d50c to your computer and use it in GitHub Desktop.
Illustration of pyvisgraph on a pixelated map
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import PIL
import PIL.ImageDraw
import pyvisgraph as vg
polys = [[vg.Point(3.0,2.0), vg.Point(6.0,2.0), vg.Point(6.0,7.0), vg.Point(3.0,7.0)]]
g = vg.VisGraph()
g.build(polys)
s = vg.Point(1.0,5.0)
e = vg.Point(8.0,5.0)
path = g.shortest_path(s, e)
print path
im = PIL.Image.new('RGB', (10,10), (255,255,255))
draw = PIL.ImageDraw.Draw(im, 'RGBA')
points = [(p.x,p.y) for p in polys[0]]
p1 = path[0]
for p2 in path[1:]:
draw.line((p1.x,p1.y,p2.x,p2.y),(255,0,0))
p1 = p2
draw.polygon(points, fill = None, outline = (0,0,0,160))
plt.text(s.x, s.y, 'S', horizontalalignment='center', verticalalignment='center')
plt.text(e.x, e.y, 'E', horizontalalignment='center', verticalalignment='center')
plt.imshow(im, origin='lower')
ax = plt.gca()
ax.set_xticks(np.arange(0, im.size[0], 1));
ax.set_yticks(np.arange(0, im.size[1], 1));
# Labels for major ticks
ax.set_xticklabels(np.arange(0, im.size[0], 1));
ax.set_yticklabels(np.arange(0, im.size[1], 1));
# Minor ticks
ax.set_xticks(np.arange(-.5, im.size[0], 1), minor=True);
ax.set_yticks(np.arange(-.5, im.size[1], 1), minor=True);
# Gridlines based on minor ticks
ax.grid(which='minor', color='gray', linestyle='-', linewidth=1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment