3 ms·
In a lot of ways, the original implementation makes a lot more sense than this "optimized" solution. Something like the original brute force, compare all the po
by mlyle 6y ago
In a lot of ways, the original implementation makes a lot more sense than this "optimized" solution.
Something like the original brute force, compare all the points' distances variant is how I'd be inclined to write one of my unit tests for the grid alignment routines. Trying a few dozen points on a few dozen grid variants is a way to make sure I've not completely screwed up math-- whether off by one, rounding problems, etc, while applying sanity checks on the overall grid code base.
This binary search "optimized" abomination is... more difficult to read and reason about than either than the integer math or "brute-force" solutions, and slower to boot. All it has going for it is that it's not the absolute slowest choice.
It may not be integer math, because we've not been told if the grid is regular... but it certainly is still easier than this.
- projectdelphai 6y agoI mean maybe but if you told me you could take my code that runs in less than a second and make it more readable but it would take 30 minutes to run instead . . . hell no I'm not taking you up on your offer. I'll just comment and document the existing solution better and call it a day.
- mlyle 6y agoYou're missing the point. The solution here is faster than the absolute naive solution of finding all distances, but it is both slower and less readable than integer math grid conversion.. I could see why you would make the choice of the naive, slow solution. I could see why you would choose the fast, integer math grid conversion one. But I don't see why you would choose something slower than the optimum solution that is also much, much less readable. That is, the naive solution and the integer math solution are Pareto-optimal choices... and this abomination ain't.