I\'m looking an interval data type for .NET 4.0. For example the interval (a,b], all point x such that a What i would like to be able to do are create interv
I implemented this a long time ago for the .NET framework, and even included support for DateTime
and TimeSpan
, as this was one of my main use cases (implementation of a time line in WPF). My implementation supports all you request, except unbounded intervals. This allowed me to do cool stuff like:
// Mockup of a GUI element and mouse position.
var timeBar = new { X = 100, Width = 200 };
int mouseX = 180;
// Find out which date on the time bar the mouse is positioned on,
// assuming it represents whole of 2014.
var timeRepresentation = new Interval( timeBar.X, timeBar.X + timeBar.Width );
DateTime start = new DateTime( 2014, 1, 1 );
DateTime end = new DateTime( 2014, 12, 31 );
var thisYear = new Interval( start, end );
DateTime hoverOver = timeRepresentation.Map( mouseX, thisYear );
// If the user clicks, zoom in to this position.
double zoomLevel = 0.5;
double zoomInAt = thisYear.GetPercentageFor( hoverOver );
Interval zoomed = thisYear.Scale( zoomLevel, zoomInAt );
// Iterate over the interval, e.g. draw labels.
zoomed.EveryStepOf( TimeSpan.FromDays( 1 ), d => DrawLabel( d ) );
More recently, I have ported a base AbstractInterval
The biggest down side to this is there are quite a few dependencies all over the place (thus copy pasting part of this project will be hard). The reason for this is the implementation of this is not trivial at all (unlike others who have commented on this). In case there still aren't any good 'interval' libraries for .NET I should really make this a separate package. The .NET Standard library is available on Nuget.