diff --git a/Bin/SunnyUI.dll b/Bin/SunnyUI.dll index 82ff6cf7..667c29a9 100644 Binary files a/Bin/SunnyUI.dll and b/Bin/SunnyUI.dll differ diff --git a/Bin/SunnyUI.pdb b/Bin/SunnyUI.pdb index c7e089bf..0ffd38e1 100644 Binary files a/Bin/SunnyUI.pdb and b/Bin/SunnyUI.pdb differ diff --git a/SunnyUI.Demo/Bin/SunnyUI.Demo.exe b/SunnyUI.Demo/Bin/SunnyUI.Demo.exe index 667bf829..608cd633 100644 Binary files a/SunnyUI.Demo/Bin/SunnyUI.Demo.exe and b/SunnyUI.Demo/Bin/SunnyUI.Demo.exe differ diff --git a/SunnyUI.Demo/Bin/SunnyUI.dll b/SunnyUI.Demo/Bin/SunnyUI.dll index 82ff6cf7..667c29a9 100644 Binary files a/SunnyUI.Demo/Bin/SunnyUI.dll and b/SunnyUI.Demo/Bin/SunnyUI.dll differ diff --git a/SunnyUI/Static/UArray.cs b/SunnyUI/Static/UArray.cs index f8e5c4e9..077a1bb5 100644 --- a/SunnyUI/Static/UArray.cs +++ b/SunnyUI/Static/UArray.cs @@ -221,7 +221,7 @@ namespace Sunny.UI bool[] bts = new bool[len]; for (int i = 0; i < b.Length; i++) { - string str = ((long)b[i]).ToNumberString(MathEx.Characters.BINARY).PadLeft(8, '0'); + string str = b[i].ToNumberString(MathEx.Characters.BINARY).PadLeft(8, '0'); for (int j = 0; j < 8; j++) { bts[i * 8 + j] = str[j] == '1'; diff --git a/SunnyUI/Static/UMath.cs b/SunnyUI/Static/UMath.cs index 65a353c8..e0ad3843 100644 --- a/SunnyUI/Static/UMath.cs +++ b/SunnyUI/Static/UMath.cs @@ -46,7 +46,7 @@ namespace Sunny.UI return Math.Sqrt(xx * xx + yy * yy); } - public static double CalcAngle(Point thisPoint,Point toPoint) + public static double CalcAngle(Point thisPoint, Point toPoint) { double az = Math.Atan2(thisPoint.Y - toPoint.Y, thisPoint.X - toPoint.X) * 180 / Math.PI; az = (az - 270 + 720) % 360; @@ -60,6 +60,74 @@ namespace Sunny.UI return az; } + /// + /// 二分查找与最近值序号 + /// + /// 列表 + /// 值 + /// 最近值序号 + public static int BinarySearchNearIndex(this IList list, T target) where T : IComparable + { + int i = 0, j = list.Count - 1; + + if (target.CompareTo(list[0]) == -1) return 0; + if (target.CompareTo(list[j]) == 1) return list.Count - 1; + while (i <= j) + { + var mid = (i + j) / 2; + if (target.CompareTo(list[mid]) == 1) i = mid + 1; + if (target.CompareTo(list[mid]) == -1) j = mid - 1; + if (target.CompareTo(list[mid]) == 0) return mid; + } + + if (i - 1 < 0) return i; + + TypeCode typeCode = Type.GetTypeCode(target.GetType()); + switch (typeCode) + { + case TypeCode.SByte: + case TypeCode.Int16: + case TypeCode.Int32: + case TypeCode.Int64: + return target.ToString().ToLong() - list[i - 1].ToString().ToLong() > + list[i].ToString().ToLong() - target.ToString().ToLong() ? i : i - 1; + + case TypeCode.Byte: + case TypeCode.UInt16: + case TypeCode.UInt32: + case TypeCode.UInt64: + return target.ToString().ToULong() - list[i - 1].ToString().ToULong() > + list[i].ToString().ToULong() - target.ToString().ToULong() ? i : i - 1; + + case TypeCode.Single: + case TypeCode.Double: + return target.ToString().ToDouble() - list[i - 1].ToString().ToDouble() > + list[i].ToString().ToDouble() - target.ToString().ToDouble() ? i : i - 1; + + case TypeCode.Decimal: + return target.ToString().ToDecimal() - list[i - 1].ToString().ToDecimal() > + list[i].ToString().ToDecimal() - target.ToString().ToDecimal() ? i : i - 1; + + case TypeCode.DateTime: + return target.ToString().ToDateTime() - list[i - 1].ToString().ToDateTime() > + list[i].ToString().ToDateTime() - target.ToString().ToDateTime() ? i : i - 1; + + default: return i - 1; + } + } + + /// + /// 二分查找与最近值序号 + /// + /// 列表 + /// 值 + /// 最近值序号 + public static int BinarySearchNearIndex(this T[] list, T target) where T : IComparable + { + return BinarySearchNearIndex(list.ToList(), target); + } + + /* /// /// 二分查找与最近值序号 /// @@ -94,6 +162,27 @@ namespace Sunny.UI { return BinarySearch(list.ToList(), target); } + */ + + public static T CheckLowerLimit(this T obj, T lowerLimit) where T : IComparable + { + return obj.CompareTo(lowerLimit) == -1 ? lowerLimit : obj; + } + + public static T CheckUpperLimit(this T obj, T upperLimit) where T : IComparable + { + return obj.CompareTo(upperLimit) == 1 ? upperLimit : obj; + } + + public static T CheckRange(this T obj, T lowerLimit, T upperLimit) where T : IComparable + { + if (lowerLimit.CompareTo(upperLimit) == -1) + return obj.CheckLowerLimit(lowerLimit).CheckUpperLimit(upperLimit); + else if (lowerLimit.CompareTo(upperLimit) == 1) + return obj.CheckLowerLimit(upperLimit).CheckUpperLimit(lowerLimit); + else + return lowerLimit; + } /// /// 点是否在区域内 @@ -691,7 +780,7 @@ namespace Sunny.UI /// 进制格式 /// 自定义进制字符串 /// 结果 - public static string ToNumberString(this long number, Characters characters = Characters.DECIMAL, string customCharacter = CHARS_PUREUPPERCHAR) + public static string ToNumberString(this ulong number, Characters characters = Characters.DECIMAL, string customCharacter = CHARS_PUREUPPERCHAR) { string DisplayText = characters.DisplayText(); if (characters == Characters.Custom && !customCharacter.IsNullOrEmpty()) @@ -700,7 +789,7 @@ namespace Sunny.UI } List result = new List(); - long t = number; + ulong t = number; if (t == 0) { return DisplayText[0].ToString(); @@ -708,8 +797,8 @@ namespace Sunny.UI while (t > 0) { - long mod = t % DisplayText.Length; - t = Math.Abs(t / DisplayText.Length); + ulong mod = t % ((ulong)DisplayText.Length); + t /= ((ulong)DisplayText.Length); string character = DisplayText[Convert.ToInt32(mod)].ToString(); result.Insert(0, character); } @@ -717,6 +806,24 @@ namespace Sunny.UI return string.Join("", result.ToArray()); } + public static string ToNumberString(this uint number, Characters characters = Characters.DECIMAL, + string customCharacter = CHARS_PUREUPPERCHAR) + { + return ((ulong)number).ToNumberString(characters, customCharacter); + } + + public static string ToNumberString(this ushort number, Characters characters = Characters.DECIMAL, + string customCharacter = CHARS_PUREUPPERCHAR) + { + return ((ulong)number).ToNumberString(characters, customCharacter); + } + + public static string ToNumberString(this byte number, Characters characters = Characters.DECIMAL, + string customCharacter = CHARS_PUREUPPERCHAR) + { + return ((ulong)number).ToNumberString(characters, customCharacter); + } + /// /// 指定字符串转换为指定进制的数字形式 ///