Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace DistanceTask
- {
- public static class DistanceTask
- {
- // Расстояние от точки (x, y) до отрезка AB с координатами A(ax, ay), B(bx, by)
- public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
- {
- var sectionAB = Math.Sqrt(Math.Pow(ax - bx, 2) + Math.Pow(ay - by, 2));
- var sectionBC = Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
- var sectionAC = Math.Sqrt(Math.Pow(ax - x, 2) + Math.Pow(ay - y, 2));
- var cosABBC = (x - ax) * (bx - ax) + (y - ay) * (by - ay) / (sectionAB * sectionBC);
- var cosABAC = (ax - bx) * (x - bx) + (ay - by) * (ay - y) / (sectionAB * sectionAC);
- var properiter = ((sectionAB + sectionAC + sectionBC) / 2);
- var S = Math.Sqrt(properiter * (properiter - sectionAB) * (properiter - sectionBC) * (properiter - sectionAC));
- if ((cosABAC >= 0 && cosABAC < 1) && (cosABBC >= 0 && cosABBC < 1))
- return (2 * s) / sectionAB;
- return
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement