Advertisement
asdfg0998

fearfe

Sep 15th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int countCollisions(const vector<pair<int, int>>& centers) {
  8.     int n = centers.size();
  9.     int collisionCount = 0;  // Variable to store the number of colliding pairs
  10.  
  11.     // Loop through all pairs of objects
  12.     for (int i = 0; i < n; ++i) {
  13.         for (int j = i + 1; j < n; ++j) {
  14.             int x1 = centers[i].first;
  15.             int y1 = centers[i].second;
  16.             int x2 = centers[j].first;
  17.             int y2 = centers[j].second;
  18.  
  19.             // Check collision conditions: |x1 - x2| <= 2 and |y1 - y2| <= 2
  20.             if (abs(x1 - x2) <= 2 && abs(y1 - y2) <= 2) {
  21.                 ++collisionCount;
  22.             }
  23.         }
  24.     }
  25.  
  26.     return collisionCount;  // Return the total number of colliding pairs
  27. }
  28.  
  29. int main() {
  30.     // Example input: List of object centers
  31.     vector<pair<int, int>> centers = {
  32.         {1, 1}, {2, 2}, {0, 4},{1,1}
  33.     };
  34.  
  35.     // Calculate and output the number of colliding pairs
  36.     int result = countCollisions(centers);
  37.     cout << "Number of colliding pairs: " << result << endl;  // Expected output: 3
  38.  
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement