I'm trying to make a program to get azimuth value from two points using this code:
import math
def azimuth(x1, x2, y1, y2):
temp = (y2 - y1) / (x2 - x1)
res = math.degrees(abs(math.atan(temp)))
if x2 > x1 and y2 > y1: #Q1
res = 90 - res
elif x2 < x1 and y2 > y1: #Q2
res = (90 - res) * -1
elif x2 < x1 and y2 < y1: #Q3
res = (90 + res) * -1
elif x2 > x1 and y2 < y1: #Q4
res += 90
else:
raise ValueError('No point difference.')
return res
I've been able to get azimuth value with range (-180)-180. Next, I need to split it into two groups of azimuth value equaly based on range of value. The goal is to get two group which get the closest azimuth value.
The problem is that pairs of points on quadrant III ((-180)-(-90)) and quadrant IV (90-180) logically can be consider as close (e.g:(-179) and 179). About how to split it equally, I've been thinking of using sorted list of azimuth and using index idx = int(math.ceil((len(lst)-1)/2))
as the median to split the list. This is how I do it on code:
lst = [-60, -50, -40, -30, -20, -10, 10, 20, 30, 40, 50, 60]
def split_list(lst):
lst.sort()
idx = int(math.ceil((len(lst)-1)/2))
left = []
right = []
for i in lst:
if i < lst[idx]:
left.append(i)
else:
right.append(i)
print(left)
print(right)
return left, right
split_list(lst)
The code above will return list [-60, -50, -40, -30, -20, -10] and [10, 20, 30, 40, 50, 60] as the result.
Is there any idea of how to do this? With the current condition, I don't have any idea of how to make quadrant III and IV considered as close.
Your problem can be reduced to finding a distance between two phase points. I say phase because phase is wrapped (in your case % 360). Thus, you need to compare the distance both ways: clockwise and counter-clockwise. The simplest might be:
If the clockwise distance is the smallest, then the first term in
min()
will be your solution. If counter-clockwise, then the second term inmin()
will be your solution. To do grouping, you'd then calculate the distances between desired points. Note that arrays don't need to be sorted for this to work, just thatA1 <= A2
.