ScaleFactor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using Resources;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SmartCoalApplication.Core
  9. {
  10. /// <summary>
  11. /// 封装了缩放坐标的功能
  12. /// </summary>
  13. public struct ScaleFactor
  14. {
  15. /// <summary>
  16. /// 分母
  17. /// </summary>
  18. private int denominator;
  19. /// <summary>
  20. /// 分子
  21. /// </summary>
  22. private int numerator;
  23. public int Denominator
  24. {
  25. get
  26. {
  27. return denominator;
  28. }
  29. }
  30. public int Numerator
  31. {
  32. get
  33. {
  34. return numerator;
  35. }
  36. }
  37. /// <summary>
  38. /// 缩放比例
  39. /// </summary>
  40. public double Ratio
  41. {
  42. get
  43. {
  44. return (double)numerator / (double)denominator;
  45. }
  46. }
  47. public static readonly ScaleFactor OneToOne = new ScaleFactor(1, 1);
  48. public static readonly ScaleFactor MinValue = new ScaleFactor(1, 100);
  49. public static readonly ScaleFactor MaxValue = new ScaleFactor(32, 1);
  50. private void Clamp()
  51. {
  52. if (this < MinValue)
  53. {
  54. this = MinValue;
  55. }
  56. else if (this > MaxValue)
  57. {
  58. this = MaxValue;
  59. }
  60. }
  61. public static ScaleFactor UseIfValid(int numerator, int denominator, ScaleFactor lastResort)
  62. {
  63. if (numerator <= 0 || denominator <= 0)
  64. {
  65. return lastResort;
  66. }
  67. else
  68. {
  69. return new ScaleFactor(numerator, denominator);
  70. }
  71. }
  72. public static ScaleFactor Min(int n1, int d1, int n2, int d2, ScaleFactor lastResort)
  73. {
  74. ScaleFactor a = UseIfValid(n1, d1, lastResort);
  75. ScaleFactor b = UseIfValid(n2, d2, lastResort);
  76. return ScaleFactor.Min(a, b);
  77. }
  78. public static ScaleFactor Max(int n1, int d1, int n2, int d2, ScaleFactor lastResort)
  79. {
  80. ScaleFactor a = UseIfValid(n1, d1, lastResort);
  81. ScaleFactor b = UseIfValid(n2, d2, lastResort);
  82. return ScaleFactor.Max(a, b);
  83. }
  84. public static ScaleFactor Min(ScaleFactor lhs, ScaleFactor rhs)
  85. {
  86. if (lhs < rhs)
  87. {
  88. return lhs;
  89. }
  90. else
  91. {
  92. return rhs;
  93. }
  94. }
  95. public static ScaleFactor Max(ScaleFactor lhs, ScaleFactor rhs)
  96. {
  97. if (lhs > rhs)
  98. {
  99. return lhs;
  100. }
  101. else
  102. {
  103. return lhs;
  104. }
  105. }
  106. public static bool operator ==(ScaleFactor lhs, ScaleFactor rhs)
  107. {
  108. return (lhs.numerator * rhs.denominator) == (rhs.numerator * lhs.denominator);
  109. }
  110. public static bool operator !=(ScaleFactor lhs, ScaleFactor rhs)
  111. {
  112. return !(lhs == rhs);
  113. }
  114. public static bool operator <(ScaleFactor lhs, ScaleFactor rhs)
  115. {
  116. return (lhs.numerator * rhs.denominator) < (rhs.numerator * lhs.denominator);
  117. }
  118. public static bool operator <=(ScaleFactor lhs, ScaleFactor rhs)
  119. {
  120. return (lhs.numerator * rhs.denominator) <= (rhs.numerator * lhs.denominator);
  121. }
  122. public static bool operator >(ScaleFactor lhs, ScaleFactor rhs)
  123. {
  124. return (lhs.numerator * rhs.denominator) > (rhs.numerator * lhs.denominator);
  125. }
  126. public static bool operator >=(ScaleFactor lhs, ScaleFactor rhs)
  127. {
  128. return (lhs.numerator * rhs.denominator) >= (rhs.numerator * lhs.denominator);
  129. }
  130. public override bool Equals(object obj)
  131. {
  132. if (obj is ScaleFactor)
  133. {
  134. ScaleFactor rhs = (ScaleFactor)obj;
  135. return this == rhs;
  136. }
  137. else
  138. {
  139. return false;
  140. }
  141. }
  142. public override int GetHashCode()
  143. {
  144. return numerator.GetHashCode() ^ denominator.GetHashCode();
  145. }
  146. private static string percentageFormat = PdnResources.GetString("ScaleFactor.Percentage.Format");
  147. public override string ToString()
  148. {
  149. try
  150. {
  151. return string.Format(percentageFormat, unchecked(Math.Round(unchecked(100 * Ratio))));
  152. }
  153. catch (ArithmeticException)
  154. {
  155. return "--";
  156. }
  157. }
  158. public int ScaleScalar(int mul)
  159. {
  160. return (int)(((long)mul * numerator) / denominator);
  161. }
  162. public int UnscaleScalar(int mul)
  163. {
  164. return (int)(((long)mul * denominator) / numerator);
  165. }
  166. public float ScaleScalar(float mul)
  167. {
  168. return (mul * (float)numerator) / (float)denominator;
  169. }
  170. public float UnscaleScalar(float mul)
  171. {
  172. return (mul * (float)denominator) / (float)numerator;
  173. }
  174. public double ScaleScalar(double mul)
  175. {
  176. return (mul * (double)numerator) / (double)denominator;
  177. }
  178. public double UnscaleScalar(double mul)
  179. {
  180. return (mul * (double)denominator) / (double)numerator;
  181. }
  182. public Point ScalePoint(Point point)
  183. {
  184. return new Point(ScaleScalar(point.X), ScaleScalar(point.Y));
  185. }
  186. public PointF ScalePoint(PointF point)
  187. {
  188. return new PointF(ScaleScalar(point.X), ScaleScalar(point.Y));
  189. }
  190. public PointF ScalePointJustX(PointF point)
  191. {
  192. return new PointF(ScaleScalar(point.X), point.Y);
  193. }
  194. public PointF ScalePointJustY(PointF point)
  195. {
  196. return new PointF(point.X, ScaleScalar(point.Y));
  197. }
  198. public PointF UnscalePoint(PointF point)
  199. {
  200. return new PointF(UnscaleScalar(point.X), UnscaleScalar(point.Y));
  201. }
  202. public PointF UnscalePointJustX(PointF point)
  203. {
  204. return new PointF(UnscaleScalar(point.X), point.Y);
  205. }
  206. public PointF UnscalePointJustY(PointF point)
  207. {
  208. return new PointF(point.X, UnscaleScalar(point.Y));
  209. }
  210. public Point ScalePointJustX(Point point)
  211. {
  212. return new Point(ScaleScalar(point.X), point.Y);
  213. }
  214. public Point ScalePointJustY(Point point)
  215. {
  216. return new Point(point.X, ScaleScalar(point.Y));
  217. }
  218. public Point UnscalePoint(Point point)
  219. {
  220. return new Point(UnscaleScalar(point.X), UnscaleScalar(point.Y));
  221. }
  222. public Point UnscalePointJustX(Point point)
  223. {
  224. return new Point(UnscaleScalar(point.X), point.Y);
  225. }
  226. public Point UnscalePointJustY(Point point)
  227. {
  228. return new Point(point.X, UnscaleScalar(point.Y));
  229. }
  230. public SizeF ScaleSize(SizeF size)
  231. {
  232. return new SizeF(ScaleScalar(size.Width), ScaleScalar(size.Height));
  233. }
  234. public SizeF UnscaleSize(SizeF size)
  235. {
  236. return new SizeF(UnscaleScalar(size.Width), UnscaleScalar(size.Height));
  237. }
  238. public Size ScaleSize(Size size)
  239. {
  240. return new Size(ScaleScalar(size.Width), ScaleScalar(size.Height));
  241. }
  242. public Size UnscaleSize(Size size)
  243. {
  244. return new Size(UnscaleScalar(size.Width), UnscaleScalar(size.Height));
  245. }
  246. public RectangleF ScaleRectangle(RectangleF rectF)
  247. {
  248. return new RectangleF(ScalePoint(rectF.Location), ScaleSize(rectF.Size));
  249. }
  250. public RectangleF UnscaleRectangle(RectangleF rectF)
  251. {
  252. return new RectangleF(UnscalePoint(rectF.Location), UnscaleSize(rectF.Size));
  253. }
  254. public Rectangle ScaleRectangle(Rectangle rect)
  255. {
  256. return new Rectangle(ScalePoint(rect.Location), ScaleSize(rect.Size));
  257. }
  258. public Rectangle UnscaleRectangle(Rectangle rect)
  259. {
  260. return new Rectangle(UnscalePoint(rect.Location), UnscaleSize(rect.Size));
  261. }
  262. private static readonly double[] scales =
  263. {
  264. 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.08, 0.12, 0.16, 0.25, 0.33, 0.50, 0.66, 1,
  265. 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32
  266. };
  267. /// <summary>
  268. /// Gets a list of values that GetNextLarger() and GetNextSmaller() will cycle through.
  269. /// </summary>
  270. /// <remarks>
  271. /// 1.0 is guaranteed to be in the array returned by this property. This list is also
  272. /// sorted in ascending order.
  273. /// </remarks>
  274. public static double[] PresetValues
  275. {
  276. get
  277. {
  278. double[] returnValue = new double[scales.Length];
  279. scales.CopyTo(returnValue, 0);
  280. return returnValue;
  281. }
  282. }
  283. /// <summary>
  284. /// Rounds the current scaling factor up to the next power of two.
  285. /// </summary>
  286. /// <returns>The new ScaleFactor value.</returns>
  287. public ScaleFactor GetNextLarger()
  288. {
  289. double ratio = Ratio + 0.005;
  290. int index = Array.FindIndex(
  291. scales,
  292. delegate (double scale)
  293. {
  294. return ratio <= scale;
  295. });
  296. if (index == -1)
  297. {
  298. index = scales.Length;
  299. }
  300. index = Math.Min(index, scales.Length - 1);
  301. return ScaleFactor.FromDouble(scales[index]);
  302. }
  303. public ScaleFactor GetNextSmaller()
  304. {
  305. double ratio = Ratio - 0.005;
  306. int index = Array.FindIndex(
  307. scales,
  308. delegate (double scale)
  309. {
  310. return ratio <= scale;
  311. });
  312. --index;
  313. if (index == -1)
  314. {
  315. index = 0;
  316. }
  317. index = Math.Max(index, 0);
  318. return ScaleFactor.FromDouble(scales[index]);
  319. }
  320. private static ScaleFactor Reduce(int numerator, int denominator)
  321. {
  322. int factor = 2;
  323. while (factor < denominator && factor < numerator)
  324. {
  325. if ((numerator % factor) == 0 && (denominator % factor) == 0)
  326. {
  327. numerator /= factor;
  328. denominator /= factor;
  329. }
  330. else
  331. {
  332. ++factor;
  333. }
  334. }
  335. return new ScaleFactor(numerator, denominator);
  336. }
  337. public static ScaleFactor FromDouble(double scalar)
  338. {
  339. int numerator = (int)(Math.Floor(scalar * 1000.0));
  340. int denominator = 1000;
  341. return Reduce(numerator, denominator);
  342. }
  343. public ScaleFactor(int numerator, int denominator)
  344. {
  345. if (denominator <= 0)
  346. {
  347. throw new ArgumentOutOfRangeException("denominator", "must be greater than 0");
  348. }
  349. if (numerator < 0)
  350. {
  351. throw new ArgumentOutOfRangeException("numerator", "must be greater than 0");
  352. }
  353. this.numerator = numerator;
  354. this.denominator = denominator;
  355. this.Clamp();
  356. }
  357. }
  358. }