using Microsoft.VisualBasic.CompilerServices; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace MuchInfo.Chart.Infrastructure.Helpers { public static class GeometryHelper { #region Methods #region Public Static Methods public static Point PointFromAngleAndDistance(Point startPoint, double inAngle, double distance) { double num = inAngle + 90.0; Point result = default(Point); result.X = (startPoint.X + Math.Cos(3.1415926535897931 * num / 180.0) * distance); result.Y = (startPoint.Y + Math.Sin(3.1415926535897931 * num / 180.0) * distance); return result; } public static double AngleFromPoints(Point P1, Point P2) { return 57.295779513082323 * Math.Atan2(P2.Y - P1.Y, P2.X - P1.X) - 90.0; } public static float DistanceBetweenPoints(Point p1, Point p2) { return (float)Math.Sqrt(Math.Pow(p1.X - p2.X, 2.0) + Math.Pow(p1.Y - p2.Y, 2.0)); } public static void EnsureControlInApplication(FrameworkElement theControl, UIElement rootPanel) { var panel = rootPanel as FrameworkElement; if (panel != null) { EnsureControlInParent(theControl, panel); } } public static void EnsureControlInParent(FrameworkElement theControl, FrameworkElement parent) { if (theControl != null) { Point point = GeometryHelper.PositionOfControlRelative(theControl, parent); Popup popup = null; Size size = default(Size); if (theControl is Popup) { popup = (Popup)theControl; point.X = (point.X + popup.HorizontalOffset); point.Y = (point.Y + popup.VerticalOffset); if (popup.Child != null && popup.Child is FrameworkElement) { size.Width = ((FrameworkElement)popup.Child).ActualWidth; size.Height = ((FrameworkElement)popup.Child).ActualHeight; } } if (popup == null) { if (point.X + size.Width > parent.ActualWidth) { theControl.SetValue(Canvas.LeftProperty, Operators.SubtractObject(theControl.GetValue(Canvas.LeftProperty), (point.X + size.Width - parent.ActualWidth))); } if (point.Y + size.Height > parent.ActualHeight) { theControl.SetValue(Canvas.TopProperty, Operators.SubtractObject(theControl.GetValue(Canvas.TopProperty), (point.Y + size.Height - parent.ActualHeight))); } if (point.X < 0.0) { theControl.SetValue(Canvas.LeftProperty, Operators.SubtractObject(theControl.GetValue(Canvas.LeftProperty), point.X)); } if (point.Y < 0.0) { theControl.SetValue(Canvas.TopProperty, Operators.SubtractObject(theControl.GetValue(Canvas.TopProperty), point.Y)); } } else { if (point.X + size.Width > parent.ActualWidth) { popup.HorizontalOffset -= point.X + size.Width - parent.ActualWidth; } if (point.Y + size.Height > parent.ActualHeight) { popup.VerticalOffset -= point.Y + size.Height - parent.ActualHeight; } if (point.X < 0.0) { popup.HorizontalOffset -= point.X; } if (point.Y < 0.0) { popup.VerticalOffset -= point.Y; } } } } public static Point PositionOfControlRelative(UIElement theControl, UIElement comparedTo) { var result = new Point(); try { var generalTransform = theControl.TransformToVisual(comparedTo); var point = new Point(0.0, 0.0); result = generalTransform.Transform(point); } catch (Exception ex) { throw ex; } return result; } public static Point PositionOfControlInApp(UIElement theControl, Panel rootPanel) { return PositionOfControlRelative(theControl, rootPanel); } public static void CheckLineExtents(ref Point P1, ref Point P2) { if (P1.X.Equals(P2.X)) { P1.Y = (Math.Min(32000.0, P1.Y)); P1.Y = (Math.Max(-32000.0, P1.Y)); P2.Y = (Math.Min(32000.0, P2.Y)); P2.Y = (Math.Max(-32000.0, P2.Y)); P1.X = (Math.Min(32000.0, P1.X)); P1.X = (Math.Max(-32000.0, P1.X)); P2.X = (Math.Min(32000.0, P2.X)); P2.X = (Math.Max(-32000.0, P2.X)); } else { var num = (P2.Y - P1.Y) / (P2.X - P1.X); if (num.Equals(0)) { P1.Y = (Math.Min(32000.0, P1.Y)); P1.Y = (Math.Max(-32000.0, P1.Y)); P2.Y = (Math.Min(32000.0, P2.Y)); P2.Y = (Math.Max(-32000.0, P2.Y)); P1.X = (Math.Min(32000.0, P1.X)); P1.X = (Math.Max(-32000.0, P1.X)); P2.X = (Math.Min(32000.0, P2.X)); P2.X = (Math.Max(-32000.0, P2.X)); } else { var @base = new Point(P1.X, P1.Y); if (P1.X < -32000.0) { P1.X = -32000.0; P1.Y = YFromLine(@base, num, P1.X); } if (P1.X > 32000.0) { P1.X = 32000.0; P1.Y = YFromLine(@base, num, P1.X); } if (P1.Y < -32000.0) { P1.X = XFromLine(@base, num, P1.Y); P1.Y = -32000.0; } if (P1.Y > 32000.0) { P1.X = XFromLine(@base, num, P1.Y); P1.Y = 32000.0; } if (P2.X < -32000.0) { P2.X = -32000.0; P2.Y = YFromLine(@base, num, P2.X); } if (P2.X > 32000.0) { P2.X = 32000.0; P2.Y = YFromLine(@base, num, P2.X); } if (P2.Y < -32000.0) { P2.X = XFromLine(@base, num, P2.Y); P2.Y = -32000.0; } if (P2.Y > 32000.0) { P2.X = XFromLine(@base, num, P2.Y); P2.Y = 32000.0; } } } } public static double YFromLine(Point @base, double slope, double x) { return @base.Y + slope * (x - @base.X); } public static double XFromLine(Point @base, double slope, double y) { return @base.X + (y - @base.Y) / slope; } public static double LineSlope(ref Point p1, ref Point p2) { return p2.X.Equals(p1.X) ? 1.7976931348623157E+308 : (p2.Y - p1.Y) / (p2.X - p1.X); } public static double K; public static double B; public static Point PointA; public static Point PointB; public static Point PointC; public static Point getRangLinePointX(Point a, Point b, double y) { PointA = a; PointB = b; getLinePointXFromY(y); return PointC; } public static Point getRangLinePointY(Point a, Point b, double x) { PointA = a; PointB = b; getLinePointYFromX(x); return PointC; } public static double getLinePointXFromY(double y) { var Rlt = default(double); getK(); getB(); if (y != default(double) && PointA != default(Point) && PointB != default(Point)) { Rlt = (y - B) / K; PointC = new Point(Rlt, y); } return Rlt; } public static double getLinePointYFromX(double x) { var Rlt = default(double); getK(); getB(); if (x != default(double) && PointA != default(Point) && PointB != default(Point)) { Rlt = K * x + B; PointC = new Point(x, Rlt); } return Rlt; } private static double getK() { var Rlt = default(double); if (PointA != default(Point) && PointB != default(Point)) { Rlt = (PointA.X - PointB.X) / (PointA.Y - PointB.Y); K = Rlt; } return Rlt; } private static double getB() { var Rlt = default(double); if (PointA != default(Point) && K != default(double)) { Rlt =PointA.Y- K * PointA.X ; B = Rlt; } return Rlt; } #endregion Public Static Methods #endregion Methods } }