2016年3月7日月曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 6.(Drawing Geometric Shapes and Fractals)、Programming Challenges #1: Packing Circles into a Square, (No. 4066)を取り組んでみる。

Programming Challenges #1: Packing Circles into a Square, (No. 4066)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from matplotlib import pyplot as plt

AX = plt.axes(xlim=(1, 5), ylim=(1, 5))

def draw_circle(x, y):
    return plt.Circle((x, y), radius=0.5, color='red', ec='black')

def draw_square():
    square = plt.Polygon([(1, 1), (5, 1), (5, 5), (1, 5)], closed=True)
    AX.add_patch(square)

if __name__ == '__main__':
    AX.set_aspect('equal')
    draw_square()
    y = 1.5
    while y < 5:
        x = 1.5
        while x < 5:
            c = draw_circle(x, y)
            AX.add_patch(c)
            x += 1.0
        y += 1.0
    plt.savefig('circles_packed_into_a_square.svg')
    plt.show()

入出力結果(Terminal, IPython)

0 コメント:

コメントを投稿