diff --git a/include/delaunator.hpp b/include/delaunator.hpp index 6bc5221..3287d02 100644 --- a/include/delaunator.hpp +++ b/include/delaunator.hpp @@ -100,13 +100,12 @@ inline std::pair circumcenter( struct compare { + std::vector const& dists; std::vector const& coords; - double cx; - double cy; bool operator()(std::size_t i, std::size_t j) { - const double d1 = dist(coords[2 * i], coords[2 * i + 1], cx, cy); - const double d2 = dist(coords[2 * j], coords[2 * j + 1], cx, cy); + const double d1 = dists[i]; + const double d2 = dists[j]; const double diff1 = d1 - d2; const double diff2 = coords[2 * i] - coords[2 * j]; const double diff3 = coords[2 * i + 1] - coords[2 * j + 1]; @@ -302,8 +301,16 @@ Delaunator::Delaunator(std::vector const& in_coords) std::tie(m_center_x, m_center_y) = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y); + std::vector dists; + dists.reserve(ids.size()); + + for (auto&& id : ids) { + const double d = dist(coords[2 * id], coords[2 * id + 1], m_center_x, m_center_y); + dists.push_back(d); + } + // sort the points by distance from the seed triangle circumcenter - std::sort(ids.begin(), ids.end(), compare{ coords, m_center_x, m_center_y }); + std::sort(ids.begin(), ids.end(), compare{ dists, coords }); // initialize a hash table for storing edges of the advancing convex hull m_hash_size = static_cast(std::llround(std::ceil(std::sqrt(n)))); @@ -508,7 +515,7 @@ std::size_t Delaunator::legalize(std::size_t a) { hull_tri[e] = a; break; } - e = hull_next[e]; + e = hull_prev[e]; } while (e != hull_start); } link(a, hbl);