Skip to content

Commit

Permalink
Finalized seminar 6
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur486 committed Nov 14, 2022
1 parent dcadd7b commit be91218
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 50 deletions.
113 changes: 77 additions & 36 deletions src/seminar/group_912/seminar_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@
# -> Each function should do one thing only
# -> Functions communicate using input parameters and their return values
#
def create_rect(x1, y1, x2, y2: int):
"""
Create a rectangle with corners (x1, y1) and (x2, y2).
:param x1:
:param y1:
:param x2:
:param y2:
:return: The newly created rectangle
"""
pass


def rect_equal(rect1, rect2):
"""
Return True iff the two rectangles are equal (have the same corners)
:param rect1:
:param rect2:
:return:
"""
pass
# def create_rect(x1, y1, x2, y2: int):
# """
# Create a rectangle with corners (x1, y1) and (x2, y2).
# :param x1:
# :param y1:
# :param x2:
# :param y2:
# :return: The newly created rectangle
# """
# pass


# def rect_equal(rect1, rect2):
# """
# Return True iff the two rectangles are equal (have the same corners)
# :param rect1:
# :param rect2:
# :return:
# """
# pass


#
Expand All @@ -75,17 +75,38 @@ def rect_equal(rect1, rect2):
#
# Dumitrana Mihnea dictionary repres.
def create_rect(x1, y1, x2, y2):
# TODO Checks to make sure its an actual rect (not a point, not a line segment)
rect = {}
rect['low_left'] = (x1, y1)
rect['up_right'] = (x2, y2)
if x1 == x2 or y1 == y2:
return None

rect = {}
rect['low_left'] = (min(x1, x2), min(y1, y2))
rect['up_right'] = (max(x1, x2), max(y1, y2))
return rect


def get_x1(rect):
return rect["low_left"][0]


def get_y1(rect):
return rect["low_left"][1]


def get_x2(rect):
return rect["up_right"][0]


def get_y2(rect):
return rect["up_right"][1]


def rect_equal(rect1, rect2: dict):
# TODO Make sure that coordinates are in order :)
return (rect1['low_left'] == rect2['low_left']) and rect1['up_right'] == rect2['up_right']
return get_x1(rect1) == get_x1(rect2) and get_x2(rect1) == get_x2(rect2) and get_y1(rect1) == get_y1(
rect2) and get_y2(rect1) == get_y2(rect2)


def to_str(rect):
return "{} {} | {} {}".format(get_x1(rect), get_y1(rect), get_x2(rect), get_y2(rect))


#
Expand Down Expand Up @@ -119,7 +140,7 @@ def gen_rectangles(count: int):
# rects = gen_rectangles(5)
# print(rects)

def addRectangle(rectangles: list, new_rect):
def add_rectangle(rectangles: list, new_rect):
"""
:param rectangles: list
:param x1,x2,y1,y2: int
Expand All @@ -128,14 +149,14 @@ def addRectangle(rectangles: list, new_rect):
updated list if rectangle is ok
"""
# newRectangle = [x1, y1, x2, y2]
newRectangle = create_rect()
# newRectangle = create_rect()
# if x1 > x2 or y1 > y2:
# return None
for rectangle in rectangles:
if rect_equal(rectangle, newRectangle):
if rect_equal(rectangle, new_rect):
return None

rectangles.append(newRectangle)
rectangles.append(new_rect)
return rectangles


Expand All @@ -145,26 +166,46 @@ def addRectangle(rectangles: list, new_rect):
# Write all functions that have input or print statements here
# Ideally, this section should not contain any calculations relevant to program functionalities
#

def show_all_rect(rect_list: list):
print("Rectangle list:")
for rect in rect_list:
print(to_str(rect))


# Chisleac Remus
def read_rect_ui():
pass
x1 = int(input("x1="))
y1 = int(input("y1="))
x2 = int(input("x2="))
y2 = int(input("y2="))

return create_rect(x1, y1, x2, y2)


def add_rectangle_ui(rectangles: list):
new_rect = read_rect_ui()
if new_rect is None:
print("Invalid rectangle. Cannot be added")
return

def add_rectangle_ui():
pass
if add_rectangle(rectangles, new_rect) is None:
print("Overlapping rectangles. Rectangle was not added.")


def start_menu():
opt = 0
rectangles = []
rectangles = gen_rectangles(1)
while True:
show_all_rect(rectangles)
print(rectangles)
print("1. Add a rectangle:\n",
"2. Delete a rectangle:\n",
"3. Show all rectangles:\n",
"4. Show rectangles that intersect a given one:\n",
"5. exit\n")
opt = input('>')
if opt == "1":
add_rectangle_ui()
add_rectangle_ui(rectangles)
elif opt == "2":
pass
elif opt == "3":
Expand Down
120 changes: 113 additions & 7 deletions src/seminar/group_915/seminar_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,25 @@ def equal_rect(rect1, rect2):
# return not (get_tr_corner(rect1) != get_tr_corner(rect2) or get_bl_corner(rect1) != get_bl_corner(rect2))


def area(rectangle):
return abs(get_tr_corner(rectangle)[0] - get_bl_corner(rectangle)[0]) * (
get_tr_corner(rectangle)[1] - get_bl_corner(rectangle)[1])


def intersect(rect1, rect2):
"""
Return True if and only if the rectangles intersect
:param rect1: First rectangle
:param rect2: Second rectangle
:return:
"""
# FIXME Replace mock implementation with the real one
return True


# Moga Denis Andrei
def to_string(rectangle):
return f"Bottom left corner: {get_bl_corner(rectangle)} -- Top right corner: {get_tr_corner(rectangle)}"
return f"Bottom left corner: {get_bl_corner(rectangle)} -- Top right corner: {get_tr_corner(rectangle)}, area is {area(rectangle)}"


#
Expand Down Expand Up @@ -123,9 +139,37 @@ def generate_rectangles(count: int):
return rectangles


list = generate_rectangles(5)
for rect in list:
print(to_string(rect))
# list = generate_rectangles(5)
# for rect in list:
# print(to_string(rect))


# Moga Denis-Andrei
def check_if_rectangle_exists(rectangle_to_check, list_of_rectangles):
"""
Args:
rectangle_to_check (List of tuples): The first rectangle represented as a list with two tuples.
list_of_rectangles (List of lists): List of lists containing rectangles.
Returns:
Boolean value: True if the rectangle already exists, False otherwise.
"""
for rectangle in list_of_rectangles:
if equal_rect(rectangle_to_check, rectangle):
return True

return False


def add_rectangle_to_list(rectangle, list_of_rectangles):
if not check_if_rectangle_exists(rectangle, list_of_rectangles):
# If the rectangle is unique, add it to the list.
list_of_rectangles.append(rectangle)
return "ok"

# Return None if that rectangle already exists in the list.
return None


#
Expand All @@ -135,6 +179,22 @@ def generate_rectangles(count: int):
# Ideally, this section should not contain any calculations relevant to program functionalities
#

def read_number(message):
number = input(message)
while not number.isnumeric():
print("Invalid input! Please enter an integer.")
number = input(message)
return int(number)


def read_rectangle():
x1 = read_number("x1 = ")
y1 = read_number("y1 = ")
x2 = read_number("x2 = ")
y2 = read_number("y2 = ")
return create_rect(x1, y1, x2, y2)


# Oniga Andrei Mihai
def clearScreen():
if os.name == "nt":
Expand All @@ -158,6 +218,42 @@ def readNumber(message):
return 0


def print_rectangles(rectangles: list):
for rect in rectangles:
print(to_string(rect))


def select_rectangle(rectangles):
"""
User selection of one rectangle
:param rectangles:
:return:
"""
index = 1
for rect in rectangles:
print(str(index) + " -> " + to_string(rect))
index += 1
selection = read_number("Select rectangle index: ")
# FIXME Handle case where selection is not a valid index
return rectangles[selection - 1]


def rect_intersection_ui(rectangles: list):
# Select a rectangle using the UI
selected_rect = select_rectangle(rectangles)
print("Selected rectangle -- " + to_string(selected_rect))

# Determine intersecting rects
intersected_rects = []
for rect in rectangles:
if intersect(selected_rect, rect) and rect != selected_rect:
intersected_rects.append(rect)
# Sort intersected rects by area
intersected_rects.sort(reverse=True, key=area)
# Prints them out sorted desc by area
print_rectangles(intersected_rects)


def printMainMenu():
print("Mein Menu:")
print(" 1. Add a rectangle to the list.")
Expand All @@ -168,18 +264,28 @@ def printMainMenu():


def main():
# rectangles = []
rectangles = generate_rectangles(10)

while True:
# clearScreen()
# print_rectangles(rectangles)
printMainMenu()
choice = readNumber("Action: ")
if choice == 1:
errorMessage("TODO: implement addition")
new_rect = read_rectangle()
if new_rect is None:
print("Invalid rectangle (a point or a line are not valid rectangles)")
else:
if add_rectangle_to_list(new_rect, rectangles) is None:
print("Duplicate rectangle. Cannot be added.")
elif choice == 2:
errorMessage("TODO: implement deletion")
elif choice == 3:
errorMessage("TODO: implement showing")
rectangles.sort(reverse=True, key=area)
print_rectangles(rectangles)
elif choice == 4:
errorMessage("TODO: implement intersections")
rect_intersection_ui(rectangles)
elif choice == 5:
break
else:
Expand Down
Loading

0 comments on commit be91218

Please sign in to comment.