geometry > numpy > star shaped (星形) の座標を得る | Matplotlibでの描画 > v0.3:座標は得られた | v0.4: モジュール化


動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0

処理概要

  • Star shapedのx,y座標を得る

code v0.3 座標は得られた

geometry_starShaped_180414.ipynb
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

'''
v0.3 Apr. 14, 2018
  - add get_starShaped()
v0.2 Apr. 14, 2018
  - fix bug: get_pentagon() did not use [radius] arg
  - get_pentagon() takes [upward] arg
v0.1 Apr. 14, 2018
  - add get_pentagon()
      + to obtain pentagon geometry
'''

NUM_VERT = 5  # number of vertices (5: for pentagon)


def get_pentagon(radius, upward):
    xs, ys = [], []
    for idx in range(NUM_VERT):
        theta = 2.0 * np.pi * idx / NUM_VERT
        if upward:
            theta += np.pi / 2.0  # to put the one vertex upward
        else:
            theta -= np.pi / 2.0  # to put the one vertex downward
        xs += [radius * np.cos(theta)]
        ys += [radius * np.sin(theta)]
    return xs, ys


def get_starShaped(rad_inner, rad_outer):
    xis, yis = get_pentagon(rad_inner, upward=False)
    xos, yos = get_pentagon(rad_outer, upward=True)
    retx = [*xis, *xos]
    rety = [*yis, *yos]
    return retx, rety

RAD_INNER = 5
RAD_OUTER = 10
xs, ys = get_starShaped(RAD_INNER, RAD_OUTER)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.scatter(xs, ys)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)

備考

  • 内部のPentagonの点は、外部のPentagonの点による線分上にはない

自分の用途(Star shapeの内部をFillする)にはこの不具合は問題ない。

code v0.4 モジュール化

  • star shaped取得処理をモジュール化した
  • テスト実行とメイン関数実行の2種類を表示
  • Figureサイズを変更
geometry_starShaped_180415.py
import numpy as np
import matplotlib.pyplot as plt

'''
v0.1 Apr. 15, 2018
  - add Test_get_starShaped()
  - separated from [geometry_starShaped_180414.ipynb]
'''

NUM_VERT = 5  # number of vertices (5: for pentagon)


def get_pentagon(radius, upward):
    xs, ys = [], []
    for idx in range(NUM_VERT):
        theta = 2.0 * np.pi * idx / NUM_VERT
        if upward:
            theta += np.pi / 2.0  # to put the one vertex upward
        else:
            theta -= np.pi / 2.0  # to put the one vertex downward
        xs += [radius * np.cos(theta)]
        ys += [radius * np.sin(theta)]
    return xs, ys


def get_starShaped(rad_inner, rad_outer):
    xis, yis = get_pentagon(rad_inner, upward=False)
    xos, yos = get_pentagon(rad_outer, upward=True)
    retx = [*xis, *xos]
    rety = [*yis, *yos]
    return retx, rety


def Test_get_starShaped():
    xs, ys = get_starShaped(rad_inner=5, rad_outer=10)
    return xs, ys

geometry_starShaped_180414.ipynb
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
from pylab import rcParams
import geometry_starShaped_180415 as GSS

'''
v0.4 Apr. 15, 2018
  - tweak figure size
  - show 2 results 
  - separate get_pentagon() and get_starShaped() to [geometry_starShaped_180415]
v0.3 Apr. 14, 2018
  - add get_starShaped()
v0.2 Apr. 14, 2018
  - fix bug: get_pentagon() did not use [radius] arg
  - get_pentagon() takes [upward] arg
v0.1 Apr. 14, 2018
  - add get_pentagon()
      + to obtain pentagon geometry
'''

rcParams['figure.figsize'] = 14, 7
rcParams['figure.dpi'] = 110

RAD_INNER = 5
RAD_OUTER = 10
xs1, ys1 = GSS.get_starShaped(RAD_INNER, RAD_OUTER)
xs2, ys2 = GSS.Test_get_starShaped()

fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1)
ax1.scatter(xs1, ys1)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.grid(True)

ax2 = fig.add_subplot(1, 2, 2)
ax2.scatter(xs2, ys2)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.grid(True)

fig.tight_layout()