diff --git a/src/TilesMath/LocalMath.cs b/src/TilesMath/LocalMath.cs new file mode 100644 index 0000000..1a0200a --- /dev/null +++ b/src/TilesMath/LocalMath.cs @@ -0,0 +1,9 @@ +namespace TilesMath; + +internal static class LocalMath +{ + public static int SimplePower(int x, int pow) + { + return (int)Math.Pow(x, pow); + } +} diff --git a/src/TilesMath/Tile.cs b/src/TilesMath/Tile.cs index 3237f0d..ef6528c 100644 --- a/src/TilesMath/Tile.cs +++ b/src/TilesMath/Tile.cs @@ -129,6 +129,24 @@ public IEnumerable ChildrenAtZoom(int zoom, Func? exclude = nu } } + /// + /// Returns the number of children at the given zoom level. + /// + /// The zoom level. + /// + public long ChildrenAtZoomCount(int zoom) + { + if (zoom < this.Zoom) throw new Exception("Cannot calculate sub tiles for a smaller zoom level"); + + if (zoom == this.Zoom) + { + return 1; + } + + var diff = zoom - this.Zoom; + return LocalMath.SimplePower(4, diff); + } + public bool Equals(Tile other) { return this.X == other.X && this.Y == other.Y && this.Zoom == other.Zoom; diff --git a/src/TilesMath/TilesMath.csproj b/src/TilesMath/TilesMath.csproj index fe62a04..8614b03 100644 --- a/src/TilesMath/TilesMath.csproj +++ b/src/TilesMath/TilesMath.csproj @@ -4,7 +4,7 @@ net6.0 enable enable - 0.0.10 + 0.0.11 TilesMath ANYWAYS BV A tiny library for tiles math. diff --git a/test/TilesMath.Tests/TileTests.cs b/test/TilesMath.Tests/TileTests.cs index b2d36f5..5db83b2 100644 --- a/test/TilesMath.Tests/TileTests.cs +++ b/test/TilesMath.Tests/TileTests.cs @@ -28,4 +28,37 @@ public void Tile_ParentAt_5ZoomsUp_ShouldEqual5thParentUp() var parent = tile.Parent?.Parent?.Parent?.Parent?.Parent ?? throw new Exception("No parent"); Assert.Equal(parent, tile.ParentAt(tile.Zoom - 5)); } + + [Fact] + public void Tile_ChildrenAtZoomCount_SameZoom_ShouldEqual1() + { + var tile = Tile.Create(1025, 4511, 14); + + Assert.Equal(1, tile.ChildrenAtZoomCount(14)); + } + + [Fact] + public void Tile_ChildrenAtZoomCount_Zoom1Higher_ShouldEqual4() + { + var tile = Tile.Create(1025, 4511, 14); + + Assert.Equal(4, tile.ChildrenAtZoomCount(15)); + } + + [Fact] + public void Tile_ChildrenAtZoomCount_Zoom2Higher_ShouldEqual16() + { + var tile = Tile.Create(1025, 4511, 14); + + Assert.Equal(16, tile.ChildrenAtZoomCount(16)); + } + + [Fact] + public void Tile_ChildrenAtZoomCount_Zoom0_ShouldEqual4ToThePowerOfZoom() + { + var tile = Tile.Create(0, 0, 0); + + Assert.Equal((int)Math.Pow(4, 14), tile.ChildrenAtZoomCount(14)); + Assert.Equal((int)Math.Pow(4, 5), tile.ChildrenAtZoomCount(5)); + } }