"""Draw the post's figures as SVG: the plain counting grid, the labeled grid,
and the edge-counting rule. Run from anywhere: python3 code/make_figures.py
Coordinates are in grid units of 0.05 mm (one small subsquare), so the 3 mm
chamber is 60 units wide. Colors match the text colors in the post."""
import math
import os
import random
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
RED, ORANGE, BLUE, PURPLE = "#cc0000", "#e69138", "#3d85c6", "#674ea7"
INK = "#1f1f1f"
FONT = "Helvetica, Arial, sans-serif"
def line(x1, y1, x2, y2, w, color=INK):
return (f'<line x1="{x1:g}" y1="{y1:g}" x2="{x2:g}" y2="{y2:g}" '
f'stroke="{color}" stroke-width="{w:g}"/>')
def triple(x1, y1, x2, y2, w, gap):
"""A Neubauer triple ruling: three thin parallel lines."""
dx, dy = (0, gap) if y1 == y2 else (gap, 0)
return "".join(line(x1 + k * dx, y1 + k * dy, x2 + k * dx, y2 + k * dy, w)
for k in (-1, 0, 1))
def rule_cell(cx, cy, sx, sy, w, size=20):
out = []
for x in range(sx, size, sx):
out.append(line(cx + x, cy, cx + x, cy + size, w))
for y in range(sy, size, sy):
out.append(line(cx, cy + y, cx + size, cy + y, w))
return out
def chamber():
"""The full Neubauer improved ruling, 0..60 in both directions."""
g = []
for cx, cy in [(0, 0), (40, 0), (0, 40), (40, 40)]:
g += rule_cell(cx, cy, 5, 5, 0.1)
for cx, cy in [(20, 0), (20, 40)]:
g += rule_cell(cx, cy, 4, 5, 0.1)
for cx, cy in [(0, 20), (40, 20)]:
g += rule_cell(cx, cy, 5, 4, 0.1)
g += rule_cell(20, 20, 1, 1, 0.05)
for v in range(24, 40, 4): # triple lines between the small squares
g.append(triple(v, 20, v, 40, 0.05, 0.18))
g.append(triple(20, v, 40, v, 0.05, 0.18))
for v in (0, 20, 40, 60): # triple lines around the big squares
g.append(triple(v, 0, v, 60, 0.07, 0.22))
g.append(triple(0, v, 60, v, 0.07, 0.22))
return "\n".join(g)
def svg(view, body, width):
x, y, w, h = view
height = round(width * h / w)
return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="{x:g} {y:g} {w:g} {h:g}" '
f'width="{width}" height="{height}" font-family="{FONT}">\n'
f'<rect x="{x:g}" y="{y:g}" width="{w:g}" height="{h:g}" fill="#ffffff"/>\n'
f'{body}\n</svg>\n')
def box(x, y, s, color, fill_opacity=0.0, w=0.45):
return (f'<rect x="{x:g}" y="{y:g}" width="{s:g}" height="{s:g}" fill="{color}" '
f'fill-opacity="{fill_opacity:g}" stroke="{color}" stroke-width="{w:g}"/>')
def label(x, y, text, color, size, anchor="middle", weight="normal"):
"""Text with a white halo, so it reads on top of the grid lines."""
return (f'<text x="{x:g}" y="{y:g}" fill="{color}" font-size="{size:g}" '
f'text-anchor="{anchor}" font-weight="{weight}" stroke="#ffffff" '
f'stroke-width="{size * 0.28:g}" stroke-linejoin="round" '
f'paint-order="stroke">{text}</text>')
def measure(x1, x2, y, color, text, size):
"""A horizontal dimension line with end ticks and its length under it."""
t = size * 0.35
return "".join([
f'<g stroke="#ffffff" stroke-width="{size * 0.3:g}" stroke-linecap="round">'
f'{line(x1, y, x2, y, size * 0.3, "#ffffff")}</g>',
line(x1, y, x2, y, 0.18, color),
line(x1, y - t, x1, y + t, 0.18, color),
line(x2, y - t, x2, y + t, 0.18, color),
label((x1 + x2) / 2, y + size * 1.05, text, color, size),
])
def grid_plain():
return svg((-1, -1, 62, 62), chamber(), 620)
def grid_labeled():
parts = [chamber()]
# Big squares: the four corners and the center; one corner shaded.
parts.append(box(0, 0, 20, RED, 0.12))
for x, y in [(40, 0), (0, 40), (40, 40), (20, 20)]:
parts.append(box(x, y, 20, RED))
# A big subsquare in the top-right corner square.
parts.append(box(50, 5, 5, ORANGE, 0.2, 0.3))
# Small squares: the four corners and the center of the center square.
for x, y in [(20, 20), (36, 20), (20, 36), (36, 36), (28, 28)]:
parts.append(box(x, y, 4, BLUE, 0.2, 0.3))
# A small subsquare.
parts.append(box(31, 37, 1, PURPLE, 0.35, 0.18))
parts.append(label(10, 29.3, "big square", RED, 3.4))
parts.append(measure(0.6, 19.4, 31.4, RED, "1.00 mm", 1.9))
parts.append(label(50, 29.3, "small square", BLUE, 3.4))
parts.append(measure(42, 46, 31.4, BLUE, "0.20 mm", 1.9))
parts.append(label(52.5, 12.6, "big subsquare", ORANGE, 1.8))
parts.append(measure(50, 55, 13.8, ORANGE, "0.25 mm", 1.3))
parts.append(label(31.5, 41.6, "small subsquare", PURPLE, 1.8))
parts.append(measure(31, 32, 42.8, PURPLE, "0.05 mm", 1.3))
parts.append(label(30, 64.2, "depth of fluid: 0.100 mm", INK, 2.2))
return svg((-1, -1, 62, 67), "\n".join(parts), 620)
def rod(x, y, angle, fill, stroke, length=0.42, width=0.14):
return (f'<rect x="{-length / 2:g}" y="{-width / 2:g}" width="{length:g}" '
f'height="{width:g}" rx="{width / 2:g}" fill="{fill}" stroke="{stroke}" '
f'stroke-width="0.025" transform="translate({x:g} {y:g}) rotate({angle:g})"/>')
def edge_rule():
"""One small square (4 x 4 subsquares) with cells on and inside its edges.
Cells on the top or left edge are out; on the bottom or right edge, in."""
m = 0.9 # how much of the neighbouring squares to show
g = []
for v in range(-1, 6):
if v in (0, 4):
g.append(triple(v, -m, v, 4 + m, 0.02, 0.07))
g.append(triple(-m, v, 4 + m, v, 0.02, 0.07))
else:
g.append(line(v, -m, v, 4 + m, 0.018))
g.append(line(-m, v, 4 + m, v, 0.018))
out_fill, out_stroke = "#e06666", "#990000"
in_fill, in_stroke = "#6d9eeb", "#1155cc"
rnd = random.Random(4)
cells = []
# On the top and left edges: not counted.
for x in (0.55, 2.3, 3.5):
cells.append(rod(x, rnd.uniform(-0.05, 0.05), rnd.uniform(-50, 50), out_fill, out_stroke))
for y in (1.2, 2.9):
cells.append(rod(rnd.uniform(-0.05, 0.05), y, rnd.uniform(40, 140), out_fill, out_stroke))
# Outside the square entirely.
cells.append(rod(-0.55, 3.3, 30, out_fill, out_stroke))
cells.append(rod(1.6, 4.55, -10, out_fill, out_stroke))
# Inside: counted.
for x, y, a in [(1.2, 0.9, 80), (2.7, 1.4, -60), (0.7, 2.2, 10), (2.1, 2.6, 95),
(3.2, 2.3, 120), (1.6, 3.4, -20), (3.4, 3.6, 40)]:
cells.append(rod(x, y, a, in_fill, in_stroke))
# On the bottom and right edges: counted.
for x in (0.9, 2.6):
cells.append(rod(x, 4 + rnd.uniform(-0.05, 0.05), rnd.uniform(-40, 40), in_fill, in_stroke))
for y in (0.6, 2.0):
cells.append(rod(4 + rnd.uniform(-0.05, 0.05), y, rnd.uniform(60, 120), in_fill, in_stroke))
body = "\n".join(g + cells + [
label(-0.35, -0.3, "out", RED, 0.42, "start", "bold"),
label(4.35, 4.68, "in", BLUE, 0.42, "end", "bold"),
])
return svg((-m, -m, 4 + 2 * m, 4 + 2 * m), body, 480)
if __name__ == "__main__":
for name, make in [("grid.svg", grid_plain), ("grid-labeled.svg", grid_labeled),
("edge-rule.svg", edge_rule)]:
with open(os.path.join(OUT, name), "w") as f:
f.write(make())
print("wrote", name)