forked from mateusvrs/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clang(format): cpp files correct indentation
- Loading branch information
1 parent
91262ed
commit 87c817e
Showing
8 changed files
with
438 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,92 @@ | ||
// Definição da classe Point e da função equals() | ||
|
||
template<typename T> | ||
template <typename T> | ||
struct Circle { | ||
Point<T> C; | ||
T r; | ||
Point<T> C; | ||
T r; | ||
|
||
enum { IN, ON, OUT } PointPosition; | ||
enum { IN, ON, OUT } PointPosition; | ||
|
||
PointPosition position(const Point& P) const | ||
{ | ||
auto d = dist(P, C); | ||
PointPosition position(const Point& P) const { | ||
auto d = dist(P, C); | ||
|
||
return equals(d, r) ? ON : (d < r ? IN : OUT); | ||
} | ||
return equals(d, r) ? ON : (d < r ? IN : OUT); | ||
} | ||
|
||
static std::optional<Circle> | ||
from_2_points_and_r(const Point<T>& P, const Point<T>& Q, T r) | ||
{ | ||
double d2 = (P.x - Q.x) * (P.x - Q.x) + (P.y - Q.y) * (P.y - Q.y); | ||
double det = r * r / d2 - 0.25; | ||
static std::optional<Circle> from_2_points_and_r(const Point<T>& P, | ||
const Point<T>& Q, T r) { | ||
double d2 = (P.x - Q.x) * (P.x - Q.x) + (P.y - Q.y) * (P.y - Q.y); | ||
double det = r * r / d2 - 0.25; | ||
|
||
if (det < 0.0) | ||
return { }; | ||
if (det < 0.0) return {}; | ||
|
||
double h = sqrt(det); | ||
double h = sqrt(det); | ||
|
||
auto x = (P.x + Q.x) * 0.5 + (P.y - Q.y) * h; | ||
auto y = (P.y + Q.y) * 0.5 + (Q.x - P.x) * h; | ||
auto x = (P.x + Q.x) * 0.5 + (P.y - Q.y) * h; | ||
auto y = (P.y + Q.y) * 0.5 + (Q.x - P.x) * h; | ||
|
||
return Circle<T>{ Point<T>(x, y), r }; | ||
} | ||
return Circle<T>{Point<T>(x, y), r}; | ||
} | ||
|
||
static std::experimental::optional<Circle> | ||
from_3_points(const Point<T>& P, const Point<T>& Q, const Point<T>& R) | ||
{ | ||
auto a = 2*(Q.x - P.x); | ||
auto b = 2*(Q.y - P.y); | ||
auto c = 2*(R.x - P.x); | ||
auto d = 2*(R.y - P.y); | ||
static std::experimental::optional<Circle> from_3_points(const Point<T>& P, | ||
const Point<T>& Q, | ||
const Point<T>& R) { | ||
auto a = 2 * (Q.x - P.x); | ||
auto b = 2 * (Q.y - P.y); | ||
auto c = 2 * (R.x - P.x); | ||
auto d = 2 * (R.y - P.y); | ||
|
||
auto det = a*d - b*c; | ||
auto det = a * d - b * c; | ||
|
||
// Pontos colineares | ||
if (equals(det, 0)) | ||
return { }; | ||
// Pontos colineares | ||
if (equals(det, 0)) return {}; | ||
|
||
auto k1 = (Q.x*Q.x + Q.y*Q.y) - (P.x*P.x + P.y*P.y); | ||
auto k2 = (R.x*R.x + R.y*R.y) - (P.x*P.x + P.y*P.y); | ||
auto k1 = (Q.x * Q.x + Q.y * Q.y) - (P.x * P.x + P.y * P.y); | ||
auto k2 = (R.x * R.x + R.y * R.y) - (P.x * P.x + P.y * P.y); | ||
|
||
// Solução do sistema por Regra de Cramer | ||
auto cx = (k1*d - k2*b)/det; | ||
auto cy = (a*k2 - c*k1)/det; | ||
// Solução do sistema por Regra de Cramer | ||
auto cx = (k1 * d - k2 * b) / det; | ||
auto cy = (a * k2 - c * k1) / det; | ||
|
||
Point<T> C { cx, cy }; | ||
auto r = distance(P, C); | ||
Point<T> C{cx, cy}; | ||
auto r = distance(P, C); | ||
|
||
return Circle<T>(C, r); | ||
} | ||
return Circle<T>(C, r); | ||
} | ||
|
||
// Interseção entre o círculo c e a reta que passa por P e Q | ||
template<typename T> std::vector<Point<T>> | ||
intersection(const Circle<T>& c, const Point<T>& P, const Point<T>& Q) | ||
{ | ||
// Interseção entre o círculo c e a reta que passa por P e Q | ||
template <typename T> | ||
std::vector<Point<T>> intersection(const Circle<T>& c, const Point<T>& P, | ||
const Point<T>& Q) { | ||
auto a = pow(Q.x - P.x, 2.0) + pow(Q.y - P.y, 2.0); | ||
auto b = 2*((Q.x - P.x) * (P.x - c.C.x) + (Q.y - P.y) * (P.y - c.C.y)); | ||
auto d = pow(c.C.x, 2.0) + pow(c.C.y, 2.0) + pow(P.x, 2.0) | ||
+ pow(P.y, 2.0) + 2*(c.C.x * P.x + c.C.y * P.y); | ||
auto b = 2 * ((Q.x - P.x) * (P.x - c.C.x) + (Q.y - P.y) * (P.y - c.C.y)); | ||
auto d = pow(c.C.x, 2.0) + pow(c.C.y, 2.0) + pow(P.x, 2.0) + pow(P.y, 2.0) + | ||
2 * (c.C.x * P.x + c.C.y * P.y); | ||
auto D = b * b - 4 * a * d; | ||
|
||
if (D < 0) | ||
return { }; | ||
else if (equals(D, 0)) | ||
{ | ||
auto u = -b/(2*a); | ||
auto x = P.x + u*(Q.x - P.x); | ||
auto y = P.y + u*(Q.y - P.y); | ||
return { Point { x, y } }; | ||
return {}; | ||
else if (equals(D, 0)) { | ||
auto u = -b / (2 * a); | ||
auto x = P.x + u * (Q.x - P.x); | ||
auto y = P.y + u * (Q.y - P.y); | ||
return {Point{x, y}}; | ||
} | ||
|
||
auto u = (-b + sqrt(D))/(2*a); | ||
auto u = (-b + sqrt(D)) / (2 * a); | ||
|
||
auto x = P.x + u*(Q.x - P.x); | ||
auto y = P.y + u*(Q.y - P.y); | ||
auto x = P.x + u * (Q.x - P.x); | ||
auto y = P.y + u * (Q.y - P.y); | ||
|
||
auto P1 = Point { x, y }; | ||
auto P1 = Point{x, y}; | ||
|
||
u = (-b - sqrt(D))/(2*a); | ||
u = (-b - sqrt(D)) / (2 * a); | ||
|
||
x = P.x + u*(Q.x - P.x); | ||
y = P.y + u*(Q.y - P.y); | ||
x = P.x + u * (Q.x - P.x); | ||
y = P.y + u * (Q.y - P.y); | ||
|
||
auto P2 = Point { x, y }; | ||
auto P2 = Point{x, y}; | ||
|
||
return { P1, P2 }; | ||
} | ||
return {P1, P2}; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,31 @@ | ||
const double EPS = 1e-9; | ||
template<typename T> | ||
bool point_in_polygon( Point<T> point, vector<Point<T>> polygon) { | ||
int num_vertices = polygon.size(); | ||
T x = point.x, y = point.y; | ||
bool inside = false; | ||
Point<T> p1 = polygon[0], p2; // p1 is the first vertex | ||
for (int i = 1; i <= num_vertices; i++) { | ||
p2 = polygon[i % num_vertices]; // next vertex | ||
const double EPS = 1e-9; | ||
template <typename T> | ||
bool point_in_polygon(Point<T> point, vector<Point<T>> polygon) { | ||
int num_vertices = polygon.size(); | ||
T x = point.x, y = point.y; | ||
bool inside = false; | ||
Point<T> p1 = polygon[0], p2; // p1 is the first vertex | ||
for (int i = 1; i <= num_vertices; i++) { | ||
p2 = polygon[i % num_vertices]; // next vertex | ||
|
||
if (abs((p2.y - p1.y) * (x - p1.x) - (p2.x - p1.x) * (y - p1.y)) < EPS && | ||
(x - p1.x) * (x - p2.x) <= 0 && (y - p1.y) * (y - p2.y) <= 0) { | ||
return true; // point is on the boundary | ||
} | ||
if (abs((p2.y - p1.y) * (x - p1.x) - (p2.x - p1.x) * (y - p1.y)) < EPS && | ||
(x - p1.x) * (x - p2.x) <= 0 && (y - p1.y) * (y - p2.y) <= 0) { | ||
return true; // point is on the boundary | ||
} | ||
|
||
if (y > min(p1.y, p2.y)) { | ||
if (y <= max(p1.y, p2.y)) { | ||
if (p1.x == p2.x) { | ||
if (x <= p1.x) { | ||
inside = !inside; | ||
} | ||
} else if (x <= max(p1.x, p2.x) && x <= (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x) { | ||
inside = !inside; | ||
} | ||
} | ||
if (y > min(p1.y, p2.y)) { | ||
if (y <= max(p1.y, p2.y)) { | ||
if (p1.x == p2.x) { | ||
if (x <= p1.x) { | ||
inside = !inside; | ||
} | ||
} else if (x <= max(p1.x, p2.x) && | ||
x <= (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x) { | ||
inside = !inside; | ||
} | ||
p1 = p2; | ||
} | ||
return inside; | ||
} | ||
} | ||
p1 = p2; | ||
} | ||
return inside; | ||
} |
Oops, something went wrong.