user1047
user1047

Reputation: 169

Range overlap in a 360 sector in Python

A 360 radian circle is separated by 30 degree sectors. There are sensors sending waves with a limited angle. Sample data as below. It means sensor 1 sends wave between 243 and 319 degree, while sensor2 can send wave to two angles, 63 to 139 and 241 to 305.

sensordata = pd.DataFrame({'sensor':['sensor1','sensor2','sensor2'], 'lower_bound':[243,63,241],'upper_bound':[319,139,305]})
    sensor  lower_bound  upper_bound
0  sensor1          243          319
1  sensor2           63          139
2  sensor2          241          305
.......

What I want is to detect in each sector which sensor can send wave, i.e, the sensor's wave intersect with that sector. For example, there's no sensor in sector 0-30, and 30-60, and sensor2 should be true in sector 60-90, etc.

sector 0, none
sector 30, none
sector 60, sensor2
sector 90, sensor2
......
sector 210, none
sector 240, sensor1 and sensor2,
......

I have tried below code. It always misses the first correct sector, for example, in sector 60-90, sensor2 should be True however below code gives nothing for that sector. Any suggestion to make it correct? Thanks.

        for sector in np.arange(0, 360, 30):
            affected_sensor = sensordata[sensordata.lower_bound <= sector]
            affected_sensor = affected_sensor[affected_sensor.upper_bound >= sector]
            print(sector, affected_sensor)

Upvotes: 0

Views: 190

Answers (1)

Prune
Prune

Reputation: 77860

You're making this far too hard. Find the lower and upper bounds as sector numbers by direct computation:

sec_lower = sector_data.lower_bound // 30
sec_upper = sector_data.upper_bound // 30
sector_coverage = list(range(sec_lower, sec_upper+1))

This gives you a list of the sectors covered by the sensor used at the moment. This is simple to accumulate in your preferred organization. If you have any sensors that wrap past 360, handle that special case dependent on your data representation. You didn't supply that notation, so I can't code a solution.

Upvotes: 0

Related Questions