Найти точку пересечения прямой и окружности - C#

Узнай цену своей работы

Формулировка задачи:

Задана окружность с центром в точке O(0,0) и радиусом R0, и прямая y = ax+b. Определить, пересекаются ли прямая и окружность. Если пересекаются, найти точку пересечения.

Решение задачи: «Найти точку пересечения прямой и окружности»

textual
Листинг программы
  1. class Programm
  2.     {
  3.         /*Circle properties*/
  4.         static double x0, y0, r;
  5.         /*Line properties*/
  6.         static double k, p;
  7.  
  8.         public static void Main()
  9.         {
  10.             /*Init circle*/
  11.             init("x0  = ", out x0);
  12.             init("y0  = ", out y0);
  13.             init("r   = ", out r);
  14.  
  15.             /*Init line*/
  16.             init("k  = ", out k);
  17.             init("p  = ", out p);
  18.  
  19.             double a = square(k) + 1;
  20.             double b = 2 * (k * (p - y0) - x0);
  21.             double c = square(b - y0) + square(x0) - square(r);
  22.  
  23.             double x1, x2;
  24.             if (trySolutionSquareEquation(a, b, c, out x1, out x2))
  25.             {
  26.                 Console.WriteLine("First point : [{0};{1}]", x1, y(x1));
  27.                 Console.WriteLine("Second point : [{0};{1}]", x2, y(x2));
  28.             }
  29.             else
  30.             {
  31.                 Console.WriteLine("Equation not have solution");                
  32.             }
  33.             Console.ReadLine();
  34.         }
  35.  
  36.         /*Function of line*/
  37.         private static double y(double x)
  38.         {
  39.             return k * x + p;
  40.         }
  41.  
  42.         /*Get square of value*/
  43.         private static double square(double value)
  44.         {
  45.             return Math.Pow(value, 2.0);
  46.         }
  47.  
  48.         /*Solving equation*/
  49.         private static bool trySolutionSquareEquation(double a, double b, double c, out double x1, out double x2)
  50.         {
  51.             x1 = double.NaN;
  52.             x2 = double.NaN;
  53.  
  54.             double discriminant = b * b - 4 * a * c;
  55.             if (discriminant < 0)
  56.             {
  57.                 return false;
  58.             }
  59.  
  60.             double k = -b / 2 * a;
  61.             discriminant /= 2 * a;
  62.             x1 = k - Math.Sqrt(discriminant);
  63.             x2 = k + Math.Sqrt(discriminant);
  64.  
  65.             return true;
  66.         }
  67.  
  68.         private static void init(string message, out double value)
  69.         {
  70.             Console.Write(message);
  71.             value = readDoubleFromTerminale();
  72.         }
  73.  
  74.          private static double readDoubleFromTerminale()
  75.         {
  76.             string doubleString;
  77.             double doubleValue;
  78.             do
  79.             {
  80.                 doubleString = Console.ReadLine();
  81.             }
  82.             while (!Double.TryParse(doubleString, out doubleValue));
  83.            
  84.             return doubleValue;
  85.         }
  86.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы