PIL / 任意の排列座標を中心に、任意の排列座標を回転させる
import matplotlib.pyplot as plt import numpy as np def rotRC(rad, RC, centerRC=[0.0, 0.0]): COS = np.cos(rad) SIN = np.sin(rad) centerRC = np.array(centerRC) RC = np.array(RC) # 座標の取りかたを数学座標(x, y)ではなく排列座標[行番号, 列番号]すなわち(-y , x)にしているので回転行列もそれに合わせて変えてある。 rotMat = np.array([[ COS, SIN], [-SIN, COS]]) shiftedRC = RC - centerRC # 回転中心を一旦原点[0行目, 0列目]にずらして、 rotatedRC = shiftedRC @ rotMat # 回転行列を適用して座標を回転させて、 # 元の回転中心に戻して、返す。 return (rotatedRC + centerRC)#.round(0).astype("int16") ######## # test # ######## if __name__ == "__main__": Pi = np.pi # この4つの排列座標[行番号, 列番号]を、 Verts = np.array([[-5, -5], [10, -5], [10, 40], [-5, 40]]) # この排列座標[行番号, 列番号]を中心に、 CenterRC = [-10, 20] # この角度だけ左に回してみる。 Rad = (1/2)*Pi rotatedVerts = rotRC(Rad, Verts, CenterRC)#.round(0).astype("int16") print(rotatedVerts) ax = plt.axes(ylim=(20,-40), xlim=(-10,50)) ax.set_aspect("equal") plt.scatter( CenterRC[1], CenterRC[0], c="black", marker="x") plt.scatter( Verts[:,1], Verts[:,0], c="blue") plt.scatter(rotatedVerts[:,1], rotatedVerts[:,0], c="red") plt.xlabel("col") plt.ylabel("row") plt.grid() plt.show()
実行結果: