Skip to content

Instantly share code, notes, and snippets.

@lukateras
Last active July 16, 2023 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukateras/a1acd5fc02d438afc22eb06de46a41a3 to your computer and use it in GitHub Desktop.
Save lukateras/a1acd5fc02d438afc22eb06de46a41a3 to your computer and use it in GitHub Desktop.
Generate an SVG sinusoid that gradually gets more rounded
# SPDX-License-Identifier: 0BSD
from functools import partial
from itertools import chain, starmap, zip_longest
import numpy
def ascent(x):
return [(x, 0), (1 - x, -1), (1, -1)]
def descent(x):
return [(x, 0), (1 - x, 1), (1, 1)]
def squiggle(x1, x2):
return chain([] if x1 is None else ascent(x1),
[] if x2 is None else descent(x2))
def groupwise(iterable, size):
args = [iter(iterable)] * size
return zip_longest(*args)
def space(n):
return numpy.linspace(0, 1, n)
def pattern(n):
return chain.from_iterable(starmap(squiggle, groupwise(space(n), 2)))
svg_template = """
<svg width="{width}" height="1" xmlns="http://www.w3.org/2000/svg">
<path d="m 0,1 {curve}" fill="none" stroke="#f00" stroke-width="0.1" />
</svg>
""".strip()
def svg_curve(points):
return " ".join(chain(["c"], map(lambda point: ",".join(map(str, point)), points)))
def svg_pattern(n):
return svg_template.format(width=n, curve=svg_curve(pattern(n)))
if __name__ == "__main__":
print(svg_pattern(31)) # fun prime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment