AlignmentsHelp.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Markup;
  9. namespace ComPDFKit.Tool
  10. {
  11. /// <summary>
  12. /// Help class for calculating the alignment position of the rectangle
  13. /// </summary>
  14. public class AlignmentsHelp
  15. {
  16. /// <summary>
  17. /// Set the source rectangle to be left-aligned in the target rectangle
  18. /// </summary>
  19. /// <param name="src">
  20. /// Origin rectangle
  21. /// </param>
  22. /// <param name="dst">
  23. /// Target rectangle
  24. /// </param>
  25. /// <returns>
  26. /// X Y direction distance required for alignment of the source rectangle
  27. /// </returns>
  28. public static Point SetAlignLeft(Rect src, Rect dst)
  29. {
  30. Point movePoint = new Point(dst.Left - src.Left, 0);
  31. return movePoint;
  32. }
  33. /// <summary>
  34. /// Set the source rectangle to be horizontally centered in the target rectangle
  35. /// </summary>
  36. /// <param name="src">
  37. /// Origin rectangle
  38. /// </param>
  39. /// <param name="dst">
  40. /// Target rectangle
  41. /// </param>
  42. /// <returns>
  43. /// X Y direction distance required for alignment of the source rectangle
  44. /// </returns>
  45. public static Point SetAlignHorizonCenter (Rect src, Rect dst)
  46. {
  47. Point movePoint = new Point((dst.Left + dst.Right - src.Left - src.Right) / 2, 0);
  48. return movePoint;
  49. }
  50. /// <summary>
  51. /// Right-align the source rectangle in the target rectangle
  52. /// </summary>
  53. /// <param name="src">
  54. /// Origin rectangle
  55. /// </param>
  56. /// <param name="dst">
  57. /// Target rectangle
  58. /// </param>
  59. /// <returns>
  60. /// X Y direction distance required for alignment of the source rectangle
  61. /// </returns>
  62. public static Point SetAlignRight(Rect src, Rect dst)
  63. {
  64. Point movePoint = new Point(dst.Right - src.Width - src.Left, 0);
  65. return movePoint;
  66. }
  67. /// <summary>
  68. /// Set the source rectangle to be top-aligned in the target rectangle
  69. /// </summary>
  70. /// <param name="src">
  71. /// Origin rectangle
  72. /// </param>
  73. /// <param name="dst">
  74. /// Target rectangle
  75. /// </param>
  76. /// <returns>
  77. /// X Y direction distance required for alignment of the source rectangle
  78. /// </returns>
  79. public static Point SetAlignTop(Rect src, Rect dst)
  80. {
  81. Point movePoint = new Point(0, dst.Top - src.Top);
  82. return movePoint;
  83. }
  84. /// <summary>
  85. /// Set the source rectangle to be vertically centered in the target rectangle
  86. /// </summary>
  87. /// <param name="src">
  88. /// Origin rectangle
  89. /// </param>
  90. /// <param name="dst">
  91. /// Target rectangle
  92. /// </param>
  93. /// <returns>
  94. /// X Y direction distance required for alignment of the source rectangle
  95. /// </returns>
  96. public static Point SetAlignVerticalCenter(Rect src, Rect dst)
  97. {
  98. Point movePoint = new Point(0, (dst.Bottom + dst.Top - src.Top - src.Bottom) / 2);
  99. return movePoint;
  100. }
  101. /// <summary>
  102. /// Set the source rectangle to be horizontally and vertically centered in the target rectangle
  103. /// </summary>
  104. /// <param name="src">
  105. /// Origin rectangle
  106. /// </param>
  107. /// <param name="dst">
  108. /// Target rectangle
  109. /// </param>
  110. /// <returns>
  111. /// X Y direction distance required for alignment of the source rectangle
  112. /// </returns>
  113. public static Point SetAlignHorizonVerticalCenter(Rect src, Rect dst)
  114. {
  115. Point movePoint = new Point((dst.Left + dst.Right - src.Left - src.Right) / 2, (dst.Bottom + dst.Top - src.Top - src.Bottom) / 2);
  116. return movePoint;
  117. }
  118. /// <summary>
  119. /// Set the source rectangle to be bottom-aligned in the target rectangle
  120. /// </summary>
  121. /// <param name="src">
  122. /// Origin rectangle
  123. /// </param>
  124. /// <param name="dst">
  125. /// Target rectangle
  126. /// </param>
  127. /// <returns>
  128. /// X Y direction distance required for alignment of the source rectangle
  129. /// </returns>
  130. public static Point SetAlignBottom(Rect src, Rect dst)
  131. {
  132. Point movePoint = new Point(0, dst.Bottom - src.Height - src.Top);
  133. return movePoint;
  134. }
  135. /// <summary>
  136. /// Set the source rectangle to be horizontally distributed and aligned in the target rectangle
  137. /// </summary>
  138. /// <param name="src">
  139. /// Array of source rectangles needed
  140. /// </param>
  141. /// <param name="dst">
  142. /// Target rectangle
  143. /// </param>
  144. /// <returns>
  145. /// Dictionary of XY direction distance required for alignment of each source rectangle
  146. /// </returns>
  147. public static Dictionary<Rect, Point> SetDistributeHorizontal(List<Rect> src, Rect dst)
  148. {
  149. Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
  150. List<double> Leftlist = new List<double>();
  151. // Sort the data according to the leftmost position of each rectangle, not the array order
  152. foreach (Rect srcRect in src)
  153. {
  154. Leftlist.Add(srcRect.Left + srcRect.Width / 2);
  155. }
  156. double[] datalist = Leftlist.ToArray();
  157. Sort(datalist, 0, Leftlist.Count - 1);
  158. double startX = dst.Left;
  159. double endX = dst.Right;
  160. double interval = (endX - startX) / Leftlist.Count;
  161. for (int i = 0; i < datalist.Count(); i++)
  162. {
  163. int index = Leftlist.IndexOf(datalist[i]);
  164. Point movePoint = new Point(startX + i * interval - src[index].Left - src[index].Width / 2, 0);
  165. dictionary.Add(src[index], movePoint);
  166. }
  167. return dictionary;
  168. }
  169. /// <summary>
  170. /// Vertically distribute the source rectangles within the target rectangle (sorting based on the leftmost position of each rectangle, not the array order)
  171. /// </summary>
  172. /// <param name="src">
  173. /// Array of source rectangles needed
  174. /// </param>
  175. /// <param name="dst">
  176. /// Target rectangle
  177. /// </param>
  178. /// <returns>
  179. /// Dictionary of XY direction distance required for alignment of each source rectangle
  180. /// </returns>
  181. public static Dictionary<Rect, Point> SetDistributeVertical(List<Rect> src, Rect dst)
  182. {
  183. Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
  184. List<double> Leftlist = new List<double>();
  185. // Sort the data according to the leftmost position of each rectangle, not the array order
  186. foreach (Rect srcRect in src)
  187. {
  188. Leftlist.Add(srcRect.Left + srcRect.Width / 2);
  189. }
  190. double[] datalist = Leftlist.ToArray();
  191. Sort(datalist, 0, Leftlist.Count - 1);
  192. double startY = dst.Top;
  193. double endY = dst.Bottom;
  194. double interval = (endY - startY) / Leftlist.Count;
  195. for (int i = 0; i < datalist.Count(); i++)
  196. {
  197. int index = Leftlist.IndexOf(datalist[i]);
  198. Point movePoint = new Point(0, startY + i * interval - src[index].Top - src[index].Height / 2);
  199. dictionary.Add(src[index], movePoint);
  200. }
  201. return dictionary;
  202. }
  203. #region Quick sort
  204. private static int SortUnit(double[] array, int low, int high)
  205. {
  206. double key = array[low];
  207. while (low < high)
  208. {
  209. // Search backward for values smaller than the key
  210. while (array[high] >= key && high > low)
  211. --high;
  212. // Put the value smaller than key on the left
  213. array[low] = array[high];
  214. // Search forward for values larger than the key
  215. while (array[low] <= key && high > low)
  216. ++low;
  217. // Put the value larger than key on the right
  218. array[high] = array[low];
  219. }
  220. // Left is smaller than key, right is larger than key.
  221. // Put the key in the current position of the cursor
  222. // At this point, 'low' equals 'high
  223. array[low] = key;
  224. return high;
  225. }
  226. private static void Sort(double[] array, int low, int high)
  227. {
  228. if (low >= high)
  229. return;
  230. // Finish a single unit sort
  231. int index = SortUnit(array, low, high);
  232. // Sort the left unit
  233. Sort(array, low, index - 1);
  234. // Sort the right unit
  235. Sort(array, index + 1, high);
  236. }
  237. #endregion
  238. }
  239. }