Created
July 28, 2018 20:10
-
-
Save ChristopherKing42/648a081f8c1c81cb071d27ec5aefc855 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from opensimplex import OpenSimplex | |
from PIL import Image | |
import random | |
from heapq import * | |
mtns = OpenSimplex(seed = random.getrandbits(32)) | |
hills = OpenSimplex(seed = random.getrandbits(32)) | |
bumps = OpenSimplex(seed = random.getrandbits(32)) | |
def terrain((x,y)): | |
return mtns.noise2d(x/30.0, y/30.0)*1/4 + hills.noise2d(x/5.0,y/5.0)*1/2 + bumps.noise2d(x, y)*1/4 | |
dirs = [(dx,dy) for dx in [0, -1, 1] for dy in [0, -1, 1]][1:] | |
def Astar(city1, city2): | |
heap = [(abs(city1[0]-city2[0])+abs(city1[1]-city2[1]), 0, [city1])] #A heap of (estimate, parLn, parRd) | |
visited = set() | |
while True: | |
(_, parLn, parRd) = heappop(heap) | |
point = parRd[0] | |
if point == city2: return parRd | |
for (dx, dy) in dirs: | |
newPoint = (point[0] + dx, point[1] + dy) | |
if newPoint in visited: continue | |
visited.add(newPoint) | |
d = (dx**2 + dy**2)**0.5 | |
slope = (terrain(point) - terrain(newPoint))/d | |
newParRd = [newPoint] + parRd | |
newParLn = parLn + (1 + (10**10 * slope**8)) * d | |
newEstimate = newParLn + abs(newPoint[0] - city2[0]) + abs(newPoint[1] - city2[1]) | |
heappush(heap, (newEstimate, newParLn, newParRd)) | |
def main(): | |
road = Astar((32,100), (100,32)) | |
im = Image.new("RGB", (128,128)) | |
pixels = [] | |
for x in range(128): | |
for y in range(128): | |
if (x,y) in [(32,100), (100,32)]: | |
pixels.append((0,0,256)) | |
elif (x,y) in road: | |
pixels.append((0,0,0)) | |
else: | |
pixels.append((256, 256, 256)) | |
im.putdata(pixels) | |
im.save("roads.bmp") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment