I have two very similar functions that is almost a duplicate of each other except that the conditions are opposite to each other. I want to follow the DRY pattern, but I have read somewhere before that passing a boolean into a function to change behavior is a bad practice(since its difficult to maintain). What is my best option here for me to write clean and non repetitive code? Thanks
def find_upper_tangent(left, right):
l_idx = get_rightmost(left)
r_idx = get_leftmost(right)
upper_tangent_left = False
upper_tangent_right = False
prev_slope = get_slope(left[l_idx], right[r_idx])
while not upper_tangent_left or not upper_tangent_right:
while not upper_tangent_left:
# move counter clockwise
next_slope = get_slope(left[(l_idx - 1) % len(left)], right[r_idx])
if next_slope < prev_slope:
l_idx = (l_idx - 1) % len(left)
prev_slope = next_slope
upper_tangent_right = False
else:
upper_tangent_left = True
while not upper_tangent_right:
# move clockwise
next_slope = get_slope(left[l_idx], right[(r_idx + 1) % len(right)])
if next_slope > prev_slope:
r_idx = (r_idx + 1) % len(right)
prev_slope = next_slope
upper_tangent_left = False
else:
upper_tangent_right = True
return l_idx, r_idx
def find_lower_tangent(left, right):
l_idx = get_rightmost(left)
r_idx = get_leftmost(right)
lower_tangent_left = False
lower_tangent_right = False
prev_slope = get_slope(left[l_idx], right[r_idx])
while not lower_tangent_left or not lower_tangent_right:
while not lower_tangent_left:
# move clockwise
next_slope = get_slope(left[(l_idx + 1) % len(left)], right[r_idx])
if next_slope > prev_slope:
l_idx = (l_idx + 1) % len(left)
prev_slope = next_slope
lower_tangent_right = False
else:
lower_tangent_left = True
while not lower_tangent_right:
# move counter clockwise
next_slope = get_slope(left[l_idx], right[(r_idx - 1) % len(right)])
if next_slope < prev_slope:
r_idx = (r_idx - 1) % len(right)
prev_slope = next_slope
lower_tangent_left = False
else:
lower_tangent_right = True
return l_idx, r_idx
Aucun commentaire:
Enregistrer un commentaire