Advertisement
MyOnAsSalat

Untitled

Mar 29th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DistanceTask
  4. {
  5.     public static class DistanceTask
  6.     {
  7.         public static double K, M, Intersection_X, Intersection_Y;
  8.  
  9.         // Расстояние от точки (x, y) до отрезка AB с координатами A(ax, ay), B(bx, by)
  10.         public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
  11.         {
  12.             if (ax - bx != 0) return SegmentIsVertical(ref ax, ref ay, ref bx, ref by, ref x, ref y);
  13.             else return SegmentIsNotVertical(ref ax, ref ay, ref bx, ref by, ref x, ref y);
  14.         }
  15.  
  16.         public static double SegmentIsVertical(ref double ax, ref double ay,
  17.                                                ref double bx, ref double by,
  18.                                                ref double x, ref double y)
  19.         {
  20.             K = (ay - by) / (ax - bx);
  21.             M = ay - K * ax;
  22.  
  23.             Intersection_Y = (M + K * K * y + K * x) / (1 + K * K);
  24.  
  25.             if (K != 0) Intersection_X = (Intersection_Y - M) / K;
  26.             else Intersection_X = x;
  27.  
  28.             if ((x == ax && y == ay) ||
  29.                  (x == bx && y == by) ||
  30.                  (y == K * x + M && ((x >= ax && x <= bx) || (x >= bx && x <= ax))))
  31.                 return 0.0;
  32.             else if ((Intersection_X >= ax && Intersection_X <= bx) ||
  33.                      (Intersection_X <= ax && Intersection_X >= bx))
  34.                 return Math.Sqrt((x - Intersection_X) * (x - Intersection_X) +
  35.                                   (y - Intersection_Y) * (y - Intersection_Y));
  36.             return FindTheMinimumDistance(ref ax, ref ay, ref bx, ref by, ref x, ref y);
  37.         }
  38.  
  39.         public static double SegmentIsNotVertical(ref double ax, ref double ay,
  40.                                                   ref double bx, ref double by,
  41.                                                   ref double x, ref double y)
  42.         {
  43.             Intersection_Y = y;
  44.             Intersection_X = ax;
  45.  
  46.             if ((Intersection_Y >= ay && Intersection_Y <= by) ||
  47.                  (Intersection_Y <= ay && Intersection_Y >= by))
  48.                 return Math.Sqrt((x - Intersection_X) * (x - Intersection_X) +
  49.                                   (y - Intersection_Y) * (y - Intersection_Y));
  50.             else if (x == ax && ((y >= ay && y <= by) ||
  51.                     (y >= by && y <= ay)))
  52.                 return 0.0;
  53.             return FindTheMinimumDistance(ref ax, ref ay, ref bx, ref by, ref x, ref y);
  54.         }
  55.  
  56.         public static double FindTheMinimumDistance(ref double ax, ref double ay,
  57.                                                     ref double bx, ref double by,
  58.                                                     ref double x, ref double y)
  59.         {
  60.             return Math.Min(
  61.                             Math.Sqrt( (x - ax) * (x - ax) + (y - ay) * (y - ay) ),
  62.                             Math.Sqrt( (x - bx) * (x - bx) + (y - by) * (y - by) )
  63.                            );
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement