kaka.farm

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/kaka.farm
Log | Files | Refs | README

commit 75dfd3d8928ea08df5f0cc5a2a614f68ac457917
parent 17a31011ee0ce83a9e4ffb0f6fd762dd84dae787
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Wed,  3 Jun 2020 15:50:05 +0300

Add a pretty looking SVG rendering. I do not understand SVG path arcs yet.

Diffstat:
Mdabbling/console.py | 20++++++++++++++++----
Mdabbling/dragon_curve.py | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/dabbling/console.py b/dabbling/console.py @@ -1,16 +1,28 @@ import click from . import __version__ -from .dragon_curve import dragon_curve, path_to_svg_file_content, write_matplotlib_png +from .dragon_curve import dragon_curve, path_to_curvy_svg_no_clue, path_to_svg_file_content, write_matplotlib_png @click.command() +@click.option( + '--iterations', + '-n', + default='5', + help='Number of iterations', + metavar='ITERS', + show_default=True, +) @click.version_option(version=__version__) -def main(): - l = dragon_curve(12) +def main(iterations: str): + l = dragon_curve(int(iterations)) svg_content = path_to_svg_file_content(l) - with open('dragon_curve.svg', 'w') as f: + with open('dragon-curve.svg', 'w') as f: + f.write(svg_content) + + svg_content = path_to_curvy_svg_no_clue(l) + with open('dragon-curvy-no-clue.svg', 'w') as f: f.write(svg_content) write_matplotlib_png(l) diff --git a/dabbling/dragon_curve.py b/dabbling/dragon_curve.py @@ -71,3 +71,44 @@ def path_to_svg_file_content( </svg> '''.strip() + + +def path_to_curvy_svg_no_clue( + path: PathType, + image_width=500, + image_height=500, + left_border=50, + right_border=50, + bottom_border=50, + top_border=50, + ) -> str: + ''' + path = list(zip( + transform([p[0] for p in path], left_border, image_width - right_border), + transform([p[1] for p in path], top_border, image_width - bottom_border))) + ''' + + min_x = min(p[0] for p in path) + min_y = min(p[1] for p in path) + max_x = max(p[0] for p in path) + max_y = max(p[1] for p in path) + + stroke_width = (max_x - min_x) * 0.01 + + consecutives = list(zip(path[:-1], path[1:])) + + svg_path = ''.join(f'L{p[0]} {p[1]}' for p in path) + + svg_path += ( + ''.join( + f'M{pair[0][0]} {pair[0][1]}' + f'A2 2 0 0 0 {pair[1][0]} {pair[1][1]}' + for pair in consecutives)) + + return f''' +<svg width="{image_width}" height="{image_height}" viewBox="{min_x-1} {min_y-1} {max_x-min_x+1} {max_y-min_y+1}" fill="white" xmlns="http://www.w3.org/2000/svg"> + + <path fill="none" stroke="black" stroke-linejoin="round" stroke-width="{stroke_width}" d="M{path[0][0]} {path[0][1]}{svg_path}"/> + +</svg> + '''.strip()