Advertisement
kokokozhina

Untitled

Dec 10th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <iostream> //exercise 6
  2. #include <string> //there is a set of points on the plane
  3. #include <vector> //find all points which is in rectangle formed by {x1,y1} {x2,y2} {x3,y3} {x4,y4}
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct dot
  9. {
  10.     int x, y;
  11.     dot(int a, int b) : x(a), y(b) {};
  12. };
  13.  
  14. vector<dot> DOT;
  15.  
  16. double sideLength(int a, int b)
  17. {
  18.     return sqrt((DOT[a].x - DOT[b].x) * (DOT[a].x - DOT[b].x) + (DOT[a].y - DOT[b].y) * (DOT[a].y- DOT[b].y));
  19. }
  20.  
  21. double squareTriangle(int a, int b, int c)
  22. {
  23.     double side1 = sideLength(a, b);
  24.     double side2 = sideLength(b, c);
  25.     double side3 = sideLength(c, a);
  26.    
  27.     double p = (side1 + side2 + side3) / 2;
  28.    
  29.     double square = sqrt(p * (p - side1) * (p - side2) * (p - side3));
  30.  
  31.     return square;
  32. }
  33.  
  34. int main()
  35. {
  36.     int a, b, counter = 0;
  37.  
  38.     cout << "Please, input the coordinates of rectangle in true way: like clockwise or counter-clockwise" << endl;
  39.     for(int i = 0; i < 4; i++)
  40.     {
  41.         cout << "Enter the x" << i + 1 << " and y" << i + 1 << endl;
  42.         cin >> a >> b;
  43.         DOT.push_back(dot(a, b));
  44.     }
  45.  
  46.     cout << "Enter coordinates of other dots. End the input by typing a letter." << endl;
  47.     while(cin >> a)
  48.     {
  49.         cin >> b;
  50.         DOT.push_back(dot(a, b));
  51.         cout << "Coordinates of this dot are " << DOT.back().x << "," << DOT.back().y << endl;
  52.         counter++;
  53.     }
  54.  
  55.     double eps = 0.000001;
  56.     double square = sideLength(0, 1) * sideLength(2, 1);
  57.     bool was = false;
  58.  
  59.     for(int i = 0; i < counter; i++)
  60.     {
  61.         double squareSum = squareTriangle(0, 1, i + 4) + squareTriangle(1, 2, i + 4) + squareTriangle(2, 3, i + 4) + squareTriangle(3, 0, i + 4);
  62.         if (abs(squareSum - square) < eps)
  63.         {
  64.             cout << "Lucky dot is " << DOT[i + 4].x << "," << DOT[i + 4].y << endl;
  65.             was = true;
  66.         }
  67.     }
  68.  
  69.     if(!was)
  70.         cout << "No lucky dots\n";
  71.  
  72.     system("pause");
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement