diff --git a/algorithm.py b/algorithm.py index d17819d..5b67015 100644 --- a/algorithm.py +++ b/algorithm.py @@ -44,11 +44,14 @@ class Algorithm(): self.step() return self._result + def is_running(self): + """Return if the algorithm is running.""" + return self._running def get_result(self): """Return the result of the algorithm.""" return self._result - def draw(self): + def draw(self, canvas, width, height, max_x, max_y): """Dummy for draw method""" class NearestNeighborsSweep(Algorithm): @@ -148,5 +151,30 @@ class NearestNeighborsSweep(Algorithm): print(result_string) return result_string - def draw(self): - """TODO: Draw the algorithm state on a canvas""" + def draw(self, canvas, width, height, max_x, max_y): + """Draw the algorithm state on a canvas""" + canvas.delete("all") + for pnt in self._input: + if len(self._es) > 0 and pnt == self._es[0]: + cur_x, cur_y = self._es[0].draw(canvas, width, height, max_x, max_y, "#0000FF") + canvas.create_line(cur_x, 0, cur_x, height, fill="#0000FF") + if self._sss["mindist"]: + canvas.create_line(cur_x - (self._sss["mindist"] * width / max_x), 0, \ + cur_x - (self._sss["mindist"] * width / max_x), height, fill="#0000FF") + canvas.create_line(cur_x - (self._sss["mindist"] * width / max_x), \ + cur_y - (self._sss["mindist"] * height / max_y), \ + cur_x, cur_y - (self._sss["mindist"] * height / max_y), fill="#0000FF") + canvas.create_line(cur_x - (self._sss["mindist"] * width / max_x), \ + cur_y + (self._sss["mindist"] * height / max_y), \ + cur_x, cur_y + (self._sss["mindist"] * height / max_y), fill="#0000FF") + else: + canvas.create_line(0, 0, 0, max_y, fill="#0000FF") + if pnt in self._result["points"]: + pnt.draw(canvas, width, height, max_x, max_y, "#FF0000") + else: + pnt.draw(canvas, width, height, max_x, max_y) + if self._result["points"][0]: + linesegment.LineSegment( \ + self._result["points"][0], self._result["points"][1] \ + ).draw(canvas, width, height, max_x, max_y, "#FF0000") + diff --git a/linesegment.py b/linesegment.py index a3e1d3d..7837627 100644 --- a/linesegment.py +++ b/linesegment.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -"""The linesegment module defines the class line_segment.""" +"""The linesegment module defines the class LineSegment.""" import random import point @@ -63,14 +63,19 @@ class LineSegment: def __str__(self): """Return the line segment values as string.""" return self.__startpoint.str() + "-->" + self.__endpoint.str() - def draw(self): - """TODO: Draw the line segment in a canvas.""" + def draw(self, canvas, width, height, max_x, max_y, color="#000000"): + """Draw the line segment in a canvas.""" + from_x, from_y = self.__startpoint.draw(canvas, width, height, max_x, max_y, color) + to_x, to_y = self.__endpoint.draw(canvas, width, height, max_x, max_y, color) + + canvas.create_line(from_x, from_y, to_x, to_y, fill=color) + def get_random(min_x=0, max_x=100, min_y=0, max_y=100): """Set the point to a random place inside the boundaries""" seg = LineSegment(point.Point(random.randint(min_x, max_x), random.randint(min_y, max_y)), \ point.Point(random.randint(min_x, max_x), random.randint(min_y, max_y))) - while ls.get_startpoint().get_x() == ls.get_endpoint().get_x(): + while seg.get_startpoint().get_x() == seg.get_endpoint().get_x(): seg = LineSegment(point.Point(random.randint(min_x, max_x), random.randint(min_y, max_y)), \ point.Point(random.randint(min_x, max_x), random.randint(min_y, max_y))) return seg diff --git a/point.py b/point.py index 445e0b5..8924288 100644 --- a/point.py +++ b/point.py @@ -23,15 +23,23 @@ class Point(): return self.__y def __eq__(self, other): """Returns True if the points coordinates are the same, else returns False""" - return self.get_x() == other.get_x() and self.get_y() == other.get_y() + if other: + return self.get_x() == other.get_x() and self.get_y() == other.get_y() + return False def __ne__(self, other): """Returns True if the points coordinates are the different, else returns False""" return self.get_x() != other.get_x() or self.get_y() != other.get_y() def __str__(self): """Returns the coordinates as a string""" return "(" + str(round(self.get_x(), 2)) + "/" + str(round(self.get_y(), 2)) + ")" - def draw(self): - """TODO: Draw the point in a canvas""" + def draw(self, canvas, width, height, max_x, max_y, color="#000000", radius=1): + """Draw the point in a canvas""" + center_x = self.get_x() * (width / max_x) + center_y = height - (self.get_y() * (height / max_y)) + if center_x <= width and center_y <= height: + canvas.create_oval(center_x - radius, center_y - radius, \ + center_x + radius, center_y + radius, fill=color) + return center_x, center_y def get_random(min_x=0, max_x=100, min_y=0, max_y=100): """Set the point to a random place inside the boundaries"""