Skip to content

Instantly share code, notes, and snippets.

@henrych4
Created November 2, 2017 08:41
Show Gist options
  • Save henrych4/20fed032ed923f1c9490837fee9dd09e to your computer and use it in GitHub Desktop.
Save henrych4/20fed032ed923f1c9490837fee9dd09e to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.spatial import distance
m1 = [1, 3.1]
m2 = [1.5, 2.1]
m3 = [2, 2.2]
m4 = [3.1, 1.1]
all_m = [m1, m2, m3, m4]
all_m = np.array(all_m)
points = np.array([
[1.5, 0.8],
[3.3, 2.5],
[4.8, 3.8],
[4, 1.2],
[3.2, 5],
[2.5, 4.6],
[0.5, 3],
[0.6, 2.1],
[2.8, 2.3],
[4.2, 4.4],
[3.8, 3.9],
[4, 2.4],
[0.8, 0.4],
[1.7, 5],
[2.1, 2.9],
[3.5, 1.7],
[2.5, 0.8],
[0.6, 2.8],
[4.2, 0.3],
[3.1, 0.1]])
for i in range(len(points)):
a = 0.3 - 0.02 * i
a_n = 0.2 - 0.02 * i
closest_m_index = distance.cdist([points[i]], all_m).argmin()
closest_m = all_m[closest_m_index]
for j in range(len(all_m)):
if j == closest_m_index:
all_m[j] = all_m[j] + a * (points[i]-all_m[j])
else:
all_m[j] = all_m[j] + a_n * (points[i]-all_m[j])
print(all_m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment