Skip to content

Commit

Permalink
clang(format): cpp files correct indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusvrs authored and github-actions[bot] committed Aug 31, 2024
1 parent 91262ed commit 87c817e
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 433 deletions.
126 changes: 60 additions & 66 deletions algorithms/geometry/circle.cpp
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};
}
};

15 changes: 10 additions & 5 deletions algorithms/geometry/convex-hull-trick.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
template <typename T = ll> struct ConvexHullTrick {
template <typename T = ll>
struct ConvexHullTrick {
static constexpr T inf = numeric_limits<T>::max();

struct Line {
Expand All @@ -20,12 +21,16 @@ template <typename T = ll> struct ConvexHullTrick {
auto it = ln.insert({a, b, 0});
while (overlap(it)) ln.erase(next(it)), update(it);
if (it != ln.begin() and !overlap(prev(it))) it = prev(it), update(it);
while (it != ln.begin() and overlap(prev(it))) it = prev(it), ln.erase(next(it)), update(it);
while (it != ln.begin() and overlap(prev(it)))
it = prev(it), ln.erase(next(it)), update(it);
}
private:

private:
void update(auto it) const {
if (next(it) == ln.end()) it->x_inter = inf;
else if (it->a == next(it)->a) (it->x_inter = it->b >= next(it)->b ? inf : -inf);
if (next(it) == ln.end())
it->x_inter = inf;
else if (it->a == next(it)->a)
(it->x_inter = it->b >= next(it)->b ? inf : -inf);
else {
auto h = (it->b - next(it)->b);
auto l = (next(it)->a - it->a);
Expand Down
22 changes: 14 additions & 8 deletions algorithms/geometry/lichao-tree.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
template <typename T = ll, T LO = T(-1e9), T HI = T(1e9)> struct LiChaoTree {
template <typename T = ll, T LO = T(-1e9), T HI = T(1e9)>
struct LiChaoTree {
// get max value at x by default
// to get min value, set inf = numeric_limits<T>::max()
static constexpr T inf = numeric_limits<T>::min();
Expand All @@ -13,7 +14,7 @@ template <typename T = ll, T LO = T(-1e9), T HI = T(1e9)> struct LiChaoTree {
struct Line {
T a, b;
array<int, 2> ch;
Line(T a_ = 0, T b_ = inf) : a(a_), b(b_), ch({-1, -1}) { }
Line(T a_ = 0, T b_ = inf) : a(a_), b(b_), ch({-1, -1}) {}
constexpr T eval(T x) const { return a * x + b; }
constexpr bool is_leaf() const { return ch[0] == -1 and ch[1] == -1; }
};
Expand All @@ -23,8 +24,10 @@ template <typename T = ll, T LO = T(-1e9), T HI = T(1e9)> struct LiChaoTree {
T query(T x, int v = 0, T l = LO, T r = HI) {
auto m = l + (r - l) / 2, val = ln[v].eval(x);
if (ln[v].is_leaf()) return val;
if (x <= m) return best(val, query(x, ch(v, 0), l, m));
else return best(val, query(x, ch(v, 1), m+1, r));
if (x <= m)
return best(val, query(x, ch(v, 0), l, m));
else
return best(val, query(x, ch(v, 1), m + 1, r));
}

void add(T a, T b) { add({a, b}, 0, LO, HI); }
Expand All @@ -35,18 +38,21 @@ template <typename T = ll, T LO = T(-1e9), T HI = T(1e9)> struct LiChaoTree {
bool R = compare(s.eval(r), ln[v].eval(r));
if (M) swap(ln[v], s), swap(ln[v].ch, s.ch);
if (s.b == inf) return;
if (L != M) add(s, ch(v, 0), l, m);
else if (R != M) add(s, ch(v, 1), m+1, r);
if (L != M)
add(s, ch(v, 0), l, m);
else if (R != M)
add(s, ch(v, 1), m + 1, r);
}

void add_segment(T a, T b, T l, T r) { add_segment({a, b}, l, r, 0, LO, HI); }
void add_segment(Line s, T l, T r, int v, T L, T R) {
if (l <= L and R <= r) return add(s, v, L, R);
auto m = L + (R - L) / 2;
if (l <= m) add_segment(s, l, r, ch(v, 0), L, m);
if (r > m) add_segment(s, l, r, ch(v, 1), m+1, R);
if (r > m) add_segment(s, l, r, ch(v, 1), m + 1, R);
}
private:

private:
int ch(int v, bool b) {
if (ln[v].ch[b] == -1) {
ln[v].ch[b] = (int)ln.size();
Expand Down
53 changes: 27 additions & 26 deletions algorithms/geometry/point-in-polygon.cpp
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;
}
Loading

0 comments on commit 87c817e

Please sign in to comment.