この記事の Awkward のバージョンは1.X.Xです
例外処理はちゃんとしてないので注意せよ。
import numpy as np import awkward as ak import matplotlib.pyplot as plt # 配列作成 obj={} rs = np.random.RandomState(0) obj["memx"]=rs.rand(1000) obj["memy"]=rs.rand(1000) memx = "memx" memy = "memy" tree = ak.Array(obj) # グラフ化 plt.scatter(tree[memx],tree[memy]) plt.plot(pointx,pointy,color="tab:red") plt.xlim(-0.1,1.1) plt.ylim(-0.1,1.1) plt.show() def get_in_convex_polygon(tree, memx, memy, pointx, pointy): assert(len(pointx)==len(pointy)) assert(pointx[0]==pointx[-1]) assert(pointy[0]==pointy[-1]) assert(len(pointx)>=4) dx = pointx[1]-pointx[0] dy = pointy[1]-pointy[0] if ((pointx[2]-pointx[0])*dy-(pointy[2]-pointy[0])*dx)>0: # 回転方向を合わせる pointx.reverse() pointy.reverse() for i in range(len(pointx)-1): dx = pointx[i+1]-pointx[i+0] dy = pointy[i+1]-pointy[i+0] tree = tree[((tree[memx]-pointx[i])*dy-(tree[memy]-pointy[i])*dx)<0] return tree # 三角形の点列 (終点は始点と同じにせよ) pointx = [0.0,0.6,0.3,0.0] pointy = [0.0,0.0,0.5,0.0] tree = get_in_convex_polygon(tree,memx,memy,pointx,pointy) # グラフ化 plt.scatter(tree[memx],tree[memy]) plt.plot(pointx,pointy,color="tab:red") plt.xlim(-0.1,1.1) plt.ylim(-0.1,1.1) plt.show()
凸多角形って言うけど、凸って字は凸多角形ではないんだよなあ。