0

I have a problem. I'm finding an intersection using the code below:

Envelope[][] extents = new Envelope[tilesCountX][tilesCountY];

// some code here to set "extents" values

var intersectedTiles =
    extents
    .SelectMany(es => es)
    .Where(e => EnvIntersects(e, bounds))
    .ToList();

private static bool EnvIntersects(Envelope e1, Envelope e2)
{
    return e1.MinX >= e2.MinX && e1.MaxX <= e2.MaxX && e1.MinY >= e2.MinY && e1.MaxY <= e2.MaxY;
}

It works but I want to get the indexes of intersected extents.

e.g.
If extents[2][7] is an intersected element, I want to get 2 and 7.

Is it possible by modifying my code?

[edit]

bounds is an Envelope that has MinX, MinY, MaxX and MaxY properties inside.

Envelope bounds = new Envelope();
bounds.MinX = some_value_1;
bounds.MaxX = some_value_2;
bounds.MinY = some_value_3;
bounds.MaxY = some_value_4;
Nickon
  • 9,652
  • 12
  • 64
  • 119

2 Answers2

4

Try this:

var intersectedTiles =
extents
.SelectMany((es,i) => es.Select((x,j)=>new{I=i,J=j,Element = x}))
.Where(e => EnvIntersects(e.Element, bounds))
.ToList();

where for all elements in intersectedTiles this is true:

intersectedTiles[x].Element == extents[intersectedTiles[x].I][intersectedTiles[x].J]
Rafal
  • 12,391
  • 32
  • 54
4

I think this could also give you what you want:

var intersectedIndices =
    from x in Enumerable.Range(0, tilesCountX)
    from y in Enumerable.Range(0, tilesCountY)
    where EnvIntersects(extents[x, y], bounds)
    select new { x, y };
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137