1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| # coding: utf-8
import math
from random import randint
NB_POINTS = 10
XMAX, YMAX = 10, 10
class Point(object):
def __init__(self, xy):
self.x = xy[0]
self.y = xy[1]
def __str__(self):
return "(%d, %d)" % (self.x, self.y)
def dist(self, other):
dx, dy = self.x - other.x, self.y - other.y
return math.hypot(dx, dy)
class B(Point):
pass
class P(Point):
pass
def neighboors(B_instance, P_instances):
distances = [(binst.dist(pinst), pinst) for pinst in P_instances]
return [(t[1], t[0]) for t in sorted(distances)]
binst = B((0, 0))
# initialisation pour toujours avoir une distance nulle (verif)
pinsts = [P((binst.x, binst.y))]
# puis remplissage aléatoire
for _ in range(NB_POINTS):
pinsts.append(P((randint(-XMAX, XMAX), randint(-YMAX, YMAX))))
result = neighboors(binst, pinsts)
for inst, distance in result:
print inst, "distance :", distance |
Partager