I am looking for the optimal algorithm in Swift 3x or 4 to determine if there are any clusters of the same color in the box. I need to do this calculation many times per second on iOS, so it would be advisable to use the acceleration angle? I am not sure how to approach this problem.
given a bit full of balls in dynamic motion, I would like, for example, to be able to apply numerical marks to balls, as in this diagram.
numbers will be updated in each frame to determine the size of their cluster.
there would be a reasonable finite distance, for example, r * 1.1to include a neighboring ball in a given cluster.
clusters of balls can fall into an array of clusters, perhaps ...

, Fermat. - ?