-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.cpp
73 lines (53 loc) · 2.75 KB
/
Geometry.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.00)
#define eps 1e-9
#define D(x) cout << #x " = " << (x) << endl
const int inf = numeric_limits<int>::max();
bool eq(double a, double b) { return fabs( a - b ) < eps; } //two numbers are equal
struct point{
double x, y;
point(){}
point(double xx, double yy) {x = xx, y = yy;} // NEVER USE xx = 0 or yy = 0 HERE
} origin = point(0, 0);
point operator+(const point &u, const point &v) {return point(u.x + v.x, u.y + v.y);} //OK
point operator-(const point &u, const point &v) {return point(u.x - v.x, u.y - v.y);} //OK
point operator*(const point &u, double v) {return point(u.x*v, u.y*v);} //OK
point operator*(double v, const point &u) {return point(u.x*v, u.y*v);} //OK
point operator*(const point &u, const point &v) {return point(u.x * v.x - u.y * v.y, u.x * v.y + v.x * u.y);} // multiplying two complex numbers
point operator/(const point &u, double v) {assert(abs(v) > eps); return point(u.x/v, u.y/v);} //OK
bool operator != (const point &u, const point &v) {return !(eq(u.x, v.x) && eq(u.y, v.y));} //OK
ostream &operator <<(ostream &os, const point &p) {
os << "(" << p.x << "," << p.y << ")";
} //OK
bool operator <(const point &u, const point &v){
if(fabs(u.x - v.x ) < eps) return u.y + eps < v.y;
return u.x + eps < v.x;
}
double norm(point u){return sqrt(u.x * u.x + u.y * u.y);} //OK
double arg(point u){ assert(u != origin); return atan2(u.y, u.x);} //OK
point polar(double r, double theta) {return point(r * cos(theta), r * sin(theta));} //OK
double dotp(point u, point v) {return u.x * v.x + u.y * v.y;} //OK
double crsp(point u, point v) {return u.x * v.y - u.y * v.x;} //OK
point unit_vector(point u) { return u / norm(u); } //OK
point rtt(point piv, point u, double theta) {return (u - piv) * polar(1.00, theta) + piv;} //OK
point projection(point p, point st, point ed) { return dotp(ed - st, p - st) / norm(ed - st) * unit_vector(ed - st) + st;} //OK
point extend(point st, point ed, double len) { return ed + unit_vector(ed-st) * len;} //OK
point segmentProjection(point p, point st, point ed)
{
double d = dotp(p - st, ed - st) / norm(ed - st);
if(d < 0) return st;
if(d > norm(ed - st) + eps) return ed;
return st + unit_vector(ed - st) * d;
} //OK
double distancePointSegment(point p, point st, point ed) {return norm(p - segmentProjection(p, st, ed)); } //OK
double distancePointLine( point P, point st, point ed) { return norm( projection(P, st, ed) - P ); } //OK
point reflection(point p, point st, point ed){
point proj = projection(p, st, ed);
if(p != proj) return extend(p, proj, norm(p - proj));
return proj;
} //OK
int main()
{
return 0;
}