Turns out I also suck at programming.

The tolerance that I introduced to conditions on single coordinates lead to
double counting intersections and also was not needed at all: If p happens to
be close to b, on_boundary would be thrown at the next iteration anyway.
This just re-arranges some lines for comprehensibility.