Skip to content

Instantly share code, notes, and snippets.

@rjdp
Last active June 16, 2018 15:59
Show Gist options
  • Save rjdp/0cdfafb55142f577d93aaf1ae693403c to your computer and use it in GitHub Desktop.
Save rjdp/0cdfafb55142f577d93aaf1ae693403c to your computer and use it in GitHub Desktop.
Count number of times 2 things cross when moving in circular path
def count_cross(rph_a, rph_b, hours=1, pos_a=0, pos_b=0, opposite_dir=True):
import math
cross_count = 0
max_rounds = hours * max(rph_a, rph_b)
for i in range(int(math.ceil(max_rounds))):
round_fr = (max_rounds - i) if i == int(math.floor(max_rounds)) else 1
a_ahead = pos_a > pos_b if not pos_a == pos_b else "eq"
pos_a += (float(rph_a) / max(rph_a, rph_b)) * round_fr
pos_b += (float(rph_b) / max(rph_a, rph_b)) * round_fr
if opposite_dir:
pos_b = 1 - pos_b
if opposite_dir:
cross_count += 2 if pos_a % 1 == pos_b % 1 else 1
else:
if (
a_ahead != "eq"
and (a_ahead and pos_b >= pos_a)
or (not a_ahead and pos_b >= pos_a)
):
cross_count += 1
pos_a %= 1
pos_b %= 1
return cross_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment