1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a) FOR(i, 0, a - 1)
#define ST first
#define ND second
#define V vector
#define RS resize
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
#define S(a) (int)a.size()

template<class T> void db(T a) { cerr << a; }
template<class L, class R> void db(pair<L, R> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; }
template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; }
template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; }
template<class T, class... TS> void dump(const char *s, T a, TS... x)
{ while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); }

#ifdef DEBUG
#define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); 
#else
#define DB(...)
#endif

using LL = long long;
using PII = pair<int, int>;
using VI = V<int>;
using VLL = V<LL>;
using VVI = V<VI>;
using VPII = V<PII>;

using ld = long double;
using Point = pair<ld, ld>;
#define X first
#define Y second
Point operator-(Point a, Point b) { return {a.X - b.X, a.Y - b.Y}; }
ld cross(Point a, Point b) { return a.X * b.Y - a.Y * b.X; }
ld cross(Point a, Point b, Point c) { return cross(b - a, c - a); }
struct line { Point t[2]; };

ld epsilon = 1e-10;
int get_sign(ld a)
{
    if(a < -epsilon) return -1;
    if(a > +epsilon) return +1;
    return 0;
}

struct Plane
{
    V<Point> points;
    Plane()
    {
        points.EB(-500, +500);
        points.EB(+500, +500);
        points.EB(+500, -500);
        points.EB(-500, -500);
    }

    void rebuild()
    {
        sort(ALL(points));

        V<Point> hull;
        int fixed = 0;
        auto add = [&](Point point)
        {
            while(S(hull) - fixed >= 2 && cross(hull[S(hull) - 2], hull[S(hull) -1], point) >= -epsilon)
                hull.pop_back();
            hull.EB(point);
        };
    
        REP(i, S(points))
            add(points[i]);
        hull.pop_back();
        fixed = S(hull);
        for(int i = S(points) - 1; i >= 0; i--)
            add(points[i]);
        hull.pop_back();

        points = hull;
    }

    bool intersect(Point a, Point b, Point q, Point r)
    {
        auto f = [](Point x, Point y, Point z) { return get_sign(cross(x, y, z)); };
        return (f(a, b, q) * f(a, b, r) == -1);
    }

    Point intersection(Point a, Point b, Point q, Point r)
    {
        auto A = [](Point a, Point b) { return b.Y - a.Y; };
        auto B = [](Point a, Point b) { return a.X - b.X; };
        auto C = [](Point a, Point b, ld A, ld B) { return A * a.X + B * a.Y; };
        ld A1 = A(a, b), B1 = B(a, b), C1 = C(a, b, A1, B1);
        ld A2 = A(q, r), B2 = B(q, r), C2 = C(q, r, A2, B2);
        ld det = A1 * B2 - A2 * B1;
        ld x = (B2 * C1 - B1 * C2) / det;
        ld y = (A1 * C2 - A2 * C1) / det;
        return {x, y};
    }

    bool empty = 0;

    void cut(Point a, Point b)
    {
        V<Point> to_add;

        REP(i, S(points))
        {
            int next = (i + 1 == S(points) ? 0 : i + 1);
            if(intersect(a, b, points[i], points[next]))
                to_add.EB(intersection(a, b, points[i], points[next]));
        }

        int pos = 0;
        while(pos < S(points))
        {
            ld crs = cross(a, b, points[pos]);
            if(cross(a, b, points[pos]) < -epsilon)
            {
                swap(points[pos], points.back());
                points.pop_back();
            }
            else
                pos++;
        }

        REP(i, S(to_add))
            points.EB(to_add[i]);

        if(S(points) > 2)
            rebuild();
        else
            empty = 1;

        DB(points);
    }

    ld area()
    {
        ld sum = 0;
        REP(i, S(points))
        {
            int next = (i + 1 == S(points) ? 0 : i + 1);
            sum += cross(points[next], points[i]) / 2;
        }
        return sum;
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n; 
    cin >> n;
    V<line> v(n);
    REP(i, n)
    {
        ld x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        v[i].t[0] = {x1, y1};
        v[i].t[1] = {x2, y2};
    }

    Plane plane;
    cout.precision(13);
    cout << fixed;
    REP(i, n) REP(q, 2) REP(j, i) REP(r, 2)
    {
        Point a = v[i].t[q];
        Point b = v[j].t[r];

        int side = 0;
        bool OK = true;
        REP(k, n)
        {
            Point v1 = v[k].t[0];
            Point v2 = v[k].t[1];
            ld d1 = cross(a, b, v1);
            ld d2 = cross(a, b, v2);
            int c1 = get_sign(d1);
            int c2 = get_sign(d2);

            if(c1 == c2 && c1 != 0)
            {
                if(side == 0)
                    side = c1;

                if(side != c1)
                {
                    OK = false;
                    break;
                }
            }
        }

        if(OK && side != 0)
        {
            if(side == -1)
                swap(a, b);
            plane.cut(a, b);
        }

        if(plane.empty || side == 0)
        {                            
            cout << ld(0) << "\n";
            return 0;
        }
    }

    cout << plane.area() << "\n";
    return 0;
}