/// <summary> /// Determines if the elementGridSpace occupies any of the same grid-space as this element. /// </summary> /// <param name="elementGridSpace">Element grid space to compare</param> /// <returns>True if this and the input elements overlay each other</returns> public bool intersects(ElementGridSpace elementGridSpace) { // For efficiency: // This is an O(N^2) complexity operation // Reduce complexity by iterating over smaller grid space return(occupiedGridSpaces.Count >= elementGridSpace.occupiedGridSpaces.Count ? isOverlaid(elementGridSpace, this) : isOverlaid(this, elementGridSpace)); }
/// <summary> /// Determines if the two input elements occupy any of the same grid-space as each other. /// For efficiency purposes, since this is an O(N^2) complexity operation, /// The smaller element should be input first. /// </summary> /// <param name="smallerTestElement">Smaller grid element</param> /// <param name="largerTestElement">Larger grid element</param> /// <returns>True of the two elements overlay each other on the gird.</returns> private static bool isOverlaid(ElementGridSpace smallerTestElement, ElementGridSpace largerTestElement) { // This will iterate through the smaller grid element's coordinates, and determines if // it has any of the larger element's grid coordinates match them. for (var i = smallerTestElement.leftColumn; i <= smallerTestElement.rightColumn; i++) { for (var j = smallerTestElement.topRow; j <= smallerTestElement.bottomRow; j++) { if (largerTestElement.hasCoordinates(i, j)) { return(true); } } } return(false); }