#include #include #include #include #include #include using namespace std; struct Point { int64_t x, y; }; struct Line { Point p, q; }; // Check orientation of point triplets int64_t orient(Point p, Point q, Point r) { int64_t v = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if(v == 0) return 0; return v < 0 ? -1 : 1; } // Given three colinear points p, q, r, the function checks if // point q lies on line segment 'pr' bool on_segment(Point p, Point q, Point r) { if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) && q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y)) return true; return false; } bool line_intersect(Point p1, Point q1, Point p2, Point q2) { // Find the four orientations needed for general and // special cases int64_t o1 = orient(p1, q1, p2); int64_t o2 = orient(p1, q1, q2); int64_t o3 = orient(p2, q2, p1); int64_t o4 = orient(p2, q2, q1); // General case if (o1 != o2 && o3 != o4) return true; // Special Cases // p1, q1 and p2 are colinear and p2 lies on segment p1q1 if (o1 == 0 && on_segment(p1, p2, q1)) return true; // p1, q1 and p2 are colinear and q2 lies on segment p1q1 if (o2 == 0 && on_segment(p1, q2, q1)) return true; // p2, q2 and p1 are colinear and p1 lies on segment p2q2 if (o3 == 0 && on_segment(p2, p1, q2)) return true; // p2, q2 and q1 are colinear and q1 lies on segment p2q2 if (o4 == 0 && on_segment(p2, q1, q2)) return true; return false; // Doesn't fall in any of the above cases } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; cin >> n; vector ls; for(int i = 0; i < n; i++){ Point p, q; cin >> p.x >> p.y >> q.x >> q.y; ls.push_back({p,q}); } //niet 1 maar components vector > g (ls.size(), vector ()); int64_t ans = -n; for(int i = 0; i < ls.size(); i++){ for(int j = i + 1; j < ls.size(); j++){ if(line_intersect(ls[i].p, ls[i].q, ls[j].p, ls[j].q)) { ans++; g[i].push_back(j); g[j].push_back(i); } } } vector seen (ls.size(),false); for(int i = 0; i < ls.size(); i++){ if(seen[i]) continue; ans++; queue Q; Q.push(i); while(!Q.empty()){ int cur = Q.front(); Q.pop(); if(seen[cur]) continue; seen[cur] = true; for(int j : g[cur]){ if(seen[j]) continue; Q.push(j); } } } cout << ans << endl; return 0; }