GeometryHelper.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Windows;
  3. namespace SHJX.Service.WorkFlowEdit
  4. {
  5. public static class GeometryHelper
  6. {
  7. public static readonly Point NullPoint = new Point(Double.NaN, Double.NaN);
  8. public static double NormalAngle(Point start, Point end)
  9. {
  10. var normal = new Vector(1, 0);
  11. var v = new Vector(end.X - start.X, end.Y - start.Y);
  12. return Vector.AngleBetween(normal, v);
  13. }
  14. public static double Length(Point start, Point end)
  15. {
  16. var v = new Vector(end.X - start.X, end.Y - start.Y);
  17. return v.Length;
  18. }
  19. public static Point EllipseLineIntersection(double a, double b, Point p)
  20. {
  21. var c = a * b / Math.Sqrt(a * a * p.Y * p.Y + b * b * p.X * p.X);
  22. var p1 = new Point(c * p.X, c * p.Y);
  23. var p2 = new Point(-c * p.X, -c * p.Y);
  24. return Length(p1, p) < Length(p2, p) ? p1 : p2;
  25. }
  26. public static Point RectLineIntersection(Rect rect, Point p)
  27. {
  28. var c = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
  29. Point res;
  30. if (SegmentsIntersection(new Point(rect.X, rect.Y), new Point(rect.Right, rect.Y), c, p, out res))
  31. return res;
  32. if (SegmentsIntersection(new Point(rect.Right, rect.Y), new Point(rect.Right, rect.Bottom), c, p, out res))
  33. return res;
  34. if (SegmentsIntersection(new Point(rect.Right, rect.Bottom), new Point(rect.X, rect.Bottom), c, p, out res))
  35. return res;
  36. SegmentsIntersection(new Point(rect.X, rect.Bottom), new Point(rect.X, rect.Y), c, p, out res);
  37. return res;
  38. }
  39. public static bool SegmentsIntersection(Point a1, Point a2, Point b1, Point b2, out Point res)
  40. {
  41. var d = (a1.X - a2.X) * (b2.Y - b1.Y) - (a1.Y - a2.Y) * (b2.X - b1.X);
  42. var da = (a1.X - b1.X) * (b2.Y - b1.Y) - (a1.Y - b1.Y) * (b2.X - b1.X);
  43. var db = (a1.X - a2.X) * (a1.Y - b1.Y) - (a1.Y - a2.Y) * (a1.X - b1.X);
  44. if (Math.Abs(d) > 0.000001)
  45. {
  46. var ta = da / d;
  47. var tb = db / d;
  48. if (0 <= ta && ta <= 1 && 0 <= tb && tb <= 1)
  49. {
  50. res = new Point(a1.X + ta * (a2.X - a1.X), a1.Y + ta * (a2.Y - a1.Y));
  51. return true;
  52. }
  53. }
  54. res = NullPoint;
  55. return false;
  56. }
  57. public static Point SegmentMiddlePoint(Point p1, Point p2)
  58. {
  59. return new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
  60. }
  61. public static bool RectContains(Rect rect, Point point)
  62. {
  63. return rect.X <= point.X && rect.Right >= point.X
  64. && rect.Y <= point.Y && rect.Bottom >= point.Y;
  65. }
  66. public static bool EllipseContains(Point center, double a, double b, Point point)
  67. {
  68. var p = point - center;
  69. return (p.X * p.X) / (a * a) + (p.Y * p.Y) / (b * b) <= 1;
  70. }
  71. public static bool AreEquals(double a, double b)
  72. {
  73. return Math.Abs(a - b) < 0.0001;
  74. }
  75. }
  76. }