Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace DistanceTask
- {
- public static class DistanceTask
- {
- public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
- {
- var sectionAB = Math.Sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
- var sectionAC = Math.Sqrt((ax - x) * (ax - x) + (ay - y) * (ay - y));
- var sectionBC = Math.Sqrt((x - bx) * (x - bx) + (y - by) * (y - by));
- var cosABBC = ((x - ax) * (bx - ax) + (y - ay) * (by - ay)) / (sectionAC * sectionAB);
- var cosABAC = ((ax - bx) * (x - bx) + (y - by) * (ay - by)) / (sectionBC * sectionAB);
- if (Math.Round(sectionAC, 3) + Math.Round(sectionBC, 3) == Math.Round(sectionAB, 3))
- {
- return 0.0;
- }
- if ((cosABAC >= 0 && cosABAC < 1) && (cosABBC >= 0 && cosABBC < 1))
- {
- var properiter = (sectionAB + sectionAC + sectionBC) / 2; //Полусумма
- var s = Math.Sqrt(properiter * (properiter - sectionAB) *
- (properiter - sectionBC) *
- (properiter - sectionAC)); // Формула герона
- return (2 * s) / sectionAB;
- }
- return Math.Min(sectionAC, sectionBC);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement