Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Определение номера области, в которую попадает точка с произвольно заданными координатами на плоскости, и площади этой области
- #include<iostream>
- #include<cmath>
- #include <locale>
- using namespace std;
- float sqr(float x) {
- return pow(x, 2);
- }
- float M1_top(float x){
- return sqrt(1 - sqr(x + 1)) + 1;
- }
- float M1_bottom(float x){
- if (x < -1 - sqrt(3)/2)
- return -sqrt(1 - sqr(x + 1)) + 1;
- else if (x < -0.5)
- return sqrt(1 - sqr(x + 1));
- else
- return sqrt(1 - x*x);
- }
- float M2_top(float x){
- return sqrt(1 - sqr(x - 1)) + 1;
- }
- float M2_bottom(float x){
- if (x < 1 + sqrt(3)/2)
- return sqrt(1 - sqr(x - 1));
- else
- return -sqrt(1 - sqr(x - 1)) + 1;
- }
- float M3_top(float x){
- if (x < 0.5)
- return sqrt(1 - sqr(x - 1));
- else
- return sqrt(1 - x*x);
- }
- float M3_bottom(float x){
- if (x < 0.5)
- return -sqrt(1 - x*x) + 1;
- else
- return -sqrt(1 - x*x) + 1;
- }
- float M4_top(float x){
- return -sqrt(1 - x*x);
- }
- float M4_bottom(float x){
- return -sqrt(1 - sqr(x + 1));
- }
- float M5_top(float x){
- return sqrt(1 - sqr(x - 1)) - 1;
- }
- float M5_bottom(float x){
- if (x < 1)
- return sqrt(1 - x*x) - 1;
- else
- return -sqrt(1 - sqr(x - 1));
- }
- int detect_region(float x, float y){
- if ((sqr(x + 1) + sqr(y - 1) <= 1) && (x*x + y*y >= 1) && (sqr(x + 1) + y*y >= 1))
- return 1;
- else if ((sqr(x - 1) + sqr(y - 1) <= 1) && (sqr(x - 1) + y*y >= 1) && (x >= 1))
- return 2;
- 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))
- return 3;
- else if ((sqr(x + 1) + y*y <= 1) && (x*x + y*y >= 1) && (x >= -1) && ( y <= 0))
- return 4;
- else if ((sqr(x - 1) + sqr(y + 1) <= 1) && ( sqr(x - 1) + y*y <= 1) && (x*x + sqr(y + 1) >= 1))
- return 5;
- else
- return -1;
- }
- float caclulate_integral(float (*func)(float), float x_start, float x_stop, float dx){
- float area = 0;
- for(float x = x_start; x <= x_stop; x += dx) {
- area += dx * func(x);
- }
- return area;
- }
- float get_region_area(int region_number) {
- float x_start, x_stop;
- float inregral_top, inregral_bottom;
- float dx = 0.001;
- switch (region_number){
- case 1:
- x_start = -2;
- x_stop = 0;
- inregral_top = caclulate_integral(M1_top, x_start, x_stop, dx);
- inregral_bottom = caclulate_integral(M1_bottom, x_start, x_stop, dx);
- return inregral_top - inregral_bottom;
- case 2:
- x_start = 1;
- x_stop = 2;
- inregral_top = caclulate_integral(M2_top, x_start, x_stop, dx);
- inregral_bottom = caclulate_integral(M2_bottom, x_start, x_stop, dx);
- return inregral_top - inregral_bottom;
- case 3:
- x_start = 1 - sqrt(3)/2;
- x_stop = sqrt(3)/2;
- inregral_top = caclulate_integral(M3_top, x_start, x_stop, dx);
- inregral_bottom = caclulate_integral(M3_bottom, x_start, x_stop, dx);
- return inregral_top - inregral_bottom;
- case 4:
- x_start = -1;
- x_stop = -0.5;
- inregral_top = caclulate_integral(M4_top, x_start, x_stop, dx);
- inregral_bottom = caclulate_integral(M4_bottom, x_start, x_stop, dx);
- return inregral_top - inregral_bottom;
- case 5:
- x_start = 0.5;
- x_stop = 1 + sqrt(3)/2;
- inregral_top = caclulate_integral(M5_top, x_start, x_stop, dx);
- inregral_bottom = caclulate_integral(M5_bottom, x_start, x_stop, dx);
- return inregral_top - inregral_bottom;
- }
- }
- int main() {
- float x, y;
- setlocale(LC_ALL,"Russian") ;
- cout << "\nВведите координаты точки: x, y " ;
- cin >> x >> y;
- int region_number = detect_region(x, y);
- if (region_number != -1) {
- float area = get_region_area(region_number);
- cout << "\nТочка в области М" << region_number << ". S = " << area;
- }
- else
- cout << "\nТочка вне выделенных областей";
- cout << "\nПовторить-1, Выход-2: ";
- int choice;
- cin >> choice;
- if (choice == 1) main();
- else return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement