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)
- {
- double coss;
- double xx, yy;
- xx = bx - ax;
- yy = by - ay;
- double x1, y1;
- x1 = x - ax;
- y1 = y - ay;
- double a, b;
- a = Math.Sqrt(xx * xx + yy * yy);
- b = Math.Sqrt(x1 * x1 + y1 * y1);
- if (by == ay)
- {
- if (x >= bx)
- return Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
- else if (x <= ax)
- return Math.Sqrt(Math.Pow(x - ax, 2) + Math.Pow(y - ay, 2));
- else
- {
- coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
- double sinn;
- sinn = Math.Sqrt(1 - Math.Pow(coss, 2));
- double S = a * b * sinn;
- return S / a;
- }
- }
- else if (ax == bx)
- {
- if ((by >= ay && y >= by) || (by <= ay && y <= by))
- return Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
- else if ((ay >= by && y >= ay) || (ay <= by && y <= ay))
- return Math.Sqrt(Math.Pow(x - ax, 2) + Math.Pow(y - ay, 2));
- else
- {
- coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
- double sinn;
- sinn = Math.Sqrt(1 - Math.Pow(coss, 2));
- double S = a * b * sinn;
- return S / a;
- }
- }
- else
- {
- coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
- if ((coss <= 0 && bx > ax && x > bx) || (coss <= 0 && ax > bx && x < bx))
- return Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
- else if ((coss <= 0 && ax < bx&&x<ax) || (coss <= 0 && ax > bx && x > ax))
- return Math.Sqrt(Math.Pow(x - ax, 2) + Math.Pow(y - ay, 2));
- else if (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy) != 0)
- {
- coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
- double sinn;
- sinn = Math.Sqrt(1 - Math.Pow(coss, 2));
- double S = a * b * sinn;
- return S / a;
- }
- else
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement