Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DistanceTask
  4. {
  5.     public static class DistanceTask
  6.     {
  7.         // Расстояние от точки (x, y) до отрезка AB с координатами A(ax, ay), B(bx, by)
  8.         public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
  9.         {
  10.             var sectionAB = Math.Sqrt(Math.Pow(ax - bx, 2) + Math.Pow(ay - by, 2));
  11.             var sectionBC = Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
  12.             var sectionAC = Math.Sqrt(Math.Pow(ax - x, 2) + Math.Pow(ay - y, 2));
  13.             var cosABBC = (x - ax) * (bx - ax) + (y - ay) * (by - ay) / (sectionAB * sectionBC);
  14.             var cosABAC = (ax - bx) * (x - bx) + (ay - by) * (ay - y) / (sectionAB * sectionAC);
  15.             var properiter = ((sectionAB + sectionAC + sectionBC) / 2);
  16.             var S = Math.Sqrt(properiter * (properiter - sectionAB) * (properiter - sectionBC) * (properiter - sectionAC));
  17.  
  18.             if ((cosABAC >= 0 && cosABAC < 1) && (cosABBC >= 0 && cosABBC < 1))
  19.                 return (2 * s) / sectionAB;
  20.  
  21.                 return
  22.  
  23.         }
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement