Skip to content

Commit e9d18c8

Browse files
author
atcoder-live
committed
uf + geom
1 parent 9997e88 commit e9d18c8

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ AtCoder解説放送ライブラリ集
1515
|名前|コード|説明|
1616
|:--|:--|:--|
1717
|BIT|[bit.cpp](bit.cpp)|Binary Indexed Tree (Fenwick Tree)|
18+
|UnionFind|[uf.cpp](uf.cpp)|Union Find|
1819

1920
### 数学
2021
|名前|コード|説明|
@@ -35,4 +36,6 @@ AtCoder解説放送ライブラリ集
3536
### 幾何
3637
|名前|コード|説明|
3738
|:--|:--|:--|
38-
|Vector|[vector.cpp](vector.cpp)|ベクトル(点を扱う際にも使う)|
39+
|基本|[geom.cpp](geom.cpp)|幾何のベース+目次|
40+
|Vector|[geom/vector.cpp](geom/vector.cpp)|ベクトル(点を扱う際にも使う)|
41+
|Circle|[geom/circle.cpp](geom/circle.cpp)||

geom.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Geometry
2+
const double eps = 1e-9;
3+
bool equal(double a, double b) { return abs(a-b) < eps;}
4+
5+
[geom/vector]
6+
[geom/circle]

geom/circle.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Circle
2+
// coding: https://youtu.be/TdR816rqc3s?t=1750
3+
// comment: https://youtu.be/TdR816rqc3s?t=11609
4+
struct Circle {
5+
V o; double r;
6+
Circle(V o=V(), double r=0) : o(o), r(r) {}
7+
vector<V> crossPoint(const Circle& c) {
8+
V v = c.o-o;
9+
double l = v.norm();
10+
if (equal(l, 0)) return {};
11+
if (equal(l+r+c.r, max({l,r,c.r})*2)) {
12+
if (equal(l+r,c.r)) return {o - v*(r/l)};
13+
return {o + v*(r/l)};
14+
}
15+
if (l+r+c.r < max({l,r,c.r})*2) return {};
16+
double x = (l*l + r*r - c.r*c.r) / (2*l);
17+
double y = sqrt(r*r - x*x);
18+
V mid = o + v*(x/l);
19+
v = v.rotate90();
20+
return {mid + v*(y/l), mid - v*(y/l)};
21+
}
22+
bool isInside(const V& p) const {
23+
return (p-o).norm() < r+eps;
24+
}
25+
};

vector.cpp renamed to geom/vector.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// vector
1+
// Vector
22
// https://youtu.be/UWbGRhF3Ozw?t=9564
3-
const double eps = 1e-9;
43
struct V {
54
double x, y;
65
V(double x=0, double y=0): x(x), y(y) {}
@@ -16,7 +15,7 @@ struct V {
1615
double cross(const V& v) const { return x*v.y - v.x*y;}
1716
double norm2() const { return x*x + y*y;}
1817
double norm() const { return sqrt(norm2());}
19-
18+
V rotate90() const { return V(y, -x);}
2019
int ort() const { // orthant
2120
if (abs(x) < eps && abs(y) < eps) return 0;
2221
if (y > 0) return x>0 ? 1 : 2;

uf.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// UnionFind
2+
// coding: https://youtu.be/TdR816rqc3s?t=726
3+
// comment: https://youtu.be/TdR816rqc3s?t=6822
4+
struct UnionFind {
5+
vector<int> d;
6+
UnionFind(int n=0): d(n,-1) {}
7+
int find(int x) {
8+
if (d[x] < 0) return x;
9+
return d[x] = find(d[x]);
10+
}
11+
bool unite(int x, int y) {
12+
x = find(x); y = find(y);
13+
if (x == y) return false;
14+
if (d[x] > d[y]) swap(x,y);
15+
d[x] += d[y];
16+
d[y] = x;
17+
return true;
18+
}
19+
bool same(int x, int y) { return find(x) == find(y);}
20+
int size(int x) { return -d[find(x)];}
21+
};

0 commit comments

Comments
 (0)