# python: calculate the area of ​​intersection of two squares

I want to calculate the intersection of two squares where the coordinates of the input square are given by the lower left corner and the upper right corner. The other square is 6 units wide and has a variable positive integer height h (to simplify the task).

For that I defined some functions:

The first is to make sure that the first two coordinates a1, a2 represent the lower left corner, and the last two coordinates represent the upper right corner. That way, if someone were to write the coordinates backwards, p. the first two numbers are the coordinates of the upper left corner and the last two numbers are the coordinates of the lower right corner, convert_to_standard would change the coordinates to the correct 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 fairly new to Python, I was wondering if there are any fancier ways to make this happen.

I've also written a function to test if the squares even intersect, maybe there is a way to improve this too:
(The wrong "return" bit is for later, get_intersection_area returns the string "bad entry" if h <0) (Sorry if I'm overexplaining 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 works unnecessarily. Specifically, the function that calculates the width (get_delta_x1) and the height (get_delta_x2) of the resulting square. I would like to run it only 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 verify that: (For some reason I can't put everything in a code sample, sorry for 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)
``````

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
``````

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
``````

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
``````

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 fast program, code changes will hardly change runtime. I just want to be more stylish for future projects where runtime will be an issue. I hope this is the right forum for that.

Thank you very much in advance