// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Polygons with holes, and several of them together. //! //! A polygon is a slice of rings: the first is its exterior boundary and any //! others are holes in it, which is how GeoJSON writes one (RFC 7946 //! §3.1.6). A multipolygon is a slice of those. Neither is a type here, only //! a shape of slice, so that storage from anywhere -- a parsed GeoJSON //! document, a shapefile, an array literal -- can be asked about directly. const std = @import("std"); const Position = @import("root.zig").Position; const ring = @import("ring.zig"); /// Whether `point` is inside the polygon: inside its exterior ring and not /// inside any of its holes. A polygon with no rings contains nothing. pub fn contains(rings: []const []const Position, point: Position) bool { if (rings.len == 0) return false; if (!ring.contains(rings[0], point)) return false; for (rings[1..]) |hole| { if (ring.contains(hole, point)) return false; } return true; } /// Whether `point` is inside any of the polygons. pub fn multiContains(polygons: []const []const []const Position, point: Position) bool { for (polygons) |p| { if (contains(p, point)) return true; } return false; } /// Whether the polygon's rings run the way RFC 7946 §3.1.6 asks: the /// exterior counterclockwise and every hole clockwise. The specification /// says SHOULD, and parsers are told not to reject a polygon for it, so this /// is a question to ask rather than a rule anything here enforces -- /// `contains` does not care. pub fn followsRightHandRule(rings: []const []const Position) bool { if (rings.len == 0) return true; if (!ring.isCounterClockwise(rings[0])) return false; for (rings[1..]) |hole| { if (ring.isCounterClockwise(hole)) return false; } return true; } const testing = std.testing; const outer = [_]Position{ .{ .longitude = 0, .latitude = 0 }, .{ .longitude = 10, .latitude = 0 }, .{ .longitude = 10, .latitude = 10 }, .{ .longitude = 0, .latitude = 10 }, .{ .longitude = 0, .latitude = 0 }, }; // Clockwise, as a hole should be. const square_hole = [_]Position{ .{ .longitude = 4, .latitude = 4 }, .{ .longitude = 4, .latitude = 6 }, .{ .longitude = 6, .latitude = 6 }, .{ .longitude = 6, .latitude = 4 }, .{ .longitude = 4, .latitude = 4 }, }; test contains { const donut = [_][]const Position{ &outer, &square_hole }; try testing.expect(contains(&donut, .{ .longitude = 2, .latitude = 2 })); try testing.expect(!contains(&donut, .{ .longitude = 5, .latitude = 5 })); try testing.expect(!contains(&donut, .{ .longitude = 20, .latitude = 5 })); try testing.expect(!contains(&.{}, .{ .longitude = 5, .latitude = 5 })); const solid = [_][]const Position{&outer}; try testing.expect(contains(&solid, .{ .longitude = 5, .latitude = 5 })); } test multiContains { const away = [_]Position{ .{ .longitude = 20, .latitude = 20 }, .{ .longitude = 30, .latitude = 20 }, .{ .longitude = 30, .latitude = 30 }, .{ .longitude = 20, .latitude = 30 }, .{ .longitude = 20, .latitude = 20 }, }; const first = [_][]const Position{ &outer, &square_hole }; const second = [_][]const Position{&away}; const both = [_][]const []const Position{ &first, &second }; try testing.expect(multiContains(&both, .{ .longitude = 25, .latitude = 25 })); try testing.expect(multiContains(&both, .{ .longitude = 1, .latitude = 1 })); try testing.expect(!multiContains(&both, .{ .longitude = 5, .latitude = 5 })); try testing.expect(!multiContains(&.{}, .{ .longitude = 5, .latitude = 5 })); } test followsRightHandRule { try testing.expect(followsRightHandRule(&.{ &outer, &square_hole })); try testing.expect(!followsRightHandRule(&.{ &square_hole, &outer })); try testing.expect(!followsRightHandRule(&.{ &outer, &outer })); }