diff --git a/point.py b/point.py index cafdd0c..41f3024 100644 --- a/point.py +++ b/point.py @@ -1,6 +1,9 @@ #!/usr/bin/python3 """The Point Module defines a Point class""" +import random +import copy + class Point(): """The Point class handles a single two-dimensional point in the coordinate system""" def __init__(self, x=0, y=0): @@ -28,5 +31,75 @@ class Point(): def get_y(self): """Returns the Y-Coordinate of the Point""" return self.__y + def set_random(self, min_x=0, max_x=100, min_y=0, max_y=100): + """Set the point to a random place inside the boundaries""" + self.set_x(random.randint(min_x, max_x)) + self.set_y(random.randint(min_y, max_y)) + return self + def str(self): + """Returns the coordinates as a string""" + return "(" + str(self.get_x()) + "/" + str(self.get_y()) + ")" def draw(self): """TODO: Draw the point in a canvas""" + + +def sort_point_set(point_set, sort_by_y=False): + """Sort a Point set using merge sort""" + num_compares = 0 + for pnt in point_set: + if not isinstance(pnt, Point): + raise Exception("Elements of point set are not all of type point.Point") + if len(point_set) > 1: + mid = len(point_set)//2 #Finding the mid of the array + left = copy.deepcopy(point_set[:mid]) # Dividing the array elements + right = copy.deepcopy(point_set[mid:]) # into 2 halves + + num_compares += sort_point_set(left, sort_by_y) # Sorting the first half + num_compares += sort_point_set(right, sort_by_y) # Sorting the second half + + i = j = k = 0 + + # Copy data to temp arrays left[] and right[] + while i < len(left) and j < len(right): + left_value = left[i].get_x() + right_value = right[j].get_x() + + if sort_by_y: + left_value = left[i].get_y() + right_value = right[j].get_y() + + if left_value == right_value: + left_value = left[i].get_y() + right_value = right[j].get_y() + + if sort_by_y: + left_value = left[i].get_x() + right_value = right[j].get_x() + + if left_value < right_value: + point_set[k] = copy.deepcopy(left[i]) + i += 1 + else: + point_set[k] = copy.deepcopy(right[j]) + j += 1 + num_compares += 1 + elif left_value < right_value: + point_set[k] = copy.deepcopy(left[i]) + i += 1 + else: + point_set[k] = copy.deepcopy(right[j]) + j += 1 + k += 1 + num_compares += 1 + + # Checking if any element was left + while i < len(left): + point_set[k] = copy.deepcopy(left[i]) + i += 1 + k += 1 + + while j < len(right): + point_set[k] = copy.deepcopy(right[j]) + j += 1 + k += 1 + return num_compares