Skip to content

Commit

Permalink
Added method to get number of children.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Feb 21, 2024
1 parent 00c8cd6 commit e57b613
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/TilesMath/LocalMath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TilesMath;

internal static class LocalMath
{
public static int SimplePower(int x, int pow)
{
return (int)Math.Pow(x, pow);
}
}
18 changes: 18 additions & 0 deletions src/TilesMath/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ public IEnumerable<Tile> ChildrenAtZoom(int zoom, Func<Tile, bool>? exclude = nu
}
}

/// <summary>
/// Returns the number of children at the given zoom level.
/// </summary>
/// <param name="zoom">The zoom level.</param>
/// <returns></returns>
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;
Expand Down
2 changes: 1 addition & 1 deletion src/TilesMath/TilesMath.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageVersion>0.0.10</PackageVersion>
<PackageVersion>0.0.11</PackageVersion>
<Title>TilesMath</Title>
<Authors>ANYWAYS BV</Authors>
<Description>A tiny library for tiles math.</Description>
Expand Down
33 changes: 33 additions & 0 deletions test/TilesMath.Tests/TileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit e57b613

Please sign in to comment.