-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpoints_and_vectors.cpp
More file actions
113 lines (110 loc) · 3.13 KB
/
points_and_vectors.cpp
File metadata and controls
113 lines (110 loc) · 3.13 KB
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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define mod 1000000007
#define PI acos(-1)
namespace p
{
struct pt
{
double x, y;
pt operator+(pt p) { return {x + p.x, y + p.y}; } // soma de pontos
pt operator-(pt p) { return {x - p.x, y - p.y}; } // subtração de pontos
pt operator*(double d) { return {x * d, y * d}; } // multiplicação por um double
pt operator/(double d) { return {x / d, y / d}; } // divisão por um double
};
double dot(pt v, pt w) // produto escalar (dot product)
{
return v.x * w.x + v.y * w.y;
}
bool is_perp(pt v, pt w) // retorna se dois vetores sao perpendiculares (angulo 90 graus)
{
return dot(v, w) == 0;
}
double cross(pt v, pt w) // produto vetorial (cross product)
{
return v.x * w.y - v.y * w.x;
}
double dist(pt a, pt b) // distancia entre 2 pontos
{
pt c = a - b;
return sqrt(c.x * c.x + c.y * c.y);
}
double dist2(pt a, pt b) // retorna o quadrado da distancia entre dois pontos
{
pt c = a - b;
return c.x * c.x + c.y * c.y;
}
bool is_colinear(pt a, pt b, pt c) // retorna se os pontos a, b e c são colineares
{
return cross(b - a, c - a) == 0;
}
bool ccw(pt a, pt b, pt c) // retorna se os pontos a,b e c estão no sentido anti horario
{
return cross(b - a, c - b) > 0;
}
bool cw(pt a, pt b, pt c) // retorna se os pontos a,b e c estão no sentido horario
{
return cross(b - a, c - b) < 0;
}
double modulo(pt v) // |v| = sqrt(x² + y²)
{
return sqrt(v.x * v.x + v.y * v.y);
}
double angle(pt a, pt b, pt c) // angulo entre os vetores ab e ac
{
// dot(ab , ac) / |ab| * |ac|
pt ab = b - a; // vetor ab
pt ac = c - a; // vetor ac
double m1 = modulo(ab);
double m2 = modulo(ac);
double m3 = m1 * m2;
return (dot(ab, ac) / m3); // retorna o cos do angulo em graus
}
pt rotate(pt p, double a) // rotacionar o ponto p em relação a origem, em a graus, no sentido anti-horario
{
a = (a * PI) / 180;
double xx = (cos(a) * p.x) + ((sin(a) * -1) * p.y);
double yy = (sin(a) * p.x) + (cos(a) * p.y);
pt ans = {xx, yy};
return ans;
}
double polar(pt p) // polar angle
{
return atan2l(p.y, p.x);
}
bool cmp(pt a, pt b) // ordenar pontos pelo polar angle
{
return polar(a) < polar(b);
}
bool cmp_x(pt a, pt b) // ordenar os pontos pela coordenada x
{
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
pt polar_to_cartesian(double r, double theta) // r - distancia do centro, theta - polar angle
{
pt ans;
ans.x = r * cos(double(theta) / 180 * PI); // assumindo que theta ta em graus, transforma pra radiano
ans.y = r * sin(double(theta) / 180 * PI);
return ans;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}