66 lines
3 KiB
Python
66 lines
3 KiB
Python
|
|
"""A hand-drawn (wobbly) frame band, drawn as a filled shape whose two sides wave gently.
|
||
|
|
|
||
|
|
The wave is zero at both ends of the corner and of the edge tile, so the pieces still join exactly.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import math
|
||
|
|
|
||
|
|
from svg_common import grey, num
|
||
|
|
|
||
|
|
OUTER_WAVES, INNER_WAVES = 1, 2
|
||
|
|
|
||
|
|
|
||
|
|
def _offsets(fraction, half_width, outer_wobble, inner_wobble):
|
||
|
|
"""How far the outer and inner sides sit from the centre line at this point (0..1 along the piece)."""
|
||
|
|
outer = half_width + outer_wobble * math.sin(2 * math.pi * OUTER_WAVES * fraction)
|
||
|
|
inner = half_width - inner_wobble * math.sin(2 * math.pi * INNER_WAVES * fraction)
|
||
|
|
return outer, inner
|
||
|
|
|
||
|
|
|
||
|
|
def _polygon(points):
|
||
|
|
return " ".join(f"{num(x)},{num(y)}" for x, y in points)
|
||
|
|
|
||
|
|
|
||
|
|
def wobbly_edge(band_mid, length, half_width, outer_wobble, inner_wobble, lightness=0.5):
|
||
|
|
"""Filled vertical band for an edge tile."""
|
||
|
|
left_side, right_side = [], []
|
||
|
|
steps = 24
|
||
|
|
for step in range(steps + 1):
|
||
|
|
y = length * step / steps
|
||
|
|
outer, inner = _offsets(step / steps, half_width, outer_wobble, inner_wobble)
|
||
|
|
left_side.append((band_mid - outer, y))
|
||
|
|
right_side.append((band_mid + inner, y))
|
||
|
|
return f'<polygon points="{_polygon(left_side + right_side[::-1])}" fill="{grey(lightness)}"/>'
|
||
|
|
|
||
|
|
|
||
|
|
def _corner_centre_line(band_mid, radius, width, height, steps=60):
|
||
|
|
"""Points (x, y, outward normal) along the corner's centre line, from the bottom edge to the right edge."""
|
||
|
|
straight_down = height - (band_mid + radius)
|
||
|
|
straight_right = width - (band_mid + radius)
|
||
|
|
arc_length = math.pi * radius / 2
|
||
|
|
total = straight_down + arc_length + straight_right
|
||
|
|
centre = band_mid + radius
|
||
|
|
points = []
|
||
|
|
for step in range(steps + 1):
|
||
|
|
travelled = total * step / steps
|
||
|
|
if travelled <= straight_down:
|
||
|
|
points.append((band_mid, height - travelled, (-1.0, 0.0), travelled / total))
|
||
|
|
elif travelled <= straight_down + arc_length:
|
||
|
|
angle = math.pi + (travelled - straight_down) / radius # from pointing left to pointing up
|
||
|
|
normal = (math.cos(angle), math.sin(angle))
|
||
|
|
points.append((centre + radius * normal[0], centre + radius * normal[1], normal, travelled / total))
|
||
|
|
else:
|
||
|
|
along = travelled - straight_down - arc_length
|
||
|
|
points.append((centre + along, band_mid, (0.0, -1.0), travelled / total))
|
||
|
|
return points
|
||
|
|
|
||
|
|
|
||
|
|
def wobbly_corner(band_mid, radius, width, height, half_width, outer_wobble, inner_wobble, lightness=0.5):
|
||
|
|
"""Filled band around the corner's turn, with the same wave rule as the edge."""
|
||
|
|
outer_side, inner_side = [], []
|
||
|
|
for x, y, (normal_x, normal_y), fraction in _corner_centre_line(band_mid, radius, width, height):
|
||
|
|
outer, inner = _offsets(fraction, half_width, outer_wobble, inner_wobble)
|
||
|
|
outer_side.append((x + normal_x * outer, y + normal_y * outer))
|
||
|
|
inner_side.append((x - normal_x * inner, y - normal_y * inner))
|
||
|
|
return f'<polygon points="{_polygon(outer_side + inner_side[::-1])}" fill="{grey(lightness)}"/>'
|