I have a simulation in which thousands of agents (multi-pixel sprites) move to different directions. There are imaginary (not drawn in the space) detection lines in the simulation space, specified by their coordinates.

What is an efficient method of detecting and counting the agents which pass each of the lines? (only the first time an agent passes the line will be counted).

Two simple methods come to my mind:

1- In each simulation step (after all agents have moved one pixel) draw the imaginary lines and see which agents the lines cut, increase the counter for the lines and mark the agent as passed through the specific lines.

2- For each "agent move", check whether it is on the line (how? draw the line each time?) , then again save the line as passed and increase the counter of the line.

![enter image description here](https://i.stack.imgur.com/nTaaz.jpg)