Math Adventures with Python
An Illustrated Guide to Exploring Math with Code
楽天ブックス(Kobo)
紀伊国屋書店(Kinoppy)
開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Math Adventures with Python: An Illustrated Guide to Exploring Math with Code (Peter Farrell(著)、No Starch Press)のPART Ⅰ(HITCHIN' UP YOUR PYTHON WAGON)、1(DRAWING POLYGONS WITH THE TURTLE MODULE)、EXERCISE1-3(TRI AND TRI AGAIN)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 import time import turtle # 保存用 import io from PIL import Image turtle.shape('turtle') def triangle(side_length=100): for i in range(3): turtle.forward(side_length) turtle.right(120) turtle.speed(0) for _ in range(60): triangle() turtle.right(5) for color, side_length in zip(['red', 'green'], [200, 300]): turtle.pencolor(color) for _ in range(60): triangle(side_length) turtle.right(5) turtle.pencolor('blue') for n in range(60): triangle(100 + n * 5) turtle.right(5) # 保存 ps = turtle.getscreen().getcanvas().postscript() with io.BytesIO() as buffer: buffer.write(ps.encode('ascii')) im = Image.open(buffer) im.save(f'sample3.png')
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ python3 sample3.py $
0 コメント:
コメントを投稿