commit bd190a5fb5973f33851009757ce1dc57e2f95cae
parent 18e3642e40614db8a107b9b4c1e81bc4d9dd14d8
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date: Mon, 1 Jun 2020 16:22:06 +0300
Fix a few bugs and output to png, svg, and stdout.
Diffstat:
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/dabbling/__init__.py b/dabbling/__init__.py
@@ -17,13 +17,13 @@ def dragon_curve(generation_number: int) -> PathType:
for i in range(generation_number):
new_portion = [ccw(n) for n in l[1:]]
new_pivot = new_portion[-1]
- l = new_portion + l
+ l = new_portion[::-1] + l
l = [
[n[0] - new_pivot[0],
n[1] - new_pivot[1]]
for n in l]
- return l
+ return l
def matplotlib(dragon_curve_path: PathType):
@@ -37,11 +37,21 @@ def matplotlib(dragon_curve_path: PathType):
def path_to_svg_file_content(path: PathType) -> str:
- svg_path = ''.join(f'M{p[0]} {p[1]} ' for p in path).strip()
- return '''
+ min_x = min(p[0] for p in path)
+ min_y = min(p[1] for p in path)
+
+ path = [(p[0] - min_x, p[1] - min_y) for p in path]
+
+ max_x = max(p[0] for p in path)
+ max_y = max(p[1] for p in path)
+
+ path = [(200 * p[0] / max_x, 200 * p[1] / max_y) for p in path]
+
+ svg_path = ''.join(f'L{p[0]} {p[1]} ' for p in path).strip()
+ return f'''
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
- <path d="{svg_path}"/>
+ <path d="M0 0 {svg_path}"/>
</svg>
'''.strip()
diff --git a/dabbling/console.py b/dabbling/console.py
@@ -1,11 +1,15 @@
import click
-from . import __version__, dragon_curve, matplotlib
+from . import __version__, dragon_curve, matplotlib, path_to_svg_file_content
@click.command()
@click.version_option(version=__version__)
def main():
- l = dragon_curve(10)
+ l = dragon_curve(12)
+ svg_content = path_to_svg_file_content(l)
+ with open('dragon_curve.svg', 'w') as f:
+ f.write(svg_content)
matplotlib(l)
+ click.echo(l)