I want to calculate the intersection of two squares where the coordinates of the input square are given by the bottom left corner and the top right corner. The other square is 6 units wide and has a variable positive integer height h (to make the task simpler).
For that I defined some functions:
The first one is to make sure that the first two coordinates a1, a2 represent the bottom left corner and the two last coordinates represent the top right corner. That way, if someone would type the coordinates the other way around, e.g. the first two numbers being the coordinates of the top left corner and the two last numbers being the coordinates for the bottom right corner, convert_to_standard would switch the coordinates to the right place:
def convert_to_standard(a1,a2,b1,b2):
if a1 <= b1 and a2 <= b2:
return (a1,a2,b1,b2)
elif a1 >= b1 or a2 >= b2:
a_1 = min(a1,b1)
b_1 = max(a1,b1)
a_2 = min(a2,b2)
b_2 = max(a2,b2)
return (a_1,a_2,b_1,b_2)
Since I'm quite new to Python I was wondering if there are more elegant ways of making this happen.
I've also written a function to test if the squares even intersect, maybe there is way of making this better too: (The "return "incorrect" bit is for later, get_intersection_area returns the string "incorrect input" if h < 0) (Sorry if I'm overexplainig too much)
def intersects(h,a1,a2,b1,b2):
if h < 0:
return "incorrect"
a1,b1,a2,b2 = convert_to_standard(a1,b1,a2,b2)
if a1 > 6: #square is on the right side of R_h
return False
if b1 < 0: #square is on the left side of R_h
return False
if a2 > h: #square is above R_h
return False
if b2 < 0: #square is below R_h
return False
else:
return True
What also bothers me is that I'm not sure if the code runs functions needlessly. Specifically the function that calculates the width (get_delta_x1) and height (get_delta_x2) of the resulting square. I would like to only run it when the intersection is not empty and the input is correct (an incorrect input would be a negative value for h). Here is the complete code to check that:
def convert_to_standard(a1,a2,b1,b2):
if a1 <= b1 and a2 <= b2:
return (a1,a2,b1,b2)
elif a1 >= b1 or a2 >= b2:
a_1 = min(a1,b1)
b_1 = max(a1,b1)
a_2 = min(a2,b2)
b_2 = max(a2,b2)
return (a_1,a_2,b_1,b_2)
#checks if the input square intersects with the "given" square (whose height h has to be chosen)
def intersects(h,a1,a2,b1,b2):
if h < 0:
return "incorrect"
a1,b1,a2,b2 = convert_to_standard(a1,b1,a2,b2)
if a1 > 6: #square is on the right side of R_h
return False
if b1 < 0: #square is on the left side of R_h
return False
if a2 > h: #square is above R_h
return False
if b2 < 0: #square is below R_h
return False
else:
return True
#lenght of the resulting intersection square
def get_delta_x1(a1,b1):
if 0 <= a1 <= 6 and 0 <= b1 <= 6: #square is inside regarding x1
return b1 - a1
elif a1 < 0: #square sticks out on the left
return b1
elif b1 > 6: #square sitcks out on the right
return 6 - a1
#height of the resulting intersection square
def get_delta_x2(h,a2,b2):
if 0 <= a2 <= h and 0 <= b2 <= h: #square is inside regarding x2
return b2 - a2
elif a2 < 0: #square sticks out below
return b2
elif b2 > h: #square sticks out above
return h - a2
#area of the intersection
def get_intersection_area(h,a1,a2,b1,b2):
if intersects(h,a1,a2,b1,b2) == True:
A = get_delta_x1(a1,b1) * get_delta_x2(h,a2,b2)
return "The area of the resulting square is <"+str(A)+"> UA."
elif intersects(h,a1,a2,b1,b2) == False:
return "The intersection of the resulting squares is empty"
elif intersects(h,a1,a2,b1,b2) == "incorrect":
return "The input is incorrect"
Since this is a quick program, changes in the code almost won't change runtime. I just want to get more elegant for future projects where runtime will be an issue. I hope this is the right forum for that.
Thank you a lot in advance