Browse Source

Back PointSet by a list, add point constraint, now move-delete works

tb-init-ui-render
Taylor Bockman 5 years ago
parent
commit
1483e9a25b
  1. 38
      clusterview/points.py

38
clusterview/points.py

@ -71,7 +71,7 @@ class Point:
self.__y + half_point) self.__y + half_point)
self.__bottom_right_corner = (self.__x + half_point, self.__bottom_right_corner = (self.__x + half_point,
self.__y - half_point) self.__y - half_point)
def __check_window_bounds(self, x, y): def __check_window_bounds(self, x, y):
""" """
@ -122,13 +122,6 @@ class Point:
self.__y == other.y and self.__y == other.y and
self.__point_size == other.point_size) self.__point_size == other.point_size)
def __hash__(self):
"""
Overridden hashing function so it can be used as a dictionary key
for attributes.
"""
return hash((self.__x, self.__y, self.__point_size))
def __repr__(self): def __repr__(self):
# For some reason I had to split this instead of using one giant # For some reason I had to split this instead of using one giant
@ -192,8 +185,10 @@ class Attribute:
class PointSet: class PointSet:
""" """
Useful container for points backed by a set to insure point Useful container for points. Since points are not hashable (they are
uniqueness. modified in place by move) we are forced to back the PointSet with an
array. However, it is still a "set" in the "uniqueness among all points"
sense because `add_point` will reject a point with a duplicate center.
""" """
def __init__(self, point_size, viewport_width, viewport_height): def __init__(self, point_size, viewport_width, viewport_height):
@ -206,7 +201,7 @@ class PointSet:
@param viewport_height The height of the viewport for bounds @param viewport_height The height of the viewport for bounds
calculations. calculations.
""" """
self.__points = set() self.__points = []
self.__point_size = point_size self.__point_size = point_size
self.__viewport_width = viewport_width self.__viewport_width = viewport_width
self.__viewport_height = viewport_height self.__viewport_height = viewport_height
@ -218,7 +213,7 @@ class PointSet:
attributes_equal = True attributes_equal = True
other_points = set(other.points) other_points = list(other.points)
attributes_equal = (attributes_equal and attributes_equal = (attributes_equal and
(len(self.__points) == len(other_points))) (len(self.__points) == len(other_points)))
@ -316,12 +311,16 @@ class PointSet:
for attr in attrs: for attr in attrs:
point.add_attribute(attr) point.add_attribute(attr)
self.__points.add(point) if point in self.__points:
# Silently reject a duplicate point (same center).
return
self.__points.append(point)
def remove_point(self, x, y): def remove_point(self, x, y):
""" """
Removes a point and it's attributes from the point set based Removes a point from the point set based on a bounding
on a bounding box calculation. box calculation.
Removing a point is an exercise is determining which points Removing a point is an exercise is determining which points
have been hit, and then pulling them out of the list. have been hit, and then pulling them out of the list.
@ -334,9 +333,6 @@ class PointSet:
@param x The x-coordinate. @param x The x-coordinate.
@param y The y-coordinate. @param y The y-coordinate.
""" """
for p in self.__points:
# Find points that match if p.hit(x, y):
matched = set(p for p in self.__points if p.hit(x, y)) self.__points.remove(p)
# In place set difference
self.__points = self.__points - matched

Loading…
Cancel
Save