Skip to content

Instantly share code, notes, and snippets.

@mooskagh
Created March 19, 2018 20:43
Show Gist options
  • Save mooskagh/7c0c41420334a4651cf6a2af373f4a66 to your computer and use it in GitHub Desktop.
Save mooskagh/7c0c41420334a4651cf6a2af373f4a66 to your computer and use it in GitHub Desktop.
#!/bin/env python3
import random
import svgwrite
from svgwrite.shapes import Circle, Line, Rect
from svgwrite.container import Group
from svgwrite.masking import Mask
from collections import defaultdict
LOGO = [
'x 132 55656 ',
'x 1 2 5 ',
'y 3 6 opo a ba fgh ',
'x 1 5 p o ba a f g',
'y 3 6 oopop a f g',
'x 1 3 5 o a f g',
'xyyxx 332 57888 popp a gfg ',
]
LINKS = [
('x1', 'red'),
('36', 'green'),
('23', 'green'),
('67', 'blue'),
('68', 'blue'),
('7o', 'blue'),
('6p', 'blue'),
('pa', 'orange'),
('af', 'indigo'),
('fg', 'orangered'),
]
# All sizes are in "Step" sizes. Step is a distance between centers of
# horizontally adjacent rounds.
TILT = 0.15 # Tangent of an angle of tilt. Or, the same, offset to the right
# of the upper row relative to the row directly under it.
DIAMETER = 0.8
MARGIN = 10
# In centimeters
REAL_HEIGHT = 10
BACKGROUND = 'black'
STROKE_WIDTH = 0.1
STROKE_OPACITY = 0.4
CIRCLE_ATTRS = {
'fill': 'red',
'fill-opacity': 1,
'stroke': 'yellow',
'stroke-width': 0.1,
}
# Computed vars
TEXT_ROWS = len(LOGO)
TEXT_COLS = max([len(x) for x in LOGO])
HEIGHT = TEXT_ROWS + 2 * MARGIN
WIDTH = TEXT_COLS + abs(TILT) * TEXT_ROWS + 2 * MARGIN
REAL_WIDTH = REAL_HEIGHT * WIDTH / HEIGHT
SIZE = ('%gcm' % REAL_WIDTH, '%gcm' % REAL_HEIGHT)
VIEWBOX_ORIGIN = (-MARGIN - 0.5 - max([0, TILT * TEXT_ROWS]), -MARGIN - 0.5)
def DrawCircles(dwg):
char_to_circles = defaultdict(list)
for i, row in enumerate(LOGO):
for j, c in enumerate(row):
if c == ' ':
continue
center = (j - i * TILT, i)
circle = Circle(
center=center,
r=DIAMETER / 2,
**CIRCLE_ATTRS,
)
char_to_circles[c].append(center)
dwg.add(circle)
return char_to_circles
def DrawLinks(circles):
res = Group(mask="url(#nodemask)")
for (link, color) in LINKS:
for f in circles[link[0]]:
for t in circles[link[1]]:
# color = '#%s' % ''.join(
# ['%01x' % random.randint(0, 15) for _ in range(6)])
line = Line(
start=f,
end=t,
stroke_width=STROKE_WIDTH,
stroke_opacity=STROKE_OPACITY,
stroke=color,
)
res.add(line)
return res
def DrawMask(circles):
res = Mask(id='nodemask')
res.add(
Rect(
insert=VIEWBOX_ORIGIN,
size=('100%', '100%'),
fill='white',
))
for x in circles.values():
for y in x:
res.add(Circle(center=y, r=DIAMETER / 2, opacity=1, fill='black'))
return res
def main():
dwg = svgwrite.Drawing('lczero.svg', size=SIZE)
dwg.viewbox(*VIEWBOX_ORIGIN, WIDTH, HEIGHT)
if BACKGROUND:
dwg.add(
Rect(
insert=VIEWBOX_ORIGIN,
size=('100%', '100%'),
fill=BACKGROUND,
))
cdwg = Group()
circles = DrawCircles(cdwg)
dwg.defs.add(DrawMask(circles))
links = DrawLinks(circles)
dwg.add(links)
dwg.add(cdwg)
dwg.save()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment