Advertisement
bochkareffsasha

lab3 daniil etition

Nov 28th, 2021 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. auto findYEllipse(float x)
  4. {
  5.     const float x0 = -9.5305f;
  6.     const int y0 = -7;
  7.     const int a = 31;
  8.     const int b = 38;
  9.     const int a2 = 961;
  10.  
  11.     struct r { float yp; float yn; };
  12.  
  13.     float yOffensive = b * sqrt(1 - (x - x0) * (x - x0) / a2);
  14.     return r{ y0 + yOffensive, y0 - yOffensive };
  15. };
  16.  
  17. auto findYCircle(float x)
  18. {
  19.     const int x0 = 1;
  20.     const float y0 = 10.9839f;
  21.     const int r2 = 961;
  22.  
  23.     float yOffensive = sqrt(r2 - (x - x0) * (x - x0));
  24.  
  25.     struct r { float yp; float yn; };
  26.     return r{ y0 + yOffensive, y0 - yOffensive };
  27. };
  28.  
  29. float rightpoint(float x)
  30. {
  31.     return (findYCircle(x).yn - findYEllipse(x).yn);
  32. };
  33.  
  34. float leftpoint(float x)
  35. {
  36.     return (findYCircle(x).yp - findYEllipse(x).yp);
  37. };
  38.  
  39. float recursive(float start, float stop, float step, float accuracy, int divstep, float (*workfunc)(float))
  40. {
  41.     float prevdiffer = (*workfunc)(start);
  42.     float differ;
  43.  
  44.     while (true)
  45.         for (float i = (start + step); i <= stop; i += step)
  46.         {
  47.             differ = (*workfunc)(i);
  48.  
  49.             if (abs(differ) <= accuracy)
  50.                 return i;
  51.  
  52.             if ((prevdiffer > 0) != (differ > 0))
  53.             {
  54.                 stop = i;
  55.                 start = i - step;
  56.                 step /= divstep;
  57.                 break;
  58.             }
  59.             else
  60.                 prevdiffer = differ;
  61.         };
  62.  
  63. };
  64.  
  65. float startrecursive(float start, float stop, float step, float accuracy, int divstep, float (*workfunc)(float))
  66. {
  67.     if (stop < start)
  68.     {
  69.         float t = stop;
  70.         stop = start;
  71.         start = t;
  72.     };
  73.  
  74.     stop = start + ((int)((stop - start) / step)) * step;
  75.     return recursive(start, stop, step, accuracy, divstep, workfunc);
  76. };
  77.  
  78. int main()
  79. {
  80.     setlocale(LC_ALL, "RU");
  81.     std::cout << "Выполнил: Ивченко Д.Я. (КЗИ-21-1Б)" << std::endl << std::endl;
  82.  
  83.     float x = startrecursive(10, 28.45f, 1, 0.0001f, 10, &rightpoint);
  84.     std::cout << "Первая точка: ( " << x << " ; " << findYCircle(x).yn << " )" << std::endl;
  85.  
  86.     x = startrecursive(-30, 0, 1, 0.0001f, 10, &leftpoint);
  87.     std::cout << "Вторая точка: ( " << x << " ; " << findYCircle(x).yp << " )" << std::endl;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement