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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <bits/stdc++.h>
using namespace std;

// #define DBG
#ifdef DBG
#define debug printf
#else
#define debug(...)
#endif

/// Gemoetria zapozyczona z biblioteczki UJ
namespace geo {

using K = long double;

const K EPS = 1e-9;

bool is_zero(K x) { return fabs(x) < EPS; }
int sgn(K x) { return x < -EPS ? -1 : x > EPS; }

struct point {
  K x, y;
  K atan() const {
    return atan2(y, x);
  }
  K len() const {
    return x*x + y*y;
  }
};

K det(point a, point b) { return a.x * b.y - a.y * b.x; }

point operator-(const point &a, const point &b) { return {a.x - b.x, a.y - b.y}; }
point operator-(const point &a) { return {-a.x, -a.y}; }
point operator+(const point &a, const point &b) { return {a.x + b.x, a.y + b.y}; }
bool operator<(const point &a, const point &b) {
  return !is_zero(a.x-b.x) ? a.x < b.x : a.y - b.y < -EPS;
}
bool operator==(const point &a, const point &b) {
  return !(a < b) && !(b < a);
}
bool operator!=(const point &a, const point &b) {
  return a < b || b < a;
}

struct segment {
  point a, b;
  K y(K x) const { return (x-a.x)*(b.y-a.y)/(b.x-a.x) + a.y; }
};

bool intersect(const segment &s, const segment &t, point &p) {
  if(sgn(det(s.b-s.a, t.a-s.a)) * sgn(det(s.b-s.a, t.b-s.a)) > 0) return false;
  if(sgn(det(t.b-t.a, s.a-t.a)) * sgn(det(t.b-t.a, s.b-t.a)) > 0) return false;
  K SA = s.a.y - s.b.y, SB = s.b.x - s.a.x; K SC = SA * s.a.x + SB * s.a.y;
  K TA = t.a.y - t.b.y, TB = t.b.x - t.a.x; K TC = TA * t.a.x + TB * t.a.y;
  K W = det({SA, SB}, {TA, TB});
  K Wx = det({SC, SB}, {TC, TB});
  K Wy = det({SA, SC}, {TA, TC});
  p = {Wx / W, Wy / W};
  return true;
}

namespace hull {
using P = point;
#define SIZE(x) (int)((x).size())
#define ALL(x) (x).begin(),(x).end()

bool left(P const& a, P const& b, P const& c) {
  return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y) < EPS;
}

void add_point(vector<P>& hull, P pt, int limit) {
  while(SIZE(hull) > limit && left(hull[SIZE(hull)-2], hull[SIZE(hull)-1], pt))
  hull.pop_back();
  hull.push_back(pt);
}

vector<P> compute_hull(vector<P>& in) {
  sort(ALL(in));
  in.resize(unique(ALL(in)) - in.begin());
  if (SIZE(in) <= 1) return in;
  vector<P> out;
  for (auto p : in) add_point(out, p, 1);
  int cursize = SIZE(out);
  for (int i = SIZE(in)-2; i >= 0; i--) add_point(out, in[i], cursize);
  out.pop_back();
  return out;
}
} // hull
} // geo

const int MAX = 100;
pair<geo::point, geo::point> arr[MAX];
int n;

bool between(geo::K a, geo::K b, geo::K t) {
  if (a < b) {
    return a < t + geo::EPS && t < b + geo::EPS;
  }
  return a < t + geo::EPS || t < b + geo::EPS;
}

bool is_safe(geo::point p) {
  static const geo::K pi = atan(1.0l) * 4;
  static vector<pair<geo::K, int>> tans;
  tans.clear();
  for (int i = 0; i < n; i++) {
    auto det = geo::det(arr[i].first - p, arr[i].second - p);
    if (geo::is_zero(det)) {
      if (between(min(arr[i].first.x, arr[i].second.x), max(arr[i].first.x, arr[i].second.x), p.x) &&
          between(min(arr[i].first.y, arr[i].second.y), max(arr[i].first.y, arr[i].second.y), p.y) &&
          arr[i].first != p && arr[i].second != p) {
        continue;
      }
      if ((p - arr[i].first).len() < (p - arr[i].second).len()) {
        swap(arr[i].first, arr[i].second);
      }
    } else {
      if (det < 0) {
        swap(arr[i].first, arr[i].second);
      }
    }
    debug("det %Lf\n", det);

    debug("%d: %Lf %Lf  %Lf %Lf\n", i, arr[i].first.x, arr[i].first.y, arr[i].second.x, arr[i].second.y);

    geo::K b, e;
    if (geo::is_zero(det)) {
      auto q = arr[i].first - arr[i].second;
      swap(q.x, q.y);
      q.y = -q.y;
      b = q.atan();
      q = -q;
      e = q.atan();
    } else {
      auto q1 = p - arr[i].second;
      swap(q1.x, q1.y);
      q1.x = -q1.x;
      auto q2 = p - arr[i].first;
      swap(q2.x, q2.y);
      q2.y = -q2.y;
      b = q1.atan();
      e = q2.atan();
    }

    tans.push_back({b, 0});
    tans.push_back({e, 1});
    debug("push back %Lf %Lf\n", b, e);
    debug("degrees %d %Lf %Lf\n", i, b / M_PI * 180, e / M_PI * 180);
    if (b > e) {
      tans.push_back({pi, 1});
      tans.push_back({-pi, 0});
    }
  }
  sort(tans.begin(), tans.end());
  if (tans.empty()) {
    return false;
  }

  if (tans[0] != make_pair(-pi, 0) || tans.back() != make_pair(pi, 1)) { // TODO FIXME
    return false;
  }
  int c = 0;
  tans.pop_back();
  for (int i = 0; i < (int)tans.size(); i++) {
    debug("wot %Lf\n", tans[i].first);
    while (i + 1 < (int)tans.size() && geo::is_zero(tans[i].first - tans[i+1].first)) {
      if (tans[i].second == 0) {
        c++;
      } else {
        c--;
      }
      i++;
    }
    if (tans[i].second == 0) {
      c++;
    } else {
      c--;
      if (c == 0) {
        debug("nope %Lf\n", tans[i].first);
        return false;
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  vector<geo::point> all, safe;
  for (int i = 0; i < n; i++) {
    scanf("%Lf %Lf %Lf %Lf", &arr[i].first.x, &arr[i].first.y, &arr[i].second.x, &arr[i].second.y);
    all.push_back(arr[i].first);
    all.push_back(arr[i].second);
  }
  // geo::point p; geo::intersect({{0, 0}, {4, 1}}, {{2, 2}, {4, 0}}, p); debug("%Lf %Lf\n", p.x, p.y);
  // cout << is_safe({6, 6}) << endl; return 0;
  for (int a = 0; a < 2 * n; a++) {
    if (is_safe(all[a])) {
      safe.push_back(all[a]);
    }
    for (int b = a + 1; b < 2 * n; b++) {
      for (int c = b + 1; c < 2 * n; c++) {
        for (int d = c + 1; d < 2 * n; d++) {
          geo::point p;
          if (geo::intersect({all[a], all[b]}, {all[c], all[d]}, p)) {
            if (is_safe(p)) {
              safe.push_back(p);
            }
          }
          if (geo::intersect({all[a], all[c]}, {all[b], all[d]}, p)) {
            if (is_safe(p)) {
              safe.push_back(p);
            }
          }
          if (geo::intersect({all[a], all[d]}, {all[c], all[b]}, p)) {
            if (is_safe(p)) {
              safe.push_back(p);
            }
          }
        }
      }
    }
  }
  debug("safe\n"); for (auto p : safe) { debug("%Lf %Lf\n", p.x, p.y); }
  auto hull = geo::hull::compute_hull(safe);
  debug("hull\n"); for (auto p : hull) { debug("%Lf %Lf\n", p.x, p.y); }
  geo::K res = 0.0;
  for (int i = 2; i < (int)hull.size(); i++) {
    res += abs(geo::det(hull[i] - hull[0], hull[i-1] - hull[0]));
  }
  res /= 2.0;
  printf("%.13Lf\n", res);
}