Reputation: 97
My program is ultimately trying to output a direction from point a to point b based on the angle received and the quadrant(1, 2, 3, 4). I have created a dictionary with the keys as the angle slice(low, high) and the values as the direction.
I am then sending the keys (tupled keys) to a list. I want to know how to access the tupled keys(range) and see where the angle received falls within the specified values of the tupled list. For example: if i received an angle of 11, it would fall within the "00.00 - 11.25" range (tuple).
Once I have established the range the angle value is within, I can then, based off of the quadrant, derive the direction from the dictionary value associated with the key(tuple range) with an if statement of some sort.
def compute_direction(angle, quadrant):
directionDict = {
(00.00, 11.25): ["E", "N", "W", "S"],
(11.25, 33.75): ["ENE", "NNW", "WSW", "SSE"],
(33.75, 56.25): ["NE", "NW", "SW", "SE"],
(56.25, 78.75): ["NNE", "WNW", "SSW", "ESE"],
(78.75, 90.00): ["N", "W", "S", "E"]
}
directionKeys = directionDict.keys()
Upvotes: 1
Views: 214
Reputation: 2361
It seems that your ranges are of size 22.5. In such case, you can use integer division to calculate where the angle falls.
locations = [(00.00, 11.25), (11.25, 33.75), (33.75, 56.25), (56.25, 78.75), (78.75, 90.00)]
location_index = int((angle+11.25)//22.5)
current_location = locations[location_index]
In essence, all we want to do is divide the range [0,90] to 5 different segments. The external two are of length 11.25, and the middle ones are of length 22.5. To make it easier, we actually divide the segment to [-11.25,101.25] to 5 equal length segments. This division itself is done by integer division //
, returns the whole part of the division.
That is, [-11.25,11.25) will return 0
, [11.25,33.75) will return 1
and so on. Then, we can just map back these integers to the respective segments.
It is also possible to get directly the beginning of the segment instead of the segment itself, using similar code.
Upvotes: 2