Frame range into chunks
Question
How to make certain frame range (ie. 1-100) break into 4 equal frame ranges (like 1-25, 26-50, 51-75, 75-100 or anything similiar). I need first and last digit from every chunked frame range.
Solution
def chunk_range(first, last, howmany):
size = ((last - first + 1) + (howmany - 1)) // howmany
while first <= last:
next = first + size
yield first, min(next - 1, last)
first = next
list(chunk_range(1, 100, 4))
returns
[(1, 25), (26, 50), (51, 75), (76, 100)]
Do note that this makes all the segments of equal length except the last one - for instance,
list(chunk_range(1, 7, 3))
gives you
[(1, 3), (4, 6), (7, 7)] # last chunk is only one item
You may instead want to distribute the error along the sequence, a la Bresenham's algorithm.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow