Something went wrong. Try again.
Positions on the Earth for Zig: point-in-polygon, bounding boxes, and great-circle distance and bearing.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Rings: closed chains of positions, the boundaries polygons are made of.//!//! A ring here is a slice of positions in order, with straight edges between//! consecutive ones in longitude and latitude and one more edge from the last//! back to the first. GeoJSON writes a ring *closed*, its last position equal//! to its first (RFC 7946 §3.1.6); every function here gives the same answer//! for a ring written closed or open, since the closing edge is then of zero//! length and contributes nothing.//!//! The plane is longitude and latitude themselves, not a projection of them,//! which is how GeoJSON defines an edge (§3.1.1). Two consequences, both//! RFC 7946's rather than this library's://!//! * A ring that crosses the antimeridian has to be cut in two there//! (§3.1.9). Written across it, its edge from 179° to -179° runs the long//! way round, through 0°.//! * Near the poles an edge of constant latitude is not the shortest path//! between its ends, which a great circle would be. For an area a few//! hundred kilometres across, such as a weather warning, the difference is//! a few hundred metres at most.
const std = @import("std");const Position = @import("root.zig").Position;
/// Whether the ring is written closed: at least four positions, and the last/// the same place as the first. That is what RFC 7946 §3.1.6 requires of a/// ring, and the functions here do not need it.pub fn isClosed(ring: []const Position) bool { return ring.len >= 4 and ring[0].eql(ring[ring.len - 1]);}
/// Twice the area enclosed by the ring, in square degrees, positive when the/// ring runs counterclockwise: the shoelace formula.////// Square degrees are not an area anyone wants -- a degree of longitude is/// 111 km at the equator and nothing at a pole -- so this is for its sign,/// which is the ring's orientation.pub fn signedArea2(ring: []const Position) f64 { if (ring.len < 3) return 0; var sum: f64 = 0; var previous = ring[ring.len - 1]; for (ring) |current| { sum += previous.longitude * current.latitude - current.longitude * previous.latitude; previous = current; } return sum;}
/// Whether the ring runs counterclockwise, which RFC 7946 §3.1.6 asks of a/// polygon's exterior ring, holes running the other way. A ring enclosing no/// area at all is neither, and answers false.pub fn isCounterClockwise(ring: []const Position) bool { return signedArea2(ring) > 0;}
/// Whether `point` is inside the ring.////// An even–odd crossing test: a ray from the point towards increasing/// longitude crosses the boundary an odd number of times exactly when the/// point is inside, whichever way the ring runs and however concave it is./// This is W. Randolph Franklin's formulation, whose half-open comparison of/// latitudes counts a ray passing exactly through a vertex once rather than/// twice.////// A point exactly on an edge may be counted inside or outside. Floating/// point decides, and no answer for the boundary of a warned area is more/// right than the other.pub fn contains(ring: []const Position, point: Position) bool { if (ring.len < 3) return false; var inside = false; var previous = ring[ring.len - 1]; for (ring) |current| { if ((current.latitude > point.latitude) != (previous.latitude > point.latitude)) { // Where the edge crosses the point's latitude. The comparison // above guarantees the two latitudes differ, so the division is // safe. const crossing = (previous.longitude - current.longitude) * (point.latitude - current.latitude) / (previous.latitude - current.latitude) + current.longitude; if (point.longitude < crossing) inside = !inside; } previous = current; } return inside;}
const testing = std.testing;
fn square(comptime closed: bool) []const Position { const open = [_]Position{ .{ .longitude = 0, .latitude = 0 }, .{ .longitude = 10, .latitude = 0 }, .{ .longitude = 10, .latitude = 10 }, .{ .longitude = 0, .latitude = 10 }, }; const closed_ring = open ++ [_]Position{open[0]}; return if (closed) &closed_ring else &open;}
test isClosed { try testing.expect(isClosed(square(true))); try testing.expect(!isClosed(square(false))); try testing.expect(!isClosed(&.{ .{ .longitude = 0, .latitude = 0 }, .{ .longitude = 0, .latitude = 0 } }));}
test "orientation" { try testing.expect(isCounterClockwise(square(true))); try testing.expect(isCounterClockwise(square(false))); try testing.expectEqual(@as(f64, 200), signedArea2(square(true)));
var reversed: [5]Position = undefined; for (square(true), 0..) |p, i| reversed[4 - i] = p; try testing.expect(!isCounterClockwise(&reversed)); try testing.expectEqual(@as(f64, -200), signedArea2(&reversed));
try testing.expect(!isCounterClockwise(&.{}));}
test "contains, on a square, closed or open" { inline for (.{ true, false }) |closed| { const r = square(closed); try testing.expect(contains(r, .{ .longitude = 5, .latitude = 5 })); try testing.expect(contains(r, .{ .longitude = 0.001, .latitude = 9.999 })); try testing.expect(!contains(r, .{ .longitude = 15, .latitude = 5 })); try testing.expect(!contains(r, .{ .longitude = -5, .latitude = 5 })); try testing.expect(!contains(r, .{ .longitude = 5, .latitude = 10.5 })); }}
test "contains, on a concave ring" { // A U open at the top: the notch between the arms is outside. const u = [_]Position{ .{ .longitude = 0, .latitude = 0 }, .{ .longitude = 30, .latitude = 0 }, .{ .longitude = 30, .latitude = 30 }, .{ .longitude = 20, .latitude = 30 }, .{ .longitude = 20, .latitude = 10 }, .{ .longitude = 10, .latitude = 10 }, .{ .longitude = 10, .latitude = 30 }, .{ .longitude = 0, .latitude = 30 }, }; try testing.expect(contains(&u, .{ .longitude = 5, .latitude = 20 })); try testing.expect(contains(&u, .{ .longitude = 25, .latitude = 20 })); try testing.expect(contains(&u, .{ .longitude = 15, .latitude = 5 })); try testing.expect(!contains(&u, .{ .longitude = 15, .latitude = 20 }));}
test "a ray through a vertex is counted once" { // A diamond, and a point level with its left and right vertices. const diamond = [_]Position{ .{ .longitude = 0, .latitude = -1 }, .{ .longitude = 1, .latitude = 0 }, .{ .longitude = 0, .latitude = 1 }, .{ .longitude = -1, .latitude = 0 }, }; try testing.expect(contains(&diamond, .{ .longitude = 0, .latitude = 0 })); try testing.expect(!contains(&diamond, .{ .longitude = -2, .latitude = 0 })); try testing.expect(!contains(&diamond, .{ .longitude = 2, .latitude = 0 }));}
test "degenerate rings contain nothing" { try testing.expect(!contains(&.{}, .{ .longitude = 0, .latitude = 0 })); try testing.expect(!contains(&.{ .{ .longitude = 0, .latitude = 0 }, .{ .longitude = 1, .latitude = 1 } }, .{ .longitude = 0.5, .latitude = 0.5 }));}