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 Distance(double x1, double x2, double y1, double y2)
- {
- return Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- }
- public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
- {
- if (ax == ay && bx == by)
- {
- return Distance(x, ax, y, ay);
- }
- double ab = Distance(bx, ax, by, ay);
- double ac = Distance(x, ax, y, ay);
- double bc = Distance(x, bx, y, by);
- if (Math.Pow(ab, 2) + Math.Pow(ac, 2) <= Math.Pow(bc, 2) || Math.Pow(ab, 2) + Math.Pow(bc, 2) <= Math.Pow(ac, 2))
- return Math.Min(ac, bc);
- double p = (ab + ac + bc) / 2;
- double h = (2 * Math.Sqrt(p * (p - ab) * (p - ac) * (p - bc))) / ab;
- return h;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement