Advertisement
STANAANDREY

Untitled

May 7th, 2023
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Point {
  8. int x, y;
  9. };
  10.  
  11. struct Segment {
  12. Point p1, p2;
  13. };
  14.  
  15. int countIntersections(vector<Segment>& segments) {
  16. int count = 0;
  17. vector<int> events;
  18.  
  19. for (const auto& segment : segments) {
  20. events.push_back(segment.p1.y);
  21. events.push_back(segment.p2.y);
  22. }
  23.  
  24. sort(events.begin(), events.end());
  25.  
  26. for (size_t i = 0; i < segments.size(); ++i) {
  27. Segment current = segments[i];
  28. for (size_t j = i + 1; j < segments.size(); ++j) {
  29. Segment other = segments[j];
  30. if (current.p1.y == current.p2.y && other.p1.y == other.p2.y)
  31. continue; // Skip parallel segments
  32.  
  33. if (current.p1.x <= other.p1.x && current.p2.x >= other.p1.x &&
  34. other.p1.y <= current.p1.y && other.p2.y >= current.p1.y)
  35. ++count;
  36. }
  37. }
  38.  
  39. return count;
  40. }
  41.  
  42. int main() {
  43. int n;
  44. cout << "Enter the number of segments: ";
  45. cin >> n;
  46.  
  47. vector<Segment> segments;
  48. cout << "Enter the coordinates of each segment (x1, y1, x2, y2):" << endl;
  49. for (int i = 0; i < n; ++i) {
  50. Segment segment;
  51. cin >> segment.p1.x >> segment.p1.y >> segment.p2.x >> segment.p2.y;
  52. segments.push_back(segment);
  53. }
  54.  
  55. int intersectionCount = countIntersections(segments);
  56. cout << "Number of intersections: " << intersectionCount << endl;
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement