from pygame.locals import *
import pygame, sys, random
BACK = (0,0,200)
ORTH = [(-1,0), (1,0), (0,-1), (0,1)]
PI = 3.141592
random.seed(9999)
def ri(n):
return int(random.random() * n)
args = [int(v) for v in sys.argv[1:]]
W, H = args[:2]
shares = sorted(args[2:])
ratio = float(W*H) / sum(shares)
counts = [int(s*ratio) for s in shares]
for i in range(W*H - sum(counts)):
counts[i] += 1
colors = [(2+ri(250), 2+ri(250), 2+ri(250)) for c in counts]
countries = range(len(counts))
random.shuffle(countries)
border = ( set((x,y) for x in (0,W-1) for y in range(H)) |
set((x,y) for x in range(W) for y in (0,H-1)) )
screen = pygame.display.set_mode((W,H))
screen.fill(BACK)
pix = screen.set_at
def look(p):
if 0 <= p[0] < W and 0 <= p[1] < H:
return screen.get_at(p)
else:
return None
clock = pygame.time.Clock()
while True:
dt = clock.tick(300)
pygame.display.flip()
if countries:
country = countries.pop()
color = colors[country]
if not countries:
color = (20,20,200) # last fill color to be water
count = counts[country]
frontier = set()
plotted = 0
loc = border.pop()
while plotted < count:
pix(loc, color)
if plotted % 50 == 0:
pygame.display.flip()
plotted += 1
direc = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
for dloc in direc:
if look(dloc) == BACK:
frontier.add(dloc)
border |= frontier
if frontier:
loc = frontier.pop()
border.discard(loc)
else:
print 'Country %s cover %u of %u' % (
shares[country], plotted, count)
break
if not countries:
fn = 'mapper%u.png' % ri(1000)
pygame.image.save(screen, fn)
for event in pygame.event.get():
if event.type == QUIT: sys.exit(0)
if not hasattr(event, 'key'): continue
if event.key == K_ESCAPE: sys.exit(0)
from pygame.locals import *
import pygame, sys, random
BACK = (0,0,200)
ORTH = [(-1,0), (1,0), (0,-1), (0,1)]
PI = 3.141592
random.seed(9999)
def ri(n):
return int(random.random() * n)
args = [int(v) for v in sys.argv[1:]]
W, H = args[:2]
shares = sorted(args[2:])
ratio = float(W*H) / sum(shares)
counts = [int(s*ratio) for s in shares]
for i in range(W*H - sum(counts)):
counts[i] += 1
colors = [(2+ri(250), 2+ri(250), 2+ri(250)) for c in counts]
countries = range(len(counts))
random.shuffle(countries)
border = ( set((x,y) for x in (0,W-1) for y in range(H)) |
set((x,y) for x in range(W) for y in (0,H-1)) )
screen = pygame.display.set_mode((W,H))
screen.fill(BACK)
pix = screen.set_at
def look(p):
if 0 <= p[0] < W and 0 <= p[1] < H:
return screen.get_at(p)
else:
return None
clock = pygame.time.Clock()
while True:
dt = clock.tick(300)
pygame.display.flip()
if countries:
country = countries.pop()
color = colors[country]
if not countries:
color = (20,20,200) # last fill color to be water
count = counts[country]
frontier = set()
plotted = 0
loc = border.pop()
while plotted < count:
pix(loc, color)
if plotted % 50 == 0:
pygame.display.flip()
plotted += 1
direc = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
for dloc in direc:
if look(dloc) == BACK:
frontier.add(dloc)
border |= frontier
if frontier:
loc = frontier.pop()
border.discard(loc)
else:
print 'Country %s cover %u of %u' % (
shares[country], plotted, count)
break
if not countries:
fn = 'mapper%u.png' % ri(1000)
pygame.image.save(screen, fn)
for event in pygame.event.get():
if event.type == QUIT: sys.exit(0)
if not hasattr(event, 'key'): continue
if event.key == K_ESCAPE: sys.exit(0)