Skip to content

Commit

Permalink
Fixed numeric instability.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Nov 15, 2023
1 parent 779f644 commit 027348a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 47 deletions.
113 changes: 67 additions & 46 deletions src/TilesMath/TileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,66 +130,87 @@ private static IEnumerable<Tile> EnumerateTilesForLineSegment((double longitude,
yield return tile;

// determine to change x or y.
var xDiff = point2Tile.Value.X - tileX;
var yDiff = point2Tile.Value.Y - tileY;

// if y at the next x is inside the current tile range (miny, maxy), we step x.
if (tileX != point2Tile.Value.X)
if (xDiff > 0)
{
if (point2Tile.Value.X > tileX)
if (yDiff == 0)
{
// try to move right.
var nextLongitude = tile.Boundaries.Right;
var latitude = GetY(nextLongitude);

if (latitude <= tile.Boundaries.Top &&
latitude >= tile.Boundaries.Bottom)
{
tileX += 1;
continue;
}
tileX += 1;
continue;
}
if (point2Tile.Value.X < tileX)

// try to move right.
var nextLongitude = tile.Boundaries.Right;
var latitude = GetY(nextLongitude);

if (latitude <= tile.Boundaries.Top &&
latitude >= tile.Boundaries.Bottom)
{
// try to move left.
var nextLongitude = tile.Boundaries.Left;
var latitude = GetY(nextLongitude);

if (latitude <= tile.Boundaries.Top &&
latitude >= tile.Boundaries.Bottom)
{
tileX -= 1;
continue;
}
tileX += 1;
continue;
}
}
if (xDiff < 0)
{
if (yDiff == 0)
{
tileX -= 1;
continue;
}

// try to move left.
var nextLongitude = tile.Boundaries.Left;
var latitude = GetY(nextLongitude);

if (latitude <= tile.Boundaries.Top &&
latitude >= tile.Boundaries.Bottom)
{
tileX -= 1;
continue;
}
}

// if x at the next y is inside the current tile range (minx, maxx), we step y.
if (tileY != point2Tile.Value.Y)
if (yDiff > 0)
{
if (point2Tile.Value.Y > tileY)
if (xDiff == 0)
{
// try to move down.
var nextLatitude = tile.Boundaries.Bottom;
var longitude = GetX(nextLatitude);

if (longitude >= tile.Boundaries.Left &&
longitude <= tile.Boundaries.Right)
{
tileY += 1;
continue;
}
tileY += 1;
continue;
}
if (point2Tile.Value.Y < tileY)

// try to move down.
var nextLatitude = tile.Boundaries.Bottom;
var longitude = GetX(nextLatitude);

if (longitude >= tile.Boundaries.Left &&
longitude <= tile.Boundaries.Right)
{
tileY += 1;
continue;
}
}

if (yDiff < 0)
{
if (xDiff == 0)
{
tileY -= 1;
continue;
}

// try to move up.
var nextLatitude = tile.Boundaries.Top;
var longitude = GetX(nextLatitude);

if (longitude >= tile.Boundaries.Left &&
longitude <= tile.Boundaries.Right)
{
// try to move up.
var nextLatitude = tile.Boundaries.Top;
var longitude = GetX(nextLatitude);

if (longitude >= tile.Boundaries.Left &&
longitude <= tile.Boundaries.Right)
{
tileY -= 1;
continue;
}
tileY -= 1;
continue;
}
}
}
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.8</PackageVersion>
<PackageVersion>0.0.9</PackageVersion>
<Title>TilesMath</Title>
<Authors>ANYWAYS BV</Authors>
<Description>A tiny library for tiles math.</Description>
Expand Down
11 changes: 11 additions & 0 deletions test/TilesMath.Tests/TileStaticTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,15 @@ public void Tile_BetweenLine_WhenLineToNorthEast_TwoTileHop_ShouldEnumerateTwoTi
Assert.Equal(8412, tiles[4].X);
Assert.Equal(5464, tiles[4].Y);
}

[Fact]
public void Tile_BelowTile_Regression1()
{
var tiles = Tile.BelowLine(new (double longitude, double latitude)[]
{
(-38.342295887706044, -4.0837899501379491),
(-38.34228515625, -4.0834527720386173)
}, 14).ToList();

}
}

0 comments on commit 027348a

Please sign in to comment.