@@ -100,13 +100,12 @@ inline std::pair<double, double> circumcenter(
100
100
101
101
struct compare {
102
102
103
+ std::vector<double > const & dists;
103
104
std::vector<double > const & coords;
104
- double cx;
105
- double cy;
106
105
107
106
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] ;
110
109
const double diff1 = d1 - d2;
111
110
const double diff2 = coords[2 * i] - coords[2 * j];
112
111
const double diff3 = coords[2 * i + 1 ] - coords[2 * j + 1 ];
@@ -302,8 +301,16 @@ Delaunator::Delaunator(std::vector<double> const& in_coords)
302
301
303
302
std::tie (m_center_x, m_center_y) = circumcenter (i0x, i0y, i1x, i1y, i2x, i2y);
304
303
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
+
305
312
// 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 });
307
314
308
315
// initialize a hash table for storing edges of the advancing convex hull
309
316
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) {
508
515
hull_tri[e] = a;
509
516
break ;
510
517
}
511
- e = hull_next [e];
518
+ e = hull_prev [e];
512
519
} while (e != hull_start);
513
520
}
514
521
link (a, hbl);
0 commit comments