diff options
author | Florian Jung <flo@windfisch.org> | 2015-08-24 22:30:33 +0200 |
---|---|---|
committer | Florian Jung <flo@windfisch.org> | 2015-08-24 22:30:33 +0200 |
commit | 1cdfcb0bce30f3debe31ec3c561e5171fda947fd (patch) | |
tree | 5745e3615a225da40e7d42dfe082b0a8094f030c | |
parent | 0dd92dbb7f1cc852f5225074908738a8594d49a3 (diff) |
more interval util functions
-rw-r--r-- | interval_utils.py | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/interval_utils.py b/interval_utils.py index 0095c06..7123893 100644 --- a/interval_utils.py +++ b/interval_utils.py @@ -70,13 +70,37 @@ def intervals_intersect(int1_, int2_): return False -def check_cell_in_interval(origin, cell, interval): +def intersection(int1s, int2s): + #expects merged, canonicalized intervals, returns overlap-free canonicalized intervals + + result = [] + + for int1 in int1s: + for int2 in int2s: + if (max(int1[0],int2[0]) <= min(int1[1],int2[1])): + result += [(max(int1[0],int2[0]), min(int1[1],int2[1]))] + + return result + +def interval_area(ints): + result = 0 + for i in merge_intervals(ints): + result += i[1]-i[0] + return result + +def interval_occupied_by_cell(origin, cell): ang = get_point_angle(origin, cell.pos) dist = math.sqrt( (cell.pos[0]-origin[0])**2 + (cell.pos[1]-origin[1])**2 ) - corridor_halfwidth = math.asin(cell.size / dist) + if cell.size >= dist: + corridor_halfwidth = math.pi/2 + else: + corridor_halfwidth = math.asin(cell.size / dist) + + return (ang-corridor_halfwidth, ang+corridor_halfwidth) - return intervals_intersect(interval, (ang-corridor_halfwidth, ang+corridor_halfwidth)) +def check_cell_in_interval(origin, cell, interval): + return intervals_intersect(interval, interval_occupied_by_cell(origin,cell)) def get_cells_in_interval(origin, interval, cells): return list(filter(lambda x: check_point_in_interval(origin, x.pos, interval), cells)) |