Расстояние от точки до фигуры - C#
Формулировка задачи:
Доброго вечера. Помогите, пожалуйста, решить задачку: нужно вернуть методом расстояние от точки до заданной фигуры.
Вот сама фигура:
На вход поступают координаты х и y, вот таким образом:
вот вместо нуля нужно записать формулу.
Вот пример:
Заранее спасибо!
public static void DrawFigure(TestCaseUI ui)
{
ui.Line(-50, -10, -50, 10, TestCase.neutralPen);
ui.Line(50, -10, 50, 10, TestCase.neutralPen);
ui.Line(-50, 10, 0, 60, TestCase.neutralPen);
ui.Line(50, 10, 0, 60, TestCase.neutralPen);
ui.Arc(0, -10, 50, 180, 180, TestCase.neutralPen);
} public double GetDistanceToCurve(double x, double y)
{
//TODO
return 0;
}Решение задачи: «Расстояние от точки до фигуры»
textual
Листинг программы
using System;
namespace Distance
{
public class DistanceTask
{
static double GetLength(double x1, double x2, double y1, double y2)
{
return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
static double GetSquare(double ab, double bc, double ac)
{
double p = (ab + bc + ac) / 2;
return Math.Sqrt(p * (p - ab) * (p - ac) * (p - bc));
}
public double GetDistanceToCurve(double x, double y)
{
double ab = GetLength(x, 0, y, 60);
double ac = GetLength(50, 0, 10, 60);
double bc1 = GetLength(x, 50, y, 10);
double bc2 = GetLength(x, -50, y, 10);
//TODO
if (y >= 60 && x == 0)
return y - 60;
if (x == 0 && y == -10)
return 35 * Math.Sqrt(2);
if (y == 0 && x >= 40)
return Math.Abs(x - 50);
if (y == 0 && x <= -40)
return Math.Abs(x + 50);
if (x >= 0 && y >= 0)
if (x >= 60 && y >= 10)
return Math.Max(2 * GetSquare(ab, bc1, ac) / ac, GetLength(x, 50, y, 10));
else return 2 * GetSquare(ab, bc1, ac) / ac;
if (x < 0 && y >= 10)
if (x <= -60 && y >= 10) return Math.Max(2 * GetSquare(ab, bc2, ac) / ac, GetLength(x, -50, y, 10));
else return 2 * GetSquare(ab, bc2, ac) / ac;
if (y < 0) return Math.Abs(50 - Math.Sqrt(x * x + (y + 10) * (y + 10)));
else return 0;
}
}
}