Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct Point {
- int x, y;
- };
- struct Segment {
- Point p1, p2;
- };
- int countIntersections(vector<Segment>& segments) {
- int count = 0;
- vector<int> events;
- for (const auto& segment : segments) {
- events.push_back(segment.p1.y);
- events.push_back(segment.p2.y);
- }
- sort(events.begin(), events.end());
- for (size_t i = 0; i < segments.size(); ++i) {
- Segment current = segments[i];
- for (size_t j = i + 1; j < segments.size(); ++j) {
- Segment other = segments[j];
- if (current.p1.y == current.p2.y && other.p1.y == other.p2.y)
- continue; // Skip parallel segments
- if (current.p1.x <= other.p1.x && current.p2.x >= other.p1.x &&
- other.p1.y <= current.p1.y && other.p2.y >= current.p1.y)
- ++count;
- }
- }
- return count;
- }
- int main() {
- int n;
- cout << "Enter the number of segments: ";
- cin >> n;
- vector<Segment> segments;
- cout << "Enter the coordinates of each segment (x1, y1, x2, y2):" << endl;
- for (int i = 0; i < n; ++i) {
- Segment segment;
- cin >> segment.p1.x >> segment.p1.y >> segment.p2.x >> segment.p2.y;
- segments.push_back(segment);
- }
- int intersectionCount = countIntersections(segments);
- cout << "Number of intersections: " << intersectionCount << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement