world-ecoregion/map-generator/index.py

76 lines
1.6 KiB
Python
Raw Normal View History

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate
import math
WIDTH = 900
HEIGHT = 450
RATIO = WIDTH / HEIGHT
MAX_ELEVATION = 100
def s(x):
# return x
return -2 * x**3 + 3 * x**2
WATER_LEVEL = 0
WATER_PROPORTION = 0.4
GROUND_PROPORTION = 1 - WATER_PROPORTION
# class Point(object):
# def __init__(self, x, y, z):
# self.x = x
# self.y = y
# self.z = z
# def is_ground(self):
# return self.z > WATER_LEVEL
# def is_water(self):
# return not(self.is_ground())
def is_ground(value):
return value > WATER_LEVEL
def max_recursion(fn, max_recursion=0):
def f(*args, recursion=0, **kwargs):
if recursion > max_recursion:
return
return fn(*args, **kwargs)
def continent_agent(ground, position, size):
if size <= 0: return
x, y = position
w, h = ground.shape
while True:
if size <= 0: break
dx = np.random.randint(2) or -1
dy = np.random.randint(2) or -1
x = max(min(x + dx, w - 1), 0)
y = max(min(y + dy, h - 1), 0)
if not is_ground(ground[x, y]):
size -= 1
ground[x, y] = 1
def generate_map(width, height, continents=4):
ground = np.zeros((width, height))
for continent in range(continents):
position = (np.random.randint(0, width), np.random.randint(0, height))
size = np.random.randint(width * height * GROUND_PROPORTION / continents)
print(size)
continent_agent(ground, position, size=size)
plt.imshow(ground.T, cmap='hot')
plt.show()
if __name__ == "__main__":
generate_map(WIDTH, HEIGHT)