Skip to content

Backport from delaunator-js #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions include/delaunator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ inline std::pair<double, double> circumcenter(

struct compare {

std::vector<double> const& dists;
std::vector<double> 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];
Expand Down Expand Up @@ -302,8 +301,16 @@ Delaunator::Delaunator(std::vector<double> const& in_coords)

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

std::vector<double> 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::size_t>(std::llround(std::ceil(std::sqrt(n))));
Expand Down Expand Up @@ -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);
Expand Down