Skip to content

Commit f55ced6

Browse files
Backport from delaunator-js for
mapbox/delaunator#44 mapbox/delaunator#33 solves delfrrr#11 delfrrr#16
1 parent c1521f6 commit f55ced6

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

include/delaunator.hpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,12 @@ inline std::pair<double, double> circumcenter(
100100

101101
struct compare {
102102

103+
std::vector<double> const& dists;
103104
std::vector<double> const& coords;
104-
double cx;
105-
double cy;
106105

107106
bool operator()(std::size_t i, std::size_t j) {
108-
const double d1 = dist(coords[2 * i], coords[2 * i + 1], cx, cy);
109-
const double d2 = dist(coords[2 * j], coords[2 * j + 1], cx, cy);
107+
const double d1 = dists[i];
108+
const double d2 = dists[j];
110109
const double diff1 = d1 - d2;
111110
const double diff2 = coords[2 * i] - coords[2 * j];
112111
const double diff3 = coords[2 * i + 1] - coords[2 * j + 1];
@@ -302,8 +301,16 @@ Delaunator::Delaunator(std::vector<double> const& in_coords)
302301

303302
std::tie(m_center_x, m_center_y) = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
304303

304+
std::vector<double> dists;
305+
dists.reserve(ids.size());
306+
307+
for (auto&& id : ids) {
308+
const double d = dist(coords[2 * id], coords[2 * id + 1], m_center_x, m_center_y);
309+
dists.push_back(d);
310+
}
311+
305312
// sort the points by distance from the seed triangle circumcenter
306-
std::sort(ids.begin(), ids.end(), compare{ coords, m_center_x, m_center_y });
313+
std::sort(ids.begin(), ids.end(), compare{ dists, coords });
307314

308315
// initialize a hash table for storing edges of the advancing convex hull
309316
m_hash_size = static_cast<std::size_t>(std::llround(std::ceil(std::sqrt(n))));
@@ -508,7 +515,7 @@ std::size_t Delaunator::legalize(std::size_t a) {
508515
hull_tri[e] = a;
509516
break;
510517
}
511-
e = hull_next[e];
518+
e = hull_prev[e];
512519
} while (e != hull_start);
513520
}
514521
link(a, hbl);

0 commit comments

Comments
 (0)