Files
Pascal Lais 89d8ff4b3f
All checks were successful
continuous-integration/drone/push Build is passing
Set point outline color correctly
The outline of the oval drawn now has the same color as the fill color
to display the whole point in this color.
2020-03-20 12:03:22 +01:00

105 lines
3.8 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 __eq__(self, other):
"""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 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, 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, outline=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"""
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