Intersection - Envelope
-
04-07-2021 - |
Question
I have Envelope[][] extents = new Envelope[][];
construction. Each envelope has MinX
, MaxX
, MinY
and MaxY
properties and represents one tile of a grid (bottom-left and top-right point). Now I have another Envelope bounds;
which contains min and max values for X-
and Y-axis
. I want to get tiles from extents
which intersects with bounds
.
Is there any simple way to do this using Envelope.Intersect
?
[edit]
For now I did it in this way (brute-force xD):
List<Envelope> intersectedTiles = new List<Envelope>();
for (int i = 0; i < extents.LongLength; i++)
{
for (int j = 0; j < extents.Length; j++)
{
if (extents[i][j].MinX >= bounds.MinX && extents[i][j].MaxX <= bounds.MaxX &&
extents[i][j].MinY >= bounds.MinY && extents[i][j].MaxY <= bounds.MaxY)
{
intersectedTiles.Add(extents[i][j]);
}
}
}
Solution
private static bool Intersects(Envelope e1, Envelope e2)
{
return e1.MinX >= e2.MinX && e1.MaxX <= e2.MaxX && e1.MinY >= e2.MinY && e1.MaxY <= e2.MaxY;
}
List<Envelope> intersectedTiles = extents.SelectMany(es => es)
.Where(e => Intersects(e, bounds))
.ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow