Yes, I realized that later. My problem was that I couldn’t get it to work in Unity, but ultimately I was chasing the wrong rabbit, because I thought it worked differently.
I fully understood what AwayFromZero did. 2.1 = 2, 2.5 = 3 and 2.7 = 3. That’s what I wanted and it worked. But I thought ToZero did the same thing but to the opposite direction (since it’s under MidpointRounding, with AwayFromZero), i.e. 2.1 = 2, 2.5 = 2 and 2.7 = 3. But yeah, it rounds everything towards zero: 2.7 = 2. So yeah, wrong function for me. Weird it to be under MidpointRounding when it has nothing to do with midpoints.
My usecase is this:
I’m doing line of sight checks on a grid of squares. I draw a line from a center of a square to a center of another square and have to find every square it intersects along the way and check if there are any obstacles in those squares. Coordinates are fixed to the centers of the squares, so origin (0,0) is in the centre of a square and the right hand edge of that square is (0.5, 0), for example. I tried to look at different algorithms like Bresenham’s line algorithm, but I couldn’t use it, because I’m essentially “drawing” every pixel the line touches, if even slightly. So I did my own algorithm.
I’m calculating, column by column, which squares the line touches in that Y column. I calculate the intersection point the line enters the column and the intersection the line leaves the column (or the starting point and/or ending point of the line if we are at either end of the line). With those intersection points, I calculate which squares they belong to and then I will know the line goes through those two squares and every square in between in that column. Then I move on to the next column until the end of the line is reached
I calculate which squares the intersections belong to by rounding the Y value of the intersection point. If the line enters the column at, say (2.5, 1.2) and exits it at (3.5, 4.8), the Y values are rounded to 1 and 5 so I know the line touches the squares (3, 1) and (3, 5) and every square in between in that column.
So far so good. But, what about the case where the line enters the column at exactly the intersection of the squares, say (0.5, 0.5)? Is the line touching one or two squares when it enters or exits the column? It was a judgement call and I decided that the line touches only the square it enters to and/or leaves from when going through intersection of squares, it doesn’t touch the tangential squares. So only one square on each side.
Depending on the direction and angle of the line, I have to use different midpoint rounding rules, so for example a line going upwards, left to right, rounds midpoints up on the entry point and down on the exit point. That way both intersections are handled symmetrically and both intersections count only one square for that column.
And I don’t even have to calculate if the line goes through square intersections. Just by using correct midpoint rounding rules handles all cases, I don’t have to know exactly where the line enters a column or its angle. So yeah, the 45 degree angle was just an example, as there are of course infinitely many lines that can go through square intersections.
So that’s the gist of it. There really isn’t any way to use integers in this algorithm as the lines are arbitrary. Well, of course you could express the angles in ratios of integers or something, but KISS. I would guess this kind of algorithm has been done many times before, but I couldn’t find any that would fit my use exactly or ones that I’d understand. I suppose my algorithm isn’t the simplest or fastest, but at least it works and I understand it.