Skip to content

Commit 0d04923

Browse files
committed
Cache distance from center point.
1 parent f49c473 commit 0d04923

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

include/delaunator.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,34 @@ inline std::pair<double, double> circumcenter(
9393
return std::make_pair(x, y);
9494
}
9595

96+
9697
struct compare {
9798

9899
std::vector<double> const& coords;
99-
double cx;
100-
double cy;
100+
std::vector<double> dists;
101+
102+
compare(std::vector<double> const& coords,
103+
double center_x, double center_y) : coords(coords)
104+
{
105+
size_t n = coords.size() / 2;
106+
dists.reserve(n);
107+
double const *xcoord = coords.data();
108+
double const *ycoord = coords.data() + 1;
109+
while (n--)
110+
{
111+
dists.push_back(dist(*xcoord, *ycoord, center_x, center_y));
112+
xcoord += 2;
113+
ycoord += 2;
114+
}
115+
}
101116

102-
bool operator()(std::size_t i, std::size_t j) {
103-
const double d1 = dist(coords[2 * i], coords[2 * i + 1], cx, cy);
104-
const double d2 = dist(coords[2 * j], coords[2 * j + 1], cx, cy);
105-
const double diff1 = d1 - d2;
117+
bool operator()(std::size_t i, std::size_t j)
118+
{
119+
const double diff1 = dists[i] - dists[j];
106120
const double diff2 = coords[2 * i] - coords[2 * j];
107121
const double diff3 = coords[2 * i + 1] - coords[2 * j + 1];
108122

123+
//ABELL - Not sure why we're not just checking != 0 here.
109124
if (diff1 > 0.0 || diff1 < 0.0) {
110125
return diff1 < 0;
111126
} else if (diff2 > 0.0 || diff2 < 0.0) {
@@ -116,6 +131,7 @@ struct compare {
116131
}
117132
};
118133

134+
119135
inline bool in_circle(
120136
const double ax,
121137
const double ay,

0 commit comments

Comments
 (0)