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
218
219
220
// Artur Kraska, II UWr

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <list>
#include <set>
#include <map>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007
#define EPS 1e-12

#define wekt(a, b, p) ((b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x))

using namespace std;

int n, m, a;
set <pair<long double, long double> > S;

struct punkt
{
    long long x, y;
};

struct punktd
{
    long double x, y;
};
vector <punktd> pd, pd2;
punktd base;

struct odcinek // skierowany
{
    punkt p1, p2;
};
odcinek tab[105];
vector <pair<punkt, punkt> > v, v2;

bool comp(punktd a, punktd b)
{
    return wekt(base, a, b) >= 0;//EPS;
}

int comp2(punkt a, punkt b)
{
    if(a.x == b.x)
        return a.y-b.y;
    return a.x-b.x;
}

void test(int i, int j, punkt a, punkt b)
{
    if(wekt(a, b, tab[i].p1) <= 0 &&
       wekt(a, b, tab[i].p2) <= 0 &&
       wekt(a, b, tab[j].p1) <= 0 &&
       wekt(a, b, tab[j].p2) <= 0)
    {
        forr(k, n)
        {
            if(wekt(a, b, tab[k].p1) < 0 && wekt(a, b, tab[k].p2) < 0)
                return ;
        }
        v.PB(MP(a, b));
    }
}

punktd przeciecie(punkt a, punkt b, punkt c, punkt d)
{
    punktd pkt;
    long long licznik = ((a.y-c.y)*(b.x-a.x) - (a.x-c.x)*(b.y-a.y));
    long long mianownik = ((d.y-c.y)*(b.x-a.x) - (d.x-c.x)*(b.y-a.y));

    if(mianownik == 0)
    {
        pkt.x = EPS;
        pkt.y = EPS; // nie robi nam to roznicy
        return pkt;
    }

    long double t = (long double)licznik / (long double)mianownik;
    long double t0 = (c.x-a.x + t*(d.x-c.x))/(b.x-a.x);
/*
    if(b.x == a.x || t < -EPS || t > 1+EPS || t0 < -EPS || t0 > 1+EPS)
    {
        pkt.x = EPS;
        pkt.y = EPS; // nie robi nam to roznicy
        return pkt;
    }
*/
    pkt.x = c.x + t*(d.x-c.x);
    pkt.y = c.y + t*(d.y-c.y);
    return pkt;
}

int main()
{
    cin >> n;
    forr(i, n)
    {
        cin >> tab[i].p1.x >> tab[i].p1.y >> tab[i].p2.x >> tab[i].p2.y;
    }

    // znajdz wszystkie odcinki ograniczajace
    // wkladaj do wektora v
    forr(i, n)
        FOR(j, i+1, n-1)
        {
            test(i, j, tab[i].p1, tab[j].p1);
            test(i, j, tab[i].p1, tab[j].p2);
            test(i, j, tab[i].p2, tab[j].p1);
            test(i, j, tab[i].p2, tab[j].p2);
            test(i, j, tab[j].p1, tab[i].p1);
            test(i, j, tab[j].p1, tab[i].p2);
            test(i, j, tab[j].p2, tab[i].p1);
            test(i, j, tab[j].p2, tab[i].p2);
        }

    // filtrujemy zle do v2
    for(auto odc : v)
    {
        //cout << "(" << odc.first.x << ", " << odc.first.y << ") - (" << odc.second.x << ", " << odc.second.y << ")" << endl;
        bool leave = 0;
        for(auto odc2 : v2)
        {
            if(comp2(odc2.first, odc2.second) * comp2(odc.first, odc.second) > 0 &&
               wekt(odc2.first, odc2.second, odc.first) == 0 &&
               wekt(odc2.first, odc2.second, odc.second) == 0)
            {
//                cout << "   odrzuca (" << odc.first.x << ", " << odc.first.y << ") - (" << odc.second.x << ", " << odc.second.y << ")" << endl;
                leave = 1;
                break;
            }
        }
        if(!leave)
        {
            v2.PB(odc);
//            cout << "(" << odc.first.x << ", " << odc.first.y << ") - (" << odc.second.x << ", " << odc.second.y << ")" << endl;
        }
    }

    // zbierz przeciecia
    forr(i, v2.size())
        FOR(j, i+1, v2.size()-1)
        {
            punktd p = przeciecie(v2[i].first, v2[i].second, v2[j].first, v2[j].second);
            if(p.x != EPS)
            {
                pd.PB(p);
//                cout << "przeciecie: (" << p.x << ", " << p.y << ")" << endl;
            }
        }

    // wywal zbedne
    for(auto p : pd)
    {
        bool leave = 0;
        for(auto odc : v2)
        {
            if(wekt(odc.first, odc.second, p) < -EPS)
            {
                leave = 1;
                break;
            }
        }
        if(!leave)
        {
            S.insert(MP(p.x, p.y));
//            cout << "dobry punkt: (" << p.x << ", " << p.y << ")" << endl;
        }
    }

    for(auto p : S)
    {
		punktd p0;
		p0.x = p.first;
		p0.y = p.second;
		pd2.PB(p0);
	}

    long double pole = 0;
    if(pd2.size() > 2)
    {
        // posortuj (znajdz otoczke) i znajdz pole
        base = pd2[0];
        sort(pd2.begin()+1, pd2.end(), comp);

/*        for(auto p : pd2)
        {
			cout << "sort: " << p.x << ", " << p.y << endl;
		}
*/
        FOR(i, 2, pd2.size()-1)
        {
            pole += wekt(base, pd2[i-1], pd2[i]);
        }
    }

    //cout.precision(30);
    printf("%.13f\n", (double)abs(pole)/2);

    return 0;
}