body = [[1,3],[3,5],[5,7], [1,5]]
from scipy import spatial
min([ spatial.distance.euclidean(body[i], body[j]) for i in range(len(body)) for j in range(len(body)) if j != i ])
min([ spatial.distance.euclidean(body[i], body[j]) for i in range(len(body)) for j in range(i+1, len(body))])
min([((body[i][0]-body[j][0])**2+(body[i][1]-body[j][1])**2, i, j) for i in range(len(body)) for j in range(i+1, len(body))])
import numpy as np
points = np.random.rand(10, 2)
print(points)
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
plt.scatter(x=points[:, 0], y=points[:, 1])
plt.show()
points[np.argsort(points[:, 0])] #usporiadanie podla prvej zlozky
def turn(A, B, C):
"Is the turn from A->B->C a 'right', 'left', or 'straight' turn?"
diff = (B.x - A.x) * (C.y - B.y) - (B.y - A.y) * (C.x - B.x)
return ('right' if diff < 0 else
'left' if diff > 0 else
'straight')
def half_hull(sorted_points):
"Return the half-hull from following points in sorted order."
# Add each point C in order; remove previous point B if A->B-C is not a left turn.
hull = []
for C in sorted_points:
# if A->B->C is not a left turn ...
while len(hull) >= 2 and turn(hull[-2], hull[-1], C) != 'left':
hull.pop() # ... then remove B from hull.
hull.append(C)
return hull
def convex_hull(points):
"Find the convex hull of a set of points."
if len(points) <= 3:
return points
# Find the two half-hulls and append them, but don't repeat first and last points
upper = half_hull(sorted(points))
lower = half_hull(reversed(sorted(points)))
return upper + lower[1:-1]
import collections
import random
Point = collections.namedtuple('Point', 'x, y')
def Points(n, seed=42):
"Generate n random points within a 3 x 2 box."
random.seed((n, seed))
b = 0.05 # border
return {Point(random.uniform(b, 3-b), random.uniform(b, 2-b))
for _ in range(n)}
convex_hull(Points(100))
def plot_points(points, style='r.', labels=False, closed=False):
"""Plot a collection of points. Optionally change the line style, label points with numbers,
and/or form a closed polygon by closing the line from the last point to the first."""
if labels:
for (i, (x, y)) in enumerate(points):
plt.text(x, y, ' '+str(i))
if closed:
points = points + [points[0]]
plt.plot([p.x for p in points], [p.y for p in points], style, linewidth=2.5)
plt.axis('scaled'); plt.axis('off')
def plot_convex_hull(points):
"Find the convex hull of these points, and show a plot."
hull = convex_hull(points)
plot_points(points)
plot_points(hull, 'bs-', closed=True, labels=True)
print(len(hull), 'of', len(points), 'points on hull')
plot_convex_hull(Points(100, seed=random.randint(0, 100)))