A computational geometry learning and experimentation tool.
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.

280 lines
8.1 KiB

from math import floor
from .exceptions import ExceededWindowBoundsError
class Point:
"""
A class representing a point. A point
has a point_size bounding box around
it.
"""
def __init__(self, x, y, point_size, viewport_width, viewport_height):
"""
Initializes a new point with a point_size bounding box.
Initialized with additional viewport data to make sure the
move function refuses to move a point outside the screen.
@param x The x-coordinate.
@param y The y-coordinate.
@param point_size The size of the point in pixels.
@param viewport_width The width of the viewport.
@param viewport_height The height of the viewport.
"""
self.__point_size = point_size
self.__x = x
self.__y = y
self.__viewport_width = viewport_width
self.__viewport_height = viewport_height
half_point = floor(point_size / 2.0)
self.__check_window_bounds(x, y)
self.__selected = False
self.__top_left_corner = (self.__x - half_point,
self.__y + half_point)
self.__bottom_right_corner = (self.__x + half_point,
self.__y - half_point)
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
@property
def point_size(self):
return self.__point_size
@property
def selected(self):
return self.__selected
def __check_window_bounds(self, x, y):
"""
Simple window bound check that raises an exception when
the point (x, y) exceeds the known viewport bounds.
@param x The x-coordinate under test.
@param y The y-coordinate under test.
@raises ExceededWindowBoundsError If the viewport bounds are exceeded.
"""
half_point = floor(self.point_size / 2.0)
# Screen size in pixels is always positive
# We need to include the half point here because
# the (x, y) for a point is the center of the square and we
# do not want the EDGES to exceed the viewport bounds.
if (x > self.__viewport_width - half_point or
y > self.__viewport_height - half_point or
x < half_point or
y < half_point):
raise ExceededWindowBoundsError
def move(self, dx, dy):
"""
Adds the deltas dx and dy to the point.
@param dx The delta in the x direction.
@param dy The delta in the y direction.
"""
self.__check_window_bounds(self.__x + dx, self.__y + dy)
self.__x += dx
self.__y += dy
def __eq__(self, other):
"""
Override for class equality.
@param other The other object.
"""
return (self.__x == other.x and
self.__y == other.y and
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):
return "POINT<X :{} Y: {} SIZE: {}>".format(self.__x,
self.__y,
self.__point_size)
def select(self):
"""
Selects the point.
"""
self.__selected = True
def unselect(self):
"""
Unselects the point.
"""
self.__selected = False
def hit(self, x, y):
"""
Determines if the point was hit inside of it's bounding box.
The condition for hit is simple - consider the following
bounding box:
-------------
| |
| (x,y) |
| |
-------------
Where the clicked location is in the center. Then the top
left corner is defined as (x - half_point_size, y + half_point_size)
and the bottom corner is (x + half_point_size, y - half_point_size)
So long as x and y are greater than the top left and less than the
top right it is considered a hit.
This function is necessary for properly deleting and selecting points.
"""
return (x >= self.__top_left_corner[0] and
x <= self.__bottom_right_corner[0] and
y <= self.__top_left_corner[1] and
y >= self.__bottom_right_corner[1])
class Attribute:
def __init__(self, name, value):
"""
Initializes an attribute.
"""
self.__name = name
self.__value = value
class PointSet:
"""
Useful container for points backed by a set to insure point
uniqueness.
"""
def __init__(self, point_size, viewport_width, viewport_height):
"""
Initializes a point container with points of size point_size.
@param point_size The size of the points.
@param viewport_width The width of the viewport for bounds
calculations.
@param viewport_height The height of the viewport for bounds
calculations.
"""
self.__points = set()
self.__attributes = {}
self.__point_size = point_size
self.__viewport_width = viewport_width
self.__viewport_height = viewport_height
@property
def points(self):
"""
Getter for points. Returns a generator for
looping.
"""
for point in self.__points:
yield point
@property
def point_size(self):
return self.__point_size
@property
def viewport_height(self):
return self.__viewport_height
@property
def viewport_width(self):
return self.__viewport_width
@viewport_height.setter
def viewport_height(self, height):
self.__viewport_height = height
@viewport_width.setter
def viewport_width(self, width):
self.__viewport_width = width
def empty(self):
return len(self.__points) == 0
5 years ago
def clear_selection(self):
"""
Handy helper function to clear all selected points.
"""
for p in self.__points:
p.unselect()
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.
@raises ExceededWindowBoundsError If the point could not be constructed
because it would be outside the window
bounds.
"""
if attrs != [] and not all(isinstance(x, Attribute) for x in attrs):
raise ValueError("Attributes in add_point must be an " +
"attribute array.")
point = Point(x, y, self.__point_size,
self.__viewport_width, self.__viewport_height)
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 based
on a bounding box calculation.
Removing a point is an exercise is determining which points
have been hit, and then pulling them out of the list.
If two points have a section overlapping, and the user clicks
the overlapped section, both points will be removed.
Currently O(n).
@param x The x-coordinate.
@param y The y-coordinate.
"""
# Find points that match
matched = set(p for p in self.__points if p.hit(x, y))
# In place set difference
self.__points = self.__points - matched
# Remove associated attributes
for point in matched:
self.__attributes.pop(point)
def attributes(self, point):
"""
Returns the attribute array for a given point.
@param point The point to get attributes for..
"""
return self.__attributes[point]