All checks were successful
continuous-integration/drone/push Build is passing
Random coordinates are now float values.
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
#!/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):
|
|
self.set_x(x)
|
|
self.set_y(y)
|
|
def set_x(self, new_x):
|
|
"""Set the X-Coordinate of the Point"""
|
|
self.__x = new_x
|
|
def get_x(self):
|
|
"""Returns the X-Coordinate of the Point"""
|
|
return self.__x
|
|
def set_y(self, new_y):
|
|
"""Set the Y-Coordinate of the Point"""
|
|
self.__y = new_y
|
|
def get_y(self):
|
|
"""Returns the Y-Coordinate of the Point"""
|
|
return self.__y
|
|
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 get_random(min_x=0, max_x=100, min_y=0, max_y=100):
|
|
"""Set the point to a random place inside the boundaries"""
|
|
return Point(random.uniform(min_x, max_x), random.uniform(min_y, max_y))
|
|
|
|
def sort_set(point_set, sort_by_y=False):
|
|
"""Sort a Point set using merge sort"""
|
|
num_compares = 0
|
|
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_set(left, sort_by_y) # Sorting the first half
|
|
num_compares += sort_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
|