Repository: valkey-io/valkey
File: src/geohash_helper.c (C)

Code before:
     * so we check first for the latitude condition */
    double lat_distance = geohashGetLatDistance(y2, y1);
    if (lat_distance > height_m / 2) {
        return 0;
    }
    double lon_distance = geohashGetDistance(x2, y2, x1, y2);
    if (lon_distance > width_m / 2) {
        return 0;
    }
    *distance = geohashGetDistance(x1, y1, x2, y2);
    return 1;
}

/* Check if `point` is inside a polygon (defined by `vertices` where each vertex's index 0 is lon & 1 is lat) using
 * ray casting and calculate the distance from the centroid to the point.
 * The Polygon's centroid's lon lat coordinates are `centroidLon` and `centroidLat`.
 * The algorithm is based on PNPOLY - Point Inclusion in Polygon Test by W. Randolph Franklin (WRF).
 * See: https://wrfranklin.org/Research/Short_Notes/pnpoly.html
 * Returns 1 if inside the polyon and returns 0 otherwise. */
int geohashGetDistanceIfInPolygon(double centroidLon, double centroidLat, double *point, double (*vertices)[2], int num_vertices, double *distance) {
    int i, j;
    int inside = 0;
    for (i = 0, j = num_vertices - 1; i < num_vertices; j = i++) {
        double *vertexA = vertices[i];
        double *vertexB = vertices[j];
        if (((vertexA[1] > point[1]) != (vertexB[1] > point[1])) && (point[0] < (vertexB[0] - vertexA[0]) * (point[1] - vertexA[1]) / (vertexB[1] - vertexA[1]) + vertexA[0])) {
            inside = !inside;
        }
    }
    if (inside) {
        *distance = geohashGetDistance(centroidLon, centroidLat, point[0], point[1]);
    }
    return inside;
}


Review comment (suggestion):
This is the core of this algorithm. It's a very long line that's difficult to read. Let's line break it to make it easier to read.

In the original algorithm on the web page, it's split into two lines like below. Let's do at least this.

```suggestion
        if (((vertexA[1] > point[1]) != (vertexB[1] > point[1])) &&
            (point[0] < (vertexB[0] - vertexA[0]) * (point[1] - vertexA[1]) / (vertexB[1] - vertexA[1]) + vertexA[0])) {
```


Code after:
                                    double *distance) {
    /* latitude distance is less expensive to compute than longitude distance
     * so we check first for the latitude condition */
    double lat_distance = geohashGetLatDistance(y2, y1);
    if (lat_distance > height_m / 2) {
        return 0;
    }
    double lon_distance = geohashGetDistance(x2, y2, x1, y2);
    if (lon_distance > width_m / 2) {
        return 0;
    }
    *distance = geohashGetDistance(x1, y1, x2, y2);
    return 1;
}

/* Check if `point` is inside a polygon (defined by `vertices` where each vertex's index 0 is lon & 1 is lat) using
 * ray casting and calculate the distance from the centroid to the point.
 * The Polygon's centroid's lon lat coordinates are `centroidLon` and `centroidLat`.
 * The algorithm is based on PNPOLY - Point Inclusion in Polygon Test by W. Randolph Franklin (WRF).
 * See: https://wrfranklin.org/Research/Short_Notes/pnpoly.html
 * Returns 1 if inside the polyon and returns 0 otherwise. */
int geohashGetDistanceIfInPolygon(double centroidLon, double centroidLat, double *point, double (*vertices)[2], int num_vertices, double *distance) {
    int i, j;
    int inside = 0;
    for (i = 0, j = num_vertices - 1; i < num_vertices; j = i++) {
        double *vertexA = vertices[i];
        double *vertexB = vertices[j];
        if (((vertexA[1] > point[1]) != (vertexB[1] > point[1])) &&
            (point[0] < (vertexB[0] - vertexA[0]) * (point[1] - vertexA[1]) / (vertexB[1] - vertexA[1]) + vertexA[0])) {
            inside = !inside;
        }
    }
    if (inside) {
        *distance = geohashGetDistance(centroidLon, centroidLat, point[0], point[1]);
    }
    return inside;
}