Last active
December 5, 2020 04:20
-
-
Save apple1417/426a2891ae1bf38af187f3576671c4cf 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
# https://docs.google.com/spreadsheets/d/1GrSU0VQN3oTZLa5dYV0AzyYFkdEaECHYjh79UUfAb38/edit?usp=sharing | |
import csv | |
import png | |
# Originally from https://sashamaps.net/docs/tools/20-colors/ | |
COLOUR_LIST = [ | |
(230, 25, 75), ( 60, 180, 75), (255, 225, 25), ( 0, 130, 200), | |
(245, 130, 48), (145, 30, 180), (240, 50, 230), (210, 245, 60), | |
(250, 190, 212), (170, 110, 40), (128, 0, 0), (128, 128, 0), | |
(255, 215, 180), ( 0, 0, 128) | |
] | |
CSV_PATH = "Catch A Ride Coords - Positions.csv" | |
IMAGE_SIZE = 2048 | |
IMAGE_PATH = "out.png" | |
GRIDLINE_INTERVAL = 5000 | |
GRIDLINE_COLOUR = (255, 255, 255, 32) | |
MARKER_POSITIONS = ((0, 0), (1e4, 0), (2e4, 2e4)) | |
CROSS_LENGTH = 5 | |
allCoords = {} | |
xMin = 9e99 | |
xMax = -9e99 | |
yMin = 9e99 | |
yMax = -9e99 | |
world_order = [] | |
with open(CSV_PATH) as file: | |
reader = csv.reader(file) | |
next(reader) # Skip the header row | |
currentWorld = "" | |
for row in reader: | |
if row[1] == "": | |
currentWorld = row[0] | |
allCoords[currentWorld] = [] | |
world_order.append(currentWorld) | |
continue | |
x = int(row[1]) | |
y = int(row[2]) | |
if x < xMin: | |
xMin = x | |
if x > xMax: | |
xMax = x | |
if y < yMin: | |
yMin = y | |
if y > yMax: | |
yMax = y | |
allCoords[currentWorld].append((x, y)) | |
xMin -= GRIDLINE_INTERVAL | |
xMax += GRIDLINE_INTERVAL | |
yMin -= GRIDLINE_INTERVAL | |
yMax += GRIDLINE_INTERVAL | |
# Want to make sure (0, 0) is centered | |
if abs(xMin) > xMax: | |
xMax = abs(xMin) | |
else: | |
xMin = -xMax | |
if abs(yMin) > yMax: | |
yMax = abs(yMin) | |
else: | |
yMin = -yMax | |
minPos = min(xMin, yMin) | |
scale = min(IMAGE_SIZE / (xMax - xMin), IMAGE_SIZE / (yMax - yMin)) | |
def adjust(x): | |
return int((x * scale) + 0.5) | |
image = {} | |
gridMin = (minPos // GRIDLINE_INTERVAL) * GRIDLINE_INTERVAL | |
gridMax = int((adjust(gridMin) + IMAGE_SIZE) // scale) + GRIDLINE_INTERVAL | |
for trueA in range(gridMin, gridMax, GRIDLINE_INTERVAL): | |
a = adjust(trueA - minPos) | |
for b in range(IMAGE_SIZE): | |
image[(a, b)] = GRIDLINE_COLOUR | |
image[(b, a)] = GRIDLINE_COLOUR | |
for pos in MARKER_POSITIONS: | |
x = adjust(pos[0] - minPos) | |
y = adjust(pos[1] - minPos) | |
image[(x, y)] = GRIDLINE_COLOUR | |
for i in range(1, (CROSS_LENGTH * 4) + 1): | |
image[(x + i, y + i)] = GRIDLINE_COLOUR | |
image[(x + i, y - i)] = GRIDLINE_COLOUR | |
image[(x - i, y + i)] = GRIDLINE_COLOUR | |
image[(x - i, y - i)] = GRIDLINE_COLOUR | |
colourIdx = 0 | |
for name in world_order: | |
colour = (COLOUR_LIST[colourIdx][0], COLOUR_LIST[colourIdx][1], COLOUR_LIST[colourIdx][2], 255) | |
colourIdx += 1 | |
print(f"{name}: #{colour[0]:02X}{colour[1]:02X}{colour[2]:02X}") | |
world = allCoords[name] | |
for pos in world: | |
x = adjust(pos[0] - minPos) | |
y = adjust(pos[1] - minPos) | |
image[(x, y)] = colour | |
for i in range(1, CROSS_LENGTH + 1): | |
image[(x + i, y + i)] = colour | |
image[(x + i, y - i)] = colour | |
image[(x - i, y + i)] = colour | |
image[(x - i, y - i)] = colour | |
def iter_rows(): | |
# Do a small transform here | |
# +x -> Up | |
# +y -> Right | |
for x in range(IMAGE_SIZE, 0, -1): | |
current_row = [] | |
for y in range(IMAGE_SIZE): | |
if (x, y) in image: | |
colour = image[(x, y)] | |
assert(len(colour) == 4) | |
current_row += list(colour) | |
else: | |
current_row += [0, 0, 0, 0] | |
yield current_row | |
png.from_array( | |
iter_rows(), | |
"RGBA", | |
info={ | |
"size": (IMAGE_SIZE, IMAGE_SIZE) | |
} | |
).save(IMAGE_PATH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment