PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Axes/TimeSpan/TimeSpanToDoubleConversion.cs

#
C# | 44 lines | 36 code | 8 blank | 0 comment | 0 complexity | 79aecbb8b679a31274ecc9e13dc7c249 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6namespace Microsoft.Research.DynamicDataDisplay.Charts
 7{
 8	internal sealed class TimeSpanToDoubleConversion
 9	{
10		public TimeSpanToDoubleConversion(TimeSpan minSpan, TimeSpan maxSpan)
11			: this(0, minSpan, 1, maxSpan)
12		{ }
13
14		public TimeSpanToDoubleConversion(double min, TimeSpan minSpan, double max, TimeSpan maxSpan)
15		{
16			this.min = min;
17			this.length = max - min;
18			this.ticksMin = minSpan.Ticks;
19			this.ticksLength = maxSpan.Ticks - ticksMin;
20		}
21
22		private double min;
23		private double length;
24		private long ticksMin;
25		private long ticksLength;
26
27		internal TimeSpan FromDouble(double d)
28		{
29			double ratio = (d - min) / length;
30			long ticks = (long)(ticksMin + ticksLength * ratio);
31
32			ticks = MathHelper.Clamp(ticks, TimeSpan.MinValue.Ticks, TimeSpan.MaxValue.Ticks);
33
34			return new TimeSpan(ticks);
35		}
36
37		internal double ToDouble(TimeSpan span)
38		{
39			double ratio = (span.Ticks - ticksMin) / (double)ticksLength;
40			return min + ratio * length;
41		}
42	}
43
44}