Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. //Определение номера области, в которую попадает точка с произвольно заданными координатами на плоскости, и площади этой области
  2.  
  3. #include<iostream>
  4. #include<cmath>
  5. #include <locale>
  6.  
  7. using namespace std;
  8.  
  9. float sqr(float x) {
  10. return pow(x, 2);
  11. }
  12.  
  13. float M1_top(float x){
  14. return sqrt(1 - sqr(x + 1)) + 1;
  15. }
  16.  
  17. float M1_bottom(float x){
  18. if (x < -1 - sqrt(3)/2)
  19. return -sqrt(1 - sqr(x + 1)) + 1;
  20. else if (x < -0.5)
  21. return sqrt(1 - sqr(x + 1));
  22. else
  23. return sqrt(1 - x*x);
  24. }
  25.  
  26. float M2_top(float x){
  27. return sqrt(1 - sqr(x - 1)) + 1;
  28. }
  29.  
  30. float M2_bottom(float x){
  31. if (x < 1 + sqrt(3)/2)
  32. return sqrt(1 - sqr(x - 1));
  33. else
  34. return -sqrt(1 - sqr(x - 1)) + 1;
  35. }
  36.  
  37. float M3_top(float x){
  38. if (x < 0.5)
  39. return sqrt(1 - sqr(x - 1));
  40. else
  41. return sqrt(1 - x*x);
  42. }
  43.  
  44. float M3_bottom(float x){
  45. if (x < 0.5)
  46. return -sqrt(1 - x*x) + 1;
  47. else
  48. return -sqrt(1 - x*x) + 1;
  49. }
  50.  
  51. float M4_top(float x){
  52. return -sqrt(1 - x*x);
  53. }
  54.  
  55. float M4_bottom(float x){
  56. return -sqrt(1 - sqr(x + 1));
  57. }
  58.  
  59. float M5_top(float x){
  60. return sqrt(1 - sqr(x - 1)) - 1;
  61. }
  62.  
  63. float M5_bottom(float x){
  64. if (x < 1)
  65. return sqrt(1 - x*x) - 1;
  66. else
  67. return -sqrt(1 - sqr(x - 1));
  68.  
  69. }
  70.  
  71. int detect_region(float x, float y){
  72. if ((sqr(x + 1) + sqr(y - 1) <= 1) && (x*x + y*y >= 1) && (sqr(x + 1) + y*y >= 1))
  73. return 1;
  74. else if ((sqr(x - 1) + sqr(y - 1) <= 1) && (sqr(x - 1) + y*y >= 1) && (x >= 1))
  75. return 2;
  76. else if ((x*x + y*y <= 1) && (sqr(x - 1) + sqr(y - 1) <= 1) && (x*x + sqr(y - 1) <= 1) && (sqr(x - 1) + y*y <= 1))
  77. return 3;
  78. else if ((sqr(x + 1) + y*y <= 1) && (x*x + y*y >= 1) && (x >= -1) && ( y <= 0))
  79. return 4;
  80. else if ((sqr(x - 1) + sqr(y + 1) <= 1) && ( sqr(x - 1) + y*y <= 1) && (x*x + sqr(y + 1) >= 1))
  81. return 5;
  82. else
  83. return -1;
  84. }
  85.  
  86. float caclulate_integral(float (*func)(float), float x_start, float x_stop, float dx){
  87. float area = 0;
  88. for(float x = x_start; x <= x_stop; x += dx) {
  89. area += dx * func(x);
  90. }
  91. return area;
  92. }
  93.  
  94. float get_region_area(int region_number) {
  95. float x_start, x_stop;
  96. float inregral_top, inregral_bottom;
  97. float dx = 0.001;
  98. switch (region_number){
  99. case 1:
  100. x_start = -2;
  101. x_stop = 0;
  102.  
  103. inregral_top = caclulate_integral(M1_top, x_start, x_stop, dx);
  104. inregral_bottom = caclulate_integral(M1_bottom, x_start, x_stop, dx);
  105. return inregral_top - inregral_bottom;
  106. case 2:
  107. x_start = 1;
  108. x_stop = 2;
  109.  
  110. inregral_top = caclulate_integral(M2_top, x_start, x_stop, dx);
  111. inregral_bottom = caclulate_integral(M2_bottom, x_start, x_stop, dx);
  112. return inregral_top - inregral_bottom;
  113. case 3:
  114. x_start = 1 - sqrt(3)/2;
  115. x_stop = sqrt(3)/2;
  116.  
  117. inregral_top = caclulate_integral(M3_top, x_start, x_stop, dx);
  118. inregral_bottom = caclulate_integral(M3_bottom, x_start, x_stop, dx);
  119. return inregral_top - inregral_bottom;
  120. case 4:
  121. x_start = -1;
  122. x_stop = -0.5;
  123.  
  124. inregral_top = caclulate_integral(M4_top, x_start, x_stop, dx);
  125. inregral_bottom = caclulate_integral(M4_bottom, x_start, x_stop, dx);
  126. return inregral_top - inregral_bottom;
  127. case 5:
  128. x_start = 0.5;
  129. x_stop = 1 + sqrt(3)/2;
  130.  
  131. inregral_top = caclulate_integral(M5_top, x_start, x_stop, dx);
  132. inregral_bottom = caclulate_integral(M5_bottom, x_start, x_stop, dx);
  133. return inregral_top - inregral_bottom;
  134. }
  135. }
  136.  
  137.  
  138. int main() {
  139. float x, y;
  140.  
  141. setlocale(LC_ALL,"Russian") ;
  142.  
  143. cout << "\nВведите координаты точки: x, y " ;
  144. cin >> x >> y;
  145.  
  146. int region_number = detect_region(x, y);
  147.  
  148. if (region_number != -1) {
  149. float area = get_region_area(region_number);
  150. cout << "\nТочка в области М" << region_number << ". S = " << area;
  151. }
  152. else
  153. cout << "\nТочка вне выделенных областей";
  154.  
  155. cout << "\nПовторить-1, Выход-2: ";
  156.  
  157. int choice;
  158. cin >> choice;
  159. if (choice == 1) main();
  160. else return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement