開発環境
- macOS High Sierra - Apple
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の6章(幾何図形とフラクタルを描画する)、5.4(プログラミングチャレンジ)、問題6-2(シェルピンスキーの三角形)を取り組んでみる。
コード(Emacs)
Python 3
#!/usr/bin/env python3 import matplotlib.pyplot as plt import random def transformation1(x, y): return 0.5 * x, 0.5 * y def transformation2(x, y): return 0.5 * x + 0.5, 0.5 * y + 0.5 def transformation3(x, y): return 0.5 * x + 1, 0.5 * y transformations = [transformation1, transformation2, transformation3] def transform(x, y): return transformations[random.randrange(3)] def get_points(n): x = 0 y = 0 xs = [x] ys = [y] for _ in range(n): t = transformations[random.randrange(3)] x, y = t(x, y) xs.append(x) ys.append(y) return xs, ys if __name__ == '__main__': xs, ys = get_points(10000) plt.plot(xs, ys, 'go', markersize=1) plt.savefig('sample2.png')
入出力結果(Terminal, Jupyter(IPython))
$ ./sample2.py $
0 コメント:
コメントを投稿