/// <summary> /// Split a domain evenly into N subdomains. /// </summary> /// <param name="n">The number of domains</param> /// <returns>An array of N equally-sized subdomains.</returns> public Domain1d[] DivideByCount(int n) { if (n <= 0) { throw new ArgumentException($"Unable to divide domain into {n} divisions."); } if (n < 2) { return(new Domain1d[] { this }); } var results = new Domain1d[n]; for (int i = 0; i < n; i++) { var from = ((1.0 / n) * i).MapToDomain(this); var to = ((1.0 / n) * (i + 1)).MapToDomain(this); results[i] = new Domain1d(from, to); } return(results); }
/// <summary> /// Map/Normalize a value from a domain to the domain (0,1). /// </summary> /// <param name="value">The value to map</param> /// <param name="domain">The domain to map from.</param> /// <returns>(value - domain.Min) / domain.Length</returns> public static double MapFromDomain(this double value, Domain1d domain) { return((value - domain.Min) / domain.Length); }
/// <summary> /// Map/scale a value between 0-1 to a target domain. Will not reject values outside 0-1. /// </summary> /// <param name="value">The value to map.</param> /// <param name="domain">The domain to map to.</param> /// <returns>value * domain.Length + domain.Min</returns> public static double MapToDomain(this double value, Domain1d domain) { return(value * domain.Length + domain.Min); }
/// <summary> /// Map/Scale a value from one domain to another. 3 mapped from (2,4) to (10, 20) would be 15. /// </summary> /// <param name="value">The value to map.</param> /// <param name="source">The source domain to map from.</param> /// <param name="target">The target domain to map to.</param> /// <returns></returns> public static double MapBetweenDomains(this double value, Domain1d source, Domain1d target) { return(value.MapFromDomain(source).MapToDomain(target)); }