-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintersection.cpp
50 lines (44 loc) · 1.13 KB
/
intersection.cpp
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
#include <iostream>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex <ld> point;
const ld EPS = 1e-9;
ld operator ^(const point &a, const point &b) {
return imag(a * conj(b));
}
ld operator |(const point &a, const point &b) {
return real(a * conj(b));
}
bool in_between(const point &a, const point &b, const point &c) {
if(norm(a - c) <= 2 * EPS || norm(b - c) <= 2 * EPS)
return true;
ld t = norm(a - b);
return (abs((a - b) ^ (c - b)) <= EPS) && (norm(a - c) <= t) && (norm(b - c) <= t);
}
point intersection(const point &a, const point &b, const point &c, const point &d) {
if(abs((b - a) ^ (d - c)) <= EPS) {
if(in_between(a, b, c))
return c;
if(in_between(a, b, d))
return d;
//if(in_between(c, d, a))
return a;
}
ld s = ((c - a) ^ (d - c)) / ((b - a) ^ (d - c));
return a + s * (b - a);
}
int main() {
ios::sync_with_stdio(false);
ld ta, tb;
point a[4];
for(int i = 0;i < 4;++i)
cin >> ta >> tb, a[i] = point(ta, tb);
point b = intersection(a[0], a[1], a[2], a[3]);
if(in_between(a[0], a[1], b))
cout << b << endl;
else
cout << "No" << endl;
return 0;
}