The virtual fence that rejected everyone
Dogwalk·

The virtual fence that rejected everyone

A walker registers the area where they work. A tutor opens the app, their address lands right in the middle of that area on the map. And the system answers that it does not serve that location.

It was not configuration, it was not missing data, it was not the map provider. It was a ray crossing the fence inside the polygon and counting the wrong intersections, because one of the comparisons used the wrong coordinate.

The tools I use on this project live in the skills store — the first one is free.

What was actually happening

On November 9, commit 711752f4 closed the issue with one direct sentence in the commit message: the ray casting used yi > lng and lat < ..., inverting the axes. The practical effect is the worst kind of failure: any point tested against a virtual fence always fell outside, even when it was inside. Service-area geofencing never matched. Ever.

The defective snippet was short enough to fit on a card:

const intersect = ((yi > lng) !== (yj > lng)) &&
  (lat < (xj - xi) * (lng - yi) / (yj - yi) + xi);

yi is the vertex latitude (polygon[i][0]), xi is the same vertex longitude (polygon[i][1]). The array convention is [lat, lng]. But the first comparison pits yi, a latitude, against lng, the longitude of the tested point. The second line repeats the mistake more subtly: it computes an interpolation between lng and yi — two magnitudes from different axes.

What makes this bug so quiet is that JavaScript has no distinct numeric type for latitude and longitude. Both are number. No linter complains, no compiler blocks it, no error shows up in production. The code runs, returns false with utter confidence, and the only thing the user sees is “outside the service area”.

And the behavior was not random: with Brazilian coordinates, latitude sits around -23 and longitude around -46. Comparing -23 against -46 always yields the same result. The parity condition of the ray casting never toggled, the internal inside counter never completed a valid crossing, and the return was false for any input.

How it was fixed

The fix was two lines, each axis compared against its own value:

const intersect = ((yi > lat) !== (yj > lat)) &&
  (lng < (xj - xi) * (lat - yi) / (yj - yi) + xi);

Now yi and yj, the vertex latitudes, are compared against lat, the point latitude. And the interpolation runs on lat - yi, consistent with the vertical axis. Same calculation, correct axes, and the virtual fence starts to work.

But what turned this fix into something worth telling was not the two lines. It was the decision not to repair without proof.

The map utilities module exported 17 functions and none of them had a test. File coverage was 0%. Among those functions was haversineDistance, which computes the real distance of a walk, and isPointInPolygon, which decides whether a walker serves an address. In other words: the two functions that fail most silently in the product were exactly the ones nobody exercised.

The commit added 239 lines of tests, 32 cases, covering all 17 functions. The expected values were not invented: they are known geodesic distances — São Paulo to Rio around 357 km, one degree of latitude at the equator around 111.32 km. isPointInPolygon got a roughly 1 km square around São Paulo and cases for inclusion, exclusion, degenerate polygon, point on a vertex and point on an edge.

And then came the part that closes the argument: the counter-proof. Reverting only the two fix lines makes one test fail — the case “point at the center is inside”. With the fix, all 32 pass. That is different from “I think I fixed it”. It is the demonstration that the test actually sees the bug, rather than merely following the code.

Why this matters

The first lesson is about types that do not exist. Latitude and longitude are the same type to the language, so the compiler will never warn that you swapped the axes. In any coordinate-handling code, the variable name and its position in the array are the only defense there is. It pays to write [lat, lng] explicitly everywhere, even when the array looks obvious.

The second is about the cost of a silent false. An error that throws shows up in the log and someone goes after it. An error that returns false with conviction is read as a legitimate business answer — “this walker does not serve here” — and can survive months without anyone doubting the calculation. The bug was not on the screen; it was in the premise.

The third is about where to put tests. The module at 0% coverage was not a peripheral file: it was the layer deciding price by distance and eligibility by area. The practical rule that came out of it is simple — a pure function that decides a business rule gets its first test before it gets to production, even when it looks like trivial math. Trivial math with a swapped axis is exactly the kind of thing nobody reviews.

The project gate today closes at 223 test files and 2890 cases on the frontend. The number is not vanity: it is what lets you revert two lines and know, within seconds, whether the behavior went back to what it was.

Next step

The map module now has full coverage of its 17 functions, but the contract test that locks the [lat, lng] convention at the boundary — before any calculation — does not exist yet. That is the next piece: stop the error from coming back through a different path, instead of relying on someone remembering the axis order.

Related entries

Same project, same ground — the stories connect.

Next step

If this was useful, there is more: I write about the same ground in the other projects, and the store holds the tools I use here.

No spam, one-click unsubscribe. I am also here: Skills store · Portfolio