You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
66 lines
1.6 KiB
5 years ago
|
class Attribute:
|
||
|
__name = None
|
||
|
__value = None
|
||
|
|
||
|
def __init__(self, name, value):
|
||
|
"""
|
||
|
Initializes an attribute.
|
||
|
"""
|
||
|
self.__name = name
|
||
|
self.__value = value
|
||
|
|
||
|
|
||
|
class PointSet:
|
||
|
"""
|
||
|
Useful point set for storing coordinates and attributes. It is
|
||
|
backed by a set to provide nice convenience functions.
|
||
|
"""
|
||
|
__points = set()
|
||
|
__attributes = {}
|
||
|
|
||
|
def add_point(self, x, y, attrs=[]):
|
||
|
"""
|
||
|
Adds a point in screen coordinates and an optional attribute to
|
||
|
the list.
|
||
|
|
||
|
@param x The x-coordinate.
|
||
|
@param y The y-coordinate.
|
||
|
@param attr An optional attribute.
|
||
|
"""
|
||
|
|
||
|
if attrs != [] and not all(isinstance(x, Attribute) for x in attrs):
|
||
|
raise ValueError("Attributes in add_point must be an " +
|
||
|
"attribute array.")
|
||
|
|
||
|
point = (x, y)
|
||
|
self.__points.add(point)
|
||
|
self.__attributes[point] = attrs
|
||
|
|
||
|
|
||
|
def remove_point(self, x, y):
|
||
|
"""
|
||
|
Removes a point and it's attributes from the point set.
|
||
|
"""
|
||
|
point = (x, y)
|
||
|
self.__points.discard(point)
|
||
|
self.__attributes.pop(point)
|
||
|
|
||
|
|
||
|
def attributes(self, x, y):
|
||
|
"""
|
||
|
Returns the attribute array for a given point.
|
||
|
|
||
|
@param x The x-coordinate of the point.
|
||
|
@param y The y-coordinate of the point.
|
||
|
"""
|
||
|
return self.__attributes[(x, y)]
|
||
|
|
||
|
@property
|
||
|
def points(self):
|
||
|
"""
|
||
|
Getter for points. Returns a generator for
|
||
|
looping.
|
||
|
"""
|
||
|
for point in self.__points:
|
||
|
yield point
|