GeometryHelper.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using Microsoft.VisualBasic.CompilerServices;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. namespace MuchInfo.Chart.Infrastructure.Helpers
  7. {
  8. public static class GeometryHelper
  9. {
  10. #region Methods
  11. #region Public Static Methods
  12. public static Point PointFromAngleAndDistance(Point startPoint, double inAngle, double distance)
  13. {
  14. double num = inAngle + 90.0;
  15. Point result = default(Point);
  16. result.X = (startPoint.X + Math.Cos(3.1415926535897931 * num / 180.0) * distance);
  17. result.Y = (startPoint.Y + Math.Sin(3.1415926535897931 * num / 180.0) * distance);
  18. return result;
  19. }
  20. public static double AngleFromPoints(Point P1, Point P2)
  21. {
  22. return 57.295779513082323 * Math.Atan2(P2.Y - P1.Y, P2.X - P1.X) - 90.0;
  23. }
  24. public static float DistanceBetweenPoints(Point p1, Point p2)
  25. {
  26. return (float)Math.Sqrt(Math.Pow(p1.X - p2.X, 2.0) + Math.Pow(p1.Y - p2.Y, 2.0));
  27. }
  28. public static void EnsureControlInApplication(FrameworkElement theControl, UIElement rootPanel)
  29. {
  30. var panel = rootPanel as FrameworkElement;
  31. if (panel != null)
  32. {
  33. EnsureControlInParent(theControl, panel);
  34. }
  35. }
  36. public static void EnsureControlInParent(FrameworkElement theControl, FrameworkElement parent)
  37. {
  38. if (theControl != null)
  39. {
  40. Point point = GeometryHelper.PositionOfControlRelative(theControl, parent);
  41. Popup popup = null;
  42. Size size = default(Size);
  43. if (theControl is Popup)
  44. {
  45. popup = (Popup)theControl;
  46. point.X = (point.X + popup.HorizontalOffset);
  47. point.Y = (point.Y + popup.VerticalOffset);
  48. if (popup.Child != null && popup.Child is FrameworkElement)
  49. {
  50. size.Width = ((FrameworkElement)popup.Child).ActualWidth;
  51. size.Height = ((FrameworkElement)popup.Child).ActualHeight;
  52. }
  53. }
  54. if (popup == null)
  55. {
  56. if (point.X + size.Width > parent.ActualWidth)
  57. {
  58. theControl.SetValue(Canvas.LeftProperty, Operators.SubtractObject(theControl.GetValue(Canvas.LeftProperty), (point.X + size.Width - parent.ActualWidth)));
  59. }
  60. if (point.Y + size.Height > parent.ActualHeight)
  61. {
  62. theControl.SetValue(Canvas.TopProperty, Operators.SubtractObject(theControl.GetValue(Canvas.TopProperty), (point.Y + size.Height - parent.ActualHeight)));
  63. }
  64. if (point.X < 0.0)
  65. {
  66. theControl.SetValue(Canvas.LeftProperty, Operators.SubtractObject(theControl.GetValue(Canvas.LeftProperty), point.X));
  67. }
  68. if (point.Y < 0.0)
  69. {
  70. theControl.SetValue(Canvas.TopProperty, Operators.SubtractObject(theControl.GetValue(Canvas.TopProperty), point.Y));
  71. }
  72. }
  73. else
  74. {
  75. if (point.X + size.Width > parent.ActualWidth)
  76. {
  77. popup.HorizontalOffset -= point.X + size.Width - parent.ActualWidth;
  78. }
  79. if (point.Y + size.Height > parent.ActualHeight)
  80. {
  81. popup.VerticalOffset -= point.Y + size.Height - parent.ActualHeight;
  82. }
  83. if (point.X < 0.0)
  84. {
  85. popup.HorizontalOffset -= point.X;
  86. }
  87. if (point.Y < 0.0)
  88. {
  89. popup.VerticalOffset -= point.Y;
  90. }
  91. }
  92. }
  93. }
  94. public static Point PositionOfControlRelative(UIElement theControl, UIElement comparedTo)
  95. {
  96. var result = new Point();
  97. try
  98. {
  99. var generalTransform = theControl.TransformToVisual(comparedTo);
  100. var point = new Point(0.0, 0.0);
  101. result = generalTransform.Transform(point);
  102. }
  103. catch (Exception ex)
  104. {
  105. throw ex;
  106. }
  107. return result;
  108. }
  109. public static Point PositionOfControlInApp(UIElement theControl, Panel rootPanel)
  110. {
  111. return PositionOfControlRelative(theControl, rootPanel);
  112. }
  113. public static void CheckLineExtents(ref Point P1, ref Point P2)
  114. {
  115. if (P1.X.Equals(P2.X))
  116. {
  117. P1.Y = (Math.Min(32000.0, P1.Y));
  118. P1.Y = (Math.Max(-32000.0, P1.Y));
  119. P2.Y = (Math.Min(32000.0, P2.Y));
  120. P2.Y = (Math.Max(-32000.0, P2.Y));
  121. P1.X = (Math.Min(32000.0, P1.X));
  122. P1.X = (Math.Max(-32000.0, P1.X));
  123. P2.X = (Math.Min(32000.0, P2.X));
  124. P2.X = (Math.Max(-32000.0, P2.X));
  125. }
  126. else
  127. {
  128. var num = (P2.Y - P1.Y) / (P2.X - P1.X);
  129. if (num.Equals(0))
  130. {
  131. P1.Y = (Math.Min(32000.0, P1.Y));
  132. P1.Y = (Math.Max(-32000.0, P1.Y));
  133. P2.Y = (Math.Min(32000.0, P2.Y));
  134. P2.Y = (Math.Max(-32000.0, P2.Y));
  135. P1.X = (Math.Min(32000.0, P1.X));
  136. P1.X = (Math.Max(-32000.0, P1.X));
  137. P2.X = (Math.Min(32000.0, P2.X));
  138. P2.X = (Math.Max(-32000.0, P2.X));
  139. }
  140. else
  141. {
  142. var @base = new Point(P1.X, P1.Y);
  143. if (P1.X < -32000.0)
  144. {
  145. P1.X = -32000.0;
  146. P1.Y = YFromLine(@base, num, P1.X);
  147. }
  148. if (P1.X > 32000.0)
  149. {
  150. P1.X = 32000.0;
  151. P1.Y = YFromLine(@base, num, P1.X);
  152. }
  153. if (P1.Y < -32000.0)
  154. {
  155. P1.X = XFromLine(@base, num, P1.Y);
  156. P1.Y = -32000.0;
  157. }
  158. if (P1.Y > 32000.0)
  159. {
  160. P1.X = XFromLine(@base, num, P1.Y);
  161. P1.Y = 32000.0;
  162. }
  163. if (P2.X < -32000.0)
  164. {
  165. P2.X = -32000.0;
  166. P2.Y = YFromLine(@base, num, P2.X);
  167. }
  168. if (P2.X > 32000.0)
  169. {
  170. P2.X = 32000.0;
  171. P2.Y = YFromLine(@base, num, P2.X);
  172. }
  173. if (P2.Y < -32000.0)
  174. {
  175. P2.X = XFromLine(@base, num, P2.Y);
  176. P2.Y = -32000.0;
  177. }
  178. if (P2.Y > 32000.0)
  179. {
  180. P2.X = XFromLine(@base, num, P2.Y);
  181. P2.Y = 32000.0;
  182. }
  183. }
  184. }
  185. }
  186. public static double YFromLine(Point @base, double slope, double x)
  187. {
  188. return @base.Y + slope * (x - @base.X);
  189. }
  190. public static double XFromLine(Point @base, double slope, double y)
  191. {
  192. return @base.X + (y - @base.Y) / slope;
  193. }
  194. public static double LineSlope(ref Point p1, ref Point p2)
  195. {
  196. return p2.X.Equals(p1.X) ? 1.7976931348623157E+308 : (p2.Y - p1.Y) / (p2.X - p1.X);
  197. }
  198. public static double K;
  199. public static double B;
  200. public static Point PointA;
  201. public static Point PointB;
  202. public static Point PointC;
  203. public static Point getRangLinePointX(Point a, Point b, double y)
  204. {
  205. PointA = a;
  206. PointB = b;
  207. getLinePointXFromY(y);
  208. return PointC;
  209. }
  210. public static Point getRangLinePointY(Point a, Point b, double x)
  211. {
  212. PointA = a;
  213. PointB = b;
  214. getLinePointYFromX(x);
  215. return PointC;
  216. }
  217. public static double getLinePointXFromY(double y)
  218. {
  219. var Rlt = default(double);
  220. getK();
  221. getB();
  222. if (y != default(double) && PointA != default(Point) && PointB != default(Point))
  223. {
  224. Rlt = (y - B) / K;
  225. PointC = new Point(Rlt, y);
  226. }
  227. return Rlt;
  228. }
  229. public static double getLinePointYFromX(double x)
  230. {
  231. var Rlt = default(double);
  232. getK();
  233. getB();
  234. if (x != default(double) && PointA != default(Point) && PointB != default(Point))
  235. {
  236. Rlt = K * x + B;
  237. PointC = new Point(x, Rlt);
  238. }
  239. return Rlt;
  240. }
  241. private static double getK()
  242. {
  243. var Rlt = default(double);
  244. if (PointA != default(Point) && PointB != default(Point))
  245. {
  246. Rlt = (PointA.X - PointB.X) / (PointA.Y - PointB.Y);
  247. K = Rlt;
  248. }
  249. return Rlt;
  250. }
  251. private static double getB()
  252. {
  253. var Rlt = default(double);
  254. if (PointA != default(Point) && K != default(double))
  255. {
  256. Rlt =PointA.Y- K * PointA.X ;
  257. B = Rlt;
  258. }
  259. return Rlt;
  260. }
  261. #endregion Public Static Methods
  262. #endregion Methods
  263. }
  264. }