#! /usr/bin/env python
from visual import vector
class PointList:
"""A wrapper around a list that provides some spatial query operations. Has only
one member variable: self.points, the list of points (as vectors)."""
def __init__(self, points):
self.points = []
for p in points:
self.points.append(vector(p))
def closest(self, p):
"""Find the closest point to p within self."""
p = vector(p)
c = self.points[0]
d = (c-p).mag2
for q in self.points:
dq = (q-p).mag2
if dq < d:
d = dq
c = q
return d,c
def in_rect(self, lower, upper):
"""Return a list of points in self and within the bounds lower and upper."""
lo = vector(lower)
up = vector(upper)
ans = []
for p in self.points:
if lo.x <= p.x <= up.x and lo.y <= p.y <= up.y:
ans.append(p)
return ans
def find_point(self, p):
"""If point p is in the set of points, return True. Otherwise return
False."""
return False