def make_spiral(self, r=0.25, G=0.0001):
for k in range(10):
x = self.X[:, 0] - 0.5
y = self.X[:, 1] - 0.5
theta = np.arctan2(x, y)
ds = [r * (i + theta / (2 * np.pi)) for i in range(int(1 / r))]
alphas = [np.sqrt(x ** 2 + y ** 2) / d for d in ds]
for alpha in alphas:
d = np.concatenate([(x * (1 - alpha))[:, None], (y * (1 - alpha))[:, None]], axis=1)
f = -G * d / (d ** 2).sum(axis=1, keepdims=True)
self.X += f
self.X = np.clip(self.X, 0, 1)
rs = np.arange(0, 0.7, 0.001)
theta = 2 * np.pi * rs / r
y = rs * np.sin(theta) + 0.5
x = -rs * np.cos(theta) + 0.5
spiral = zip(x, y)
self.collection = matplotlib.collections.LineCollection([spiral], colors='k')
评论列表
文章目录