// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The rectangle of longitude and latitude around something. //! //! What a box is for is ruling things out cheaply: a point outside an area's //! box is outside the area, and two areas whose boxes do not meet do not //! meet, whatever their shapes. RFC 7946 §5 carries one as `bbox` for the //! same reason. //! //! A box whose `west` is greater than its `east` crosses the antimeridian: //! it runs from `west` eastwards through 180° and on to `east`, which is how //! §5.2 writes such a box. Fiji, say, is `west = 177, east = -178`. const std = @import("std"); const Position = @import("root.zig").Position; const BoundingBox = @This(); west: f64, south: f64, east: f64, north: f64, /// The world, as a box. pub const world: BoundingBox = .{ .west = -180, .south = -90, .east = 180, .north = 90 }; /// The smallest box, not crossing the antimeridian, that holds every one of /// `positions`; null for none. /// /// Not crossing, because which way round a set of points "should" go cannot /// be told from the points alone. A box around Fiji computed this way runs /// the long way round the globe; one that knows better can be built by hand /// or with `merge`. pub fn of(positions: []const Position) ?BoundingBox { if (positions.len == 0) return null; var box: BoundingBox = .{ .west = positions[0].longitude, .east = positions[0].longitude, .south = positions[0].latitude, .north = positions[0].latitude, }; for (positions[1..]) |p| { box.west = @min(box.west, p.longitude); box.east = @max(box.east, p.longitude); box.south = @min(box.south, p.latitude); box.north = @max(box.north, p.latitude); } return box; } /// Whether the box crosses the antimeridian. pub fn crossesAntimeridian(box: BoundingBox) bool { return box.west > box.east; } /// Whether `point` is inside the box, edges included. pub fn contains(box: BoundingBox, point: Position) bool { if (point.latitude < box.south or point.latitude > box.north) return false; if (box.crossesAntimeridian()) { return point.longitude >= box.west or point.longitude <= box.east; } return point.longitude >= box.west and point.longitude <= box.east; } /// Whether the two boxes share any point, edges included. pub fn intersects(a: BoundingBox, b: BoundingBox) bool { if (a.north < b.south or b.north < a.south) return false; // Each box's longitudes as one or two plain intervals, and any pair // overlapping is enough. var a_parts: [2][2]f64 = undefined; var b_parts: [2][2]f64 = undefined; for (a.longitudeIntervals(&a_parts)) |x| { for (b.longitudeIntervals(&b_parts)) |y| { if (x[0] <= y[1] and y[0] <= x[1]) return true; } } return false; } fn longitudeIntervals(box: BoundingBox, buffer: *[2][2]f64) []const [2]f64 { if (!box.crossesAntimeridian()) { buffer[0] = .{ box.west, box.east }; return buffer[0..1]; } buffer[0] = .{ box.west, 180 }; buffer[1] = .{ -180, box.east }; return buffer[0..2]; } /// The smallest box that holds both. /// /// Longitude is a circle, so "smallest" has two candidates -- starting at /// either box's west edge and going east far enough to take in the other -- /// and this takes the narrower. Two boxes on either side of the antimeridian /// therefore merge into one that crosses it rather than one spanning the /// rest of the world. pub fn merge(a: BoundingBox, b: BoundingBox) BoundingBox { const south = @min(a.south, b.south); const north = @max(a.north, b.north); const a_span = span(a); const b_span = span(b); // Starting at a's west: far enough to reach the end of b. const from_a = @max(a_span, eastward(a.west, b.west) + b_span); const from_b = @max(b_span, eastward(b.west, a.west) + a_span); const west, const width = if (from_a <= from_b) .{ a.west, from_a } else .{ b.west, from_b }; if (width >= 360) return .{ .west = -180, .south = south, .east = 180, .north = north }; var east = west + width; if (east > 180) east -= 360; return .{ .west = west, .south = south, .east = east, .north = north }; } /// How many degrees of longitude the box covers. fn span(box: BoundingBox) f64 { return if (box.crossesAntimeridian()) box.east - box.west + 360 else box.east - box.west; } /// How far east from `from` one goes to reach `to`, in [0, 360). fn eastward(from: f64, to: f64) f64 { return @mod(to - from, 360); } const testing = std.testing; test of { try testing.expectEqual(@as(?BoundingBox, null), of(&.{})); const box = of(&.{ .{ .longitude = -93.6, .latitude = 41.5 }, .{ .longitude = -87.6, .latitude = 41.9 }, .{ .longitude = -90, .latitude = 38.6 }, }).?; try testing.expectEqual(BoundingBox{ .west = -93.6, .south = 38.6, .east = -87.6, .north = 41.9 }, box); } test "contains, both kinds" { const plain: BoundingBox = .{ .west = -10, .south = -10, .east = 10, .north = 10 }; try testing.expect(plain.contains(.{ .longitude = 0, .latitude = 0 })); try testing.expect(plain.contains(.{ .longitude = 10, .latitude = 10 })); try testing.expect(!plain.contains(.{ .longitude = 11, .latitude = 0 })); try testing.expect(!plain.contains(.{ .longitude = 0, .latitude = -11 })); const fiji: BoundingBox = .{ .west = 177, .south = -21, .east = -178, .north = -12 }; try testing.expect(fiji.crossesAntimeridian()); try testing.expect(fiji.contains(.{ .longitude = 178, .latitude = -17 })); try testing.expect(fiji.contains(.{ .longitude = -179, .latitude = -17 })); try testing.expect(!fiji.contains(.{ .longitude = 0, .latitude = -17 })); } test intersects { const a: BoundingBox = .{ .west = 0, .south = 0, .east = 10, .north = 10 }; try testing.expect(a.intersects(.{ .west = 5, .south = 5, .east = 15, .north = 15 })); try testing.expect(a.intersects(.{ .west = 10, .south = 10, .east = 20, .north = 20 })); try testing.expect(!a.intersects(.{ .west = 11, .south = 0, .east = 20, .north = 10 })); try testing.expect(!a.intersects(.{ .west = 0, .south = 11, .east = 10, .north = 20 })); const fiji: BoundingBox = .{ .west = 177, .south = -21, .east = -178, .north = -12 }; try testing.expect(fiji.intersects(.{ .west = -179.5, .south = -20, .east = -179, .north = -15 })); try testing.expect(fiji.intersects(.{ .west = 170, .south = -20, .east = -170, .north = -15 })); try testing.expect(!fiji.intersects(.{ .west = -170, .south = -20, .east = 170, .north = -15 })); } test merge { const a: BoundingBox = .{ .west = 0, .south = 0, .east = 10, .north = 10 }; const b: BoundingBox = .{ .west = 20, .south = -5, .east = 30, .north = 5 }; try testing.expectEqual(BoundingBox{ .west = 0, .south = -5, .east = 30, .north = 10 }, a.merge(b)); try testing.expectEqual(a.merge(b), b.merge(a)); // Either side of the antimeridian: the short way, across it. const east_of: BoundingBox = .{ .west = 175, .south = 0, .east = 179, .north = 1 }; const west_of: BoundingBox = .{ .west = -179, .south = 0, .east = -175, .north = 1 }; const merged = east_of.merge(west_of); try testing.expect(merged.crossesAntimeridian()); try testing.expectEqual(@as(f64, 175), merged.west); try testing.expectEqual(@as(f64, -175), merged.east); // One inside the other. try testing.expectEqual(a, a.merge(.{ .west = 2, .south = 2, .east = 3, .north = 3 })); // Two that together go all the way round. const half: BoundingBox = .{ .west = -180, .south = 0, .east = 0, .north = 1 }; const other: BoundingBox = .{ .west = 0, .south = 0, .east = 180, .north = 1 }; const round = half.merge(other); try testing.expectEqual(@as(f64, -180), round.west); try testing.expectEqual(@as(f64, 180), round.east); }