Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DistanceTask
  4. {
  5.     public static class DistanceTask
  6.     {
  7.         public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
  8.         {
  9.             var sectionAB = Math.Sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
  10.             var sectionAC = Math.Sqrt((ax - x) * (ax - x) + (ay - y) * (ay - y));
  11.             var sectionBC = Math.Sqrt((x - bx) * (x - bx) + (y - by) * (y - by));
  12.             var cosABBC = ((x - ax) * (bx - ax) + (y - ay) * (by - ay)) / (sectionAC * sectionAB);
  13.             var cosABAC = ((ax - bx) * (x - bx) + (y - by) * (ay - by)) / (sectionBC * sectionAB);
  14.             if (Math.Round(sectionAC, 3) + Math.Round(sectionBC, 3) == Math.Round(sectionAB, 3))
  15.             {
  16.                 return 0.0;
  17.             }
  18.  
  19.             if ((cosABAC >= 0 && cosABAC < 1) && (cosABBC >= 0 && cosABBC < 1))
  20.             {
  21.                 var properiter = (sectionAB + sectionAC + sectionBC) / 2; //Полусумма
  22.                 var s = Math.Sqrt(properiter * (properiter - sectionAB) *
  23.                                   (properiter - sectionBC) *
  24.                                   (properiter - sectionAC)); // Формула герона                    
  25.                 return (2 * s) / sectionAB;
  26.             }
  27.             return Math.Min(sectionAC, sectionBC);
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement