int _getEdgeHexagons()

in src/h3lib/lib/algos.c [719:762]


int _getEdgeHexagons(const Geofence* geofence, int numHexagons, int res,
                     int* numSearchHexes, H3Index* search, H3Index* found) {
    for (int i = 0; i < geofence->numVerts; i++) {
        GeoCoord origin = geofence->verts[i];
        GeoCoord destination = i == geofence->numVerts - 1
                                   ? geofence->verts[0]
                                   : geofence->verts[i + 1];
        const int numHexesEstimate =
            lineHexEstimate(&origin, &destination, res);
        for (int j = 0; j < numHexesEstimate; j++) {
            GeoCoord interpolate;
            interpolate.lat =
                (origin.lat * (numHexesEstimate - j) / numHexesEstimate) +
                (destination.lat * j / numHexesEstimate);
            interpolate.lon =
                (origin.lon * (numHexesEstimate - j) / numHexesEstimate) +
                (destination.lon * j / numHexesEstimate);
            H3Index pointHex = H3_EXPORT(geoToH3)(&interpolate, res);
            // A simple hash to store the hexagon, or move to another place if
            // needed
            int loc = (int)(pointHex % numHexagons);
            int loopCount = 0;
            while (found[loc] != 0) {
                // If this conditional is reached, the `found` memory block is
                // too small for the given polygon. This should not happen.
                if (loopCount > numHexagons)
                    return HEX_HASH_OVERFLOW;  // LCOV_EXCL_LINE
                if (found[loc] == pointHex)
                    break;  // At least two points of the geofence index to the
                            // same cell
                loc = (loc + 1) % numHexagons;
                loopCount++;
            }
            if (found[loc] == pointHex)
                continue;  // Skip this hex, already exists in the found hash
            // Otherwise, set it in the found hash for now
            found[loc] = pointHex;

            search[*numSearchHexes] = pointHex;
            (*numSearchHexes)++;
        }
    }
    return 0;
}