Can you give a little bit of context for why you want what you want? Such that most questions answer themselves.
Next clarifying question: Suppose you have a single curve, which is a large figure-eight in 2d. This curve has a self-intersection. Are you sure that you only want to count the grid square with the self-intersection as multiplicity one? As opposed to counting it with multiplicity two?
Next question: What about curves that donât intersect the interior of a grid cell, but that do have nonempty intersection with the boundary. I can imagine 3 reasonable answers: (1) tag the cell!, (2) donât tag the cell!, and (3) the curves are assumed in general position, so it doesnât matter (so throwing an exception is fine).
As an example, consider the 2d grid (1:1:100) x (1:1:100) and the line segment (2.5,2.5)-> (50,50). Is the grid cell 50:51 x 50:51 tagged? Is the grid cell 30:31 x 29:30 tagged?
Is your grid uniform, i.e. each axis is a linearly spaced grid? Are the spacings of each axis the same? Is the number of axis ticks on each axis the same? Is the total size of each axis the same (i.e. a cube) or do you rather have a rectangular grid?
In the following I am assuming the following partially clarified problem statement: The curves are considered as closed sets, i.e. including endpoints; grid cells are considered as closed sets, i.e. including boundaries, and are therefore grid cells are not mutually disjoint; a curve consisting of a single point can tag 8 cells if it sits on a grid point. Then you want to count, for each grid cell, the number of curves that have nonempty intersection with that grid cell.
Now we have a clarified real analysis problem statement. But weâre in the puny finitary realm of floating point numbers. So you need to tell us how you want to handle the approximations involved.
Do you really need an exact count? Your desired function is only upper semi-continuous. This suggests that you really only need to compute an upper bound (which you want to be reasonably tight).
To give a handwavy example: Suppose you have a real configuration where the curve does not intersect a grid-cell in Float64, but does intersect the grid-cell when viewed in Float32. Upper semi-continuity suggests that you should be OK with tagging the cell, even if you decide to work in the Float64 world. On the other hand, suppose your configuration is such that the cell doesnât intersect the curve in Float64, but it does intersect in Float128. Upper semi-continuity suggests that you want the cell tagged, even if you work in Float64 exclusively.
To go further on that, what does it mean for a curve to intersect a grid cell âin Float64â / âin Float32â / etc? There are two reasonablish definitions:
- The whole Float64 discretization scheme applies to all grid and curve parameters. Then you view it as a real analysis problem.
- For the two points p1, p2 you ask whether there exist a floating point number
0 <= t1 <= 1
and 0 <= t2 <= 1
such that t1+t2 == 1
and p1*t1 + t2*p2
lies in the grid cell, when everything is viewed as floating point. (note that it would be something different and very weird asymmetric to ask about 0 <= t <= 1
such that p1*t + (1-t)*p2
lies in the grid cell! Because values 0 < t2 < eps
cannot be expressed as t2 = (1-t1)
in float).
All this subtleties suggest that you really care about a reasonably tight upper bound, not about exact counts.