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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

template <typename T>
struct point {
	T x, y;
	point() : x(0), y(0) {}
	point(T x, T y) : x(x), y(y) {}
};

typedef point<int> pti;
typedef long double ld;

template <typename T>
point<T> vec(point<T> a, point<T> b) {
	return point<T>(b.x - a.x, b.y - a.y);
}

template <typename T>
T det(point<T> a, point<T> b) {
	return a.x * b.y - a.y * b.x;
}

template <typename T>
T right_turn(point<T> a, point<T> b, point<T> c) {
	return det(vec(a, b), vec(a, c)) < 0;
}

template <typename T>
T left_turn(point<T> a, point<T> b, point<T> c) {
	return det(vec(a, b), vec(a, c)) > 0;
}

template <typename T>
bool cmp_angle(const pair<point<T>, point<T>>& a, const pair<point<T>, point<T>>& b) {
	point<T> av = vec(a.first, a.second);
	point<T> bv = vec(b.first, b.second);

	if (av.y == 0 && av.x > 0) {
		return bv.y != 0 || bv.x < 0;
	} else if (av.y >= 0) {
		return bv.y < 0 || det(av, bv) > 0;
	} else {
		return det(av, bv) > 0 && bv.y < 0;
	}
}

template <typename T>
T dot(point<T> a, point<T> b) {
	return a.x * b.x + a.y * b.y;
}

template <typename T>
ostream& operator<< (ostream& stream, const point<T>& p) {
	stream << '(' << p.x << ", " << p.y << ')';
	return stream;
}

template <typename T>
ld dist(const point<T>& a, const point<T>& b) {
	return sqrt((ld)dot(vec(a, b), vec(a, b)));
}

template <typename T>
ld dist_line(point<T> a, point<T> b, point<T> p) {
	return (ld)det(vec(a, b), vec(a, p)) / dist(a, b);
}

template <typename T, typename S>
point<T> operator+ (const point<T>& a, const point<S>& b) {
	return point<T>(a.x + b.x, a.y + b.y);
}

template <typename T>
point<ld> operator* (ld k, const point<T>& p) {
	return point<ld>(k * p.x, k * p.y);
}

int main() {
	ios_base::sync_with_stdio(0);
	cout << fixed;
	cout.precision(11);

	int n; cin >> n;

	vector<vector<pti>> t(n, vector<pti>(2));

	for (int i = 0; i < n; i++) {
		cin >> t[i][0].x >> t[i][0].y >> t[i][1].x >> t[i][1].y;
	}

	vector<pair<pti, pti>> half;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			vector<pair<pti, pti>> cuts;

			if (i == j) {
				cuts.push_back({ t[i][0], t[i][1] });
				cuts.push_back({ t[i][1], t[i][0] });
			} else {
				for (int p = 0; p < 4; p++) {
					int ip = p & 1, jp = p >> 1;

					if ( !left_turn(t[i][ip], t[j][jp], t[i][!ip])
					  && !left_turn(t[i][ip], t[j][jp], t[j][!jp])) {
						cuts.push_back({ t[i][ip], t[j][jp] });
					}
				}
			}

			for (auto cut : cuts) {
				bool farthest = true;
				for (int k = 0; k < n; k++) {
					if (k == i || k == j) continue;
					if (right_turn(cut.first, cut.second, t[k][0])
					 && right_turn(cut.first, cut.second, t[k][1])) {
						farthest = false;
						break;
					}
				}

				if (farthest) {
					half.push_back(cut);
				}
			}
		}
	}

	sort(half.begin(), half.end(), cmp_angle<int>);

	auto prev = half[0];
	int it = 0;
	for (unsigned i = 1; i < half.size(); i++) {
		auto vp = vec(prev.first, prev.second);
		auto vi = vec(half[i].first, half[i].second);

		if (det(vp, vi) == 0 && dot(vp, vi) > 0) {
			if (right_turn(half[i].first, half[i].second, prev.first)) {
				prev = half[it] = half[i];
			}
		} else {
			prev = half[it+1] = half[i];
			it++;
		}
	}

	half.resize(it+1);

	ld area = 0;

	for (int i = 0; i < half.size(); i++) {
		ld s = -std::numeric_limits<ld>::max(), t = std::numeric_limits<ld>::max();
		auto vi = vec(half[i].first, half[i].second);

		for (int j = 0; j < half.size(); j++) {
			if (i == j) continue;
			if (det(vi, vec(half[j].first, half[j].second)) == 0) {
				if (!left_turn(half[i].first, half[i].second, half[j].first)) {
					cout << 0 << endl;
					return 0;
				}
			} else {
				ld da = dist_line(half[j].first, half[j].second, half[i].first);
				ld db = dist_line(half[j].first, half[j].second, half[i].second);
				ld c = da / (da - db);

				if (det(vi, vec(half[j].first, half[j].second)) < 0) {
					// open
					s = max(s, c);
				} else {
					// close
					t = min(t, c);
				}
			}

			if (t < s) break;
		}


		if (s < t) {
			area += det(s * vi + half[i].first, t * vi + half[i].first);
		}
	}

	area /= 2.0;

	cout << std::abs(area) << endl;

	return 0;
}