Advertisement
juxtapositions

Untitled

Feb 19th, 2023
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. // Define a structure to represent a point
  6. struct Point {
  7.     double x, y;
  8. };
  9.  
  10. // Calculate the cross product of two vectors
  11. double crossProduct(Point a, Point b, Point c) {
  12.     return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  13. }
  14.  
  15. // Check if two points are on the same side of a line passing through two other points
  16. bool sameSide(Point a, Point b, Point c, Point d) {
  17.     double cp1 = crossProduct(c, d, a);
  18.     double cp2 = crossProduct(c, d, b);
  19.     return cp1 * cp2 >= 0;
  20. }
  21.  
  22. // Check if there exists a point that all the other thirteen points lie on the same side of the line passing through these points
  23. bool hasSameSidePoint(Point points[], int n) {
  24.     for (int i = 0; i < n; i++) {
  25.         int count1 = 0, count2 = 0;
  26.         for (int j = 0; j < n; j++) {
  27.             if (i != j) {
  28.                 if (sameSide(points[i], points[j], points[i+1], points[(i+2)%n])) {
  29.                     count1++;
  30.                 }
  31.                 if (sameSide(points[i], points[j], points[i+1], points[(i+n-1)%n])) {
  32.                     count2++;
  33.                 }
  34.             }
  35.         }
  36.         if (count1 == n-2 || count2 == n-2) {
  37.             return true;
  38.         }
  39.     }
  40.     return false;
  41. }
  42.  
  43. int main() {
  44.     // Define the set of points
  45.     Point points[15] = {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {14, 14}};
  46.     int n = 15;
  47.  
  48.     // Check if there exists a point that all the other thirteen points lie on the same side of the line passing through these points
  49.     bool result = hasSameSidePoint(points, n);
  50.  
  51.     // Output the result
  52.     if (result) {
  53.         cout << "There exists a point that all the other thirteen points lie on the same side of the line passing through these points." << endl;
  54.     } else {
  55.         cout << "There does not exist a point that all the other thirteen points lie on the same side of the line passing through these points." << endl;
  56.     }
  57.  
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement