Add draw implementation for existing classes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-03-11 19:49:47 +01:00
parent 8040ecbd55
commit 867745fe4e
3 changed files with 51 additions and 10 deletions

View File

@@ -44,11 +44,14 @@ class Algorithm():
self.step() self.step()
return self._result return self._result
def is_running(self):
"""Return if the algorithm is running."""
return self._running
def get_result(self): def get_result(self):
"""Return the result of the algorithm.""" """Return the result of the algorithm."""
return self._result return self._result
def draw(self): def draw(self, canvas, width, height, max_x, max_y):
"""Dummy for draw method""" """Dummy for draw method"""
class NearestNeighborsSweep(Algorithm): class NearestNeighborsSweep(Algorithm):
@@ -148,5 +151,30 @@ class NearestNeighborsSweep(Algorithm):
print(result_string) print(result_string)
return result_string return result_string
def draw(self): def draw(self, canvas, width, height, max_x, max_y):
"""TODO: Draw the algorithm state on a canvas""" """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")

View File

@@ -1,5 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
"""The linesegment module defines the class line_segment.""" """The linesegment module defines the class LineSegment."""
import random import random
import point import point
@@ -63,14 +63,19 @@ class LineSegment:
def __str__(self): def __str__(self):
"""Return the line segment values as string.""" """Return the line segment values as string."""
return self.__startpoint.str() + "-->" + self.__endpoint.str() return self.__startpoint.str() + "-->" + self.__endpoint.str()
def draw(self): def draw(self, canvas, width, height, max_x, max_y, color="#000000"):
"""TODO: Draw the line segment in a canvas.""" """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): 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""" """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)), \ 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))) 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)), \ 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))) point.Point(random.randint(min_x, max_x), random.randint(min_y, max_y)))
return seg return seg

View File

@@ -23,15 +23,23 @@ class Point():
return self.__y return self.__y
def __eq__(self, other): def __eq__(self, other):
"""Returns True if the points coordinates are the same, else returns False""" """Returns True if the points coordinates are the same, else returns False"""
if other:
return self.get_x() == other.get_x() and self.get_y() == other.get_y() return self.get_x() == other.get_x() and self.get_y() == other.get_y()
return False
def __ne__(self, other): def __ne__(self, other):
"""Returns True if the points coordinates are the different, else returns False""" """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() return self.get_x() != other.get_x() or self.get_y() != other.get_y()
def __str__(self): def __str__(self):
"""Returns the coordinates as a string""" """Returns the coordinates as a string"""
return "(" + str(round(self.get_x(), 2)) + "/" + str(round(self.get_y(), 2)) + ")" return "(" + str(round(self.get_x(), 2)) + "/" + str(round(self.get_y(), 2)) + ")"
def draw(self): def draw(self, canvas, width, height, max_x, max_y, color="#000000", radius=1):
"""TODO: Draw the point in a canvas""" """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): 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""" """Set the point to a random place inside the boundaries"""