Skip to content

Instantly share code, notes, and snippets.

@sinairv
Created May 14, 2012 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinairv/2693975 to your computer and use it in GitHub Desktop.
Save sinairv/2693975 to your computer and use it in GitHub Desktop.
Determining how two intervals overlap
/// <summary>
/// Provides tools to detect the kind of the overlap between two intervals
/// </summary>
public static class IntervalOverlap
{
/// <summary>
/// Detects kind of the overlap that the specified two ranges have.
/// </summary>
/// <param name="start1">The inclusive start of the first interval.</param>
/// <param name="end1">The exclusive end of the first interval.</param>
/// <param name="start2">The inclusive start of the second interval.</param>
/// <param name="end2">The exclusive end of the second interval.</param>
/// <returns></returns>
public static IntervalOverlapKinds Detect(int start1, int end1, int start2, int end2)
{
IntervalOverlapKinds relation = IntervalOverlapKinds.FirstAfterSecond;
if (start2 <= start1 && end1 <= end2)
{
// (--)
relation = IntervalOverlapKinds.FirstInsideSecond;
}
else if (start1 <= start2 && end2 <= end1)
{
// -()-
relation = IntervalOverlapKinds.FirstIncludesSecond;
}
else if (start1 < start2 && start2 < end1 && end1 <= end2)
{
// -(-)
relation = IntervalOverlapKinds.FirstBeforeAndInsideSecond;
}
else if (start2 <= start1 && start1 < end2 && end1 > end2)
{
// (-)-
relation = IntervalOverlapKinds.FirstInsideAndAfterSecond;
}
else if (start1 < start2 && end1 <= start2)
{
// --()
relation = IntervalOverlapKinds.FirstBeforeSecond;
}
else if (start1 >= end2)
{
// ()--
relation = IntervalOverlapKinds.FirstAfterSecond;
}
else
{
throw new Exception("Could not detect interval overlapping kinds");
}
return relation;
}
/// <summary>
/// Gets the sign corresponding to the given overlap kind.
/// -- means the first interval, and
/// () means the second interval.
/// </summary>
/// <param name="overlapKind">Kind of the overlap.</param>
/// <returns></returns>
public static string GetSign(IntervalOverlapKinds overlapKind)
{
switch (overlapKind)
{
case IntervalOverlapKinds.FirstInsideSecond:
return "(--)";
case IntervalOverlapKinds.FirstIncludesSecond:
return "-()-";
case IntervalOverlapKinds.FirstBeforeAndInsideSecond:
return "-(-)";
case IntervalOverlapKinds.FirstInsideAndAfterSecond:
return "(-)-";
case IntervalOverlapKinds.FirstBeforeSecond:
return "--()";
case IntervalOverlapKinds.FirstAfterSecond:
return "()--";
default:
throw new Exception("Unknown interval overlap type!");
}
}
}
/// <summary>
/// Enumerates all possible ways two intervals may overlap
/// </summary>
public enum IntervalOverlapKinds
{
/// <summary>
/// (--)
/// </summary>
FirstInsideSecond,
/// <summary>
/// -()-
/// </summary>
FirstIncludesSecond,
/// <summary>
/// -(-)
/// </summary>
FirstBeforeAndInsideSecond,
/// <summary>
/// (-)-
/// </summary>
FirstInsideAndAfterSecond,
/// <summary>
/// --()
/// </summary>
FirstBeforeSecond,
/// <summary>
/// ()--
/// </summary>
FirstAfterSecond
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment