Skip to content

Commit edc036d

Browse files
committed
Split point.hpp
1 parent 76bb022 commit edc036d

File tree

6 files changed

+98
-82
lines changed

6 files changed

+98
-82
lines changed

cp-algo/geometry/all

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#ifndef CP_ALGO_GEOMETRY_ALL
22
#define CP_ALGO_GEOMETRY_ALL
33
#include "point.hpp"
4+
#include "convex_hull.hpp"
5+
#include "closest_pair.hpp"
46
#endif // CP_ALGO_GEOMETRY_ALL

cp-algo/geometry/closest_pair.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef CP_ALGO_GEOMETRY_CLOSEST_PAIR_HPP
2+
#define CP_ALGO_GEOMETRY_CLOSEST_PAIR_HPP
3+
#include "../random/rng.hpp"
4+
#include "point.hpp"
5+
#include <vector>
6+
#include <map>
7+
namespace cp_algo::geometry {
8+
// Rabin & Lipton
9+
template<typename ftype>
10+
auto closest_pair(std::vector<point_t<ftype>> const& r) {
11+
using point = point_t<ftype>;
12+
size_t n = size(r);
13+
int64_t md = 1e18;
14+
for(size_t i = 0; i < n / 100; i++) {
15+
auto A = random::rng() % n;
16+
auto B = random::rng() % n;
17+
if(A != B) {
18+
md = std::min(md, norm(r[A] - r[B]));
19+
if(md == 0) {
20+
return std::pair{A, B};
21+
}
22+
}
23+
}
24+
std::map<point, std::vector<int>> neigs;
25+
md = ceil(sqrtl(md));
26+
for(size_t i = 0; i < n; i++) {
27+
neigs[r[i] / md].push_back(i);
28+
}
29+
size_t a = 0, b = 1;
30+
md = norm(r[a] - r[b]);
31+
for(auto &[p, id]: neigs) {
32+
for(int dx: {-1, 0, 1}) {
33+
for(int dy: {-1, 0, 1}) {
34+
auto pp = p + point{dx, dy};
35+
if(!neigs.count(pp)) {
36+
continue;
37+
}
38+
for(size_t i: neigs[p + point{dx, dy}]) {
39+
for(size_t j: id) {
40+
if(j == i) {
41+
break;
42+
}
43+
int64_t cur = norm(r[i] - r[j]);
44+
if(cur < md) {
45+
md = cur;
46+
a = i;
47+
b = j;
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
return std::pair{a, b};
55+
}
56+
}
57+
#endif // CP_ALGO_GEOMETRY_CLOSEST_PAIR_HPP

cp-algo/geometry/convex_hull.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef CP_ALGO_GEOMETRY_CONVEX_HULL_HPP
2+
#define CP_ALGO_GEOMETRY_CONVEX_HULL_HPP
3+
#include "point.hpp"
4+
#include <algorithm>
5+
#include <utility>
6+
#include <vector>
7+
#include <ranges>
8+
namespace cp_algo::geometry {
9+
template<typename ftype>
10+
std::vector<point_t<ftype>> convex_hull(std::vector<point_t<ftype>> r) {
11+
using point = point_t<ftype>;
12+
std::ranges::sort(r);
13+
if(size(r) <= 1 || r[0] == r.back()) {
14+
return empty(r) ? r : std::vector{r[0]};
15+
}
16+
std::vector<point> hull = {r[0]};
17+
for(int half: {0, 1}) {
18+
size_t base = size(hull);
19+
for(auto it: std::views::drop(r, 1)) {
20+
while(size(hull) >= base + 1) {
21+
point a = hull.back();
22+
if(point::ccw(it - a, *(end(hull) - 2) - a)) {
23+
break;
24+
} else {
25+
hull.pop_back();
26+
}
27+
}
28+
hull.push_back(it);
29+
}
30+
std::ranges::reverse(r);
31+
std::ignore = half;
32+
}
33+
hull.pop_back();
34+
return hull;
35+
}
36+
}
37+
#endif // CP_ALGO_GEOMETRY_CONVEX_HULL_HPP

cp-algo/geometry/point.hpp

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
#ifndef CP_ALGO_GEOMETRY_POINT_HPP
22
#define CP_ALGO_GEOMETRY_POINT_HPP
33
#include "../random/rng.hpp"
4-
#include <algorithm>
54
#include <iostream>
65
#include <complex>
7-
#include <utility>
8-
#include <vector>
9-
#include <ranges>
10-
#include <map>
116
namespace cp_algo::geometry {
127
template<typename ftype>
138
struct point_t: public std::complex<ftype> {
@@ -49,80 +44,5 @@ namespace cp_algo::geometry {
4944
std::cout << x() << ' ' << y() << "\n";
5045
}
5146
};
52-
53-
template<typename point>
54-
std::vector<point> convex_hull(std::vector<point> r) {
55-
std::ranges::sort(r);
56-
if(size(r) <= 1 || r[0] == r.back()) {
57-
return empty(r) ? r : std::vector{r[0]};
58-
}
59-
std::vector<point> hull = {r[0]};
60-
for(int half: {0, 1}) {
61-
size_t base = size(hull);
62-
for(auto it: std::views::drop(r, 1)) {
63-
while(size(hull) >= base + 1) {
64-
point a = hull.back();
65-
if(point::ccw(it - a, *(end(hull) - 2) - a)) {
66-
break;
67-
} else {
68-
hull.pop_back();
69-
}
70-
}
71-
hull.push_back(it);
72-
}
73-
std::ranges::reverse(r);
74-
std::ignore = half;
75-
}
76-
hull.pop_back();
77-
return hull;
78-
}
79-
80-
// Rabin & Lipton
81-
template<typename point>
82-
auto closest_pair(std::vector<point> const& r) {
83-
size_t n = size(r);
84-
int64_t md = 1e18;
85-
for(size_t i = 0; i < n; i++) {
86-
auto A = random::rng() % n;
87-
auto B = random::rng() % n;
88-
if(A != B) {
89-
md = std::min(md, norm(r[A] - r[B]));
90-
if(md == 0) {
91-
return std::pair{A, B};
92-
}
93-
}
94-
}
95-
std::map<point, std::vector<int>> neigs;
96-
md = ceil(sqrtl(md));
97-
for(size_t i = 0; i < n; i++) {
98-
neigs[r[i] / md].push_back(i);
99-
}
100-
size_t a = 0, b = 1;
101-
md = norm(r[a] - r[b]);
102-
for(auto &[p, id]: neigs) {
103-
for(int dx: {-1, 0, 1}) {
104-
for(int dy: {-1, 0, 1}) {
105-
auto pp = p + point{dx, dy};
106-
if(!neigs.count(pp)) {
107-
continue;
108-
}
109-
for(size_t i: neigs[p + point{dx, dy}]) {
110-
for(size_t j: id) {
111-
if(j == i) {
112-
break;
113-
}
114-
int64_t cur = norm(r[i] - r[j]);
115-
if(cur < md) {
116-
md = cur;
117-
a = i;
118-
b = j;
119-
}
120-
}
121-
}
122-
}
123-
}
124-
}
125-
return std::pair{a, b};
126-
}
12747
}
12848
#endif // CP_ALGO_GEOMETRY_POINT_HPP

verify/geometry/closest_pair.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @brief Closest Pair of Points
22
#define PROBLEM "https://judge.yosupo.jp/problem/closest_pair"
33
#pragma GCC optimize("Ofast,unroll-loops")
4-
#include "cp-algo/geometry/point.hpp"
4+
#include "cp-algo/geometry/closest_pair.hpp"
55
#include <bits/stdc++.h>
66

77
using namespace std;

verify/geometry/hull.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define PROBLEM "https://judge.yosupo.jp/problem/static_convex_hull"
33
#pragma GCC optimize("Ofast,unroll-loops")
44
#pragma GCC target("tune=native")
5-
#include "cp-algo/geometry/point.hpp"
5+
#include "cp-algo/geometry/convex_hull.hpp"
66
#include <bits/stdc++.h>
77

88
using namespace std;

0 commit comments

Comments
 (0)