Skip to content

Commit a380769

Browse files
mrgreywatermourner
authored andcommitted
fix #54
1 parent 5da1cb5 commit a380769

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

include/mapbox/earcut.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Earcut {
8484
bool hashing;
8585
double minX, maxX;
8686
double minY, maxY;
87-
double size;
87+
double inv_size = 0;
8888

8989
template <typename T, typename Alloc = std::allocator<T>>
9090
class ObjectPool {
@@ -135,7 +135,6 @@ void Earcut<N>::operator()(const Polygon& points) {
135135

136136
double x;
137137
double y;
138-
size = 0;
139138
int threshold = 80;
140139
std::size_t len = 0;
141140

@@ -170,7 +169,8 @@ void Earcut<N>::operator()(const Polygon& points) {
170169
} while (p != outerNode);
171170

172171
// minX, minY and size are later used to transform coords into integers for z-order calculation
173-
size = std::max<double>(maxX - minX, maxY - minY);
172+
inv_size = std::max<double>(maxX - minX, maxY - minY);
173+
inv_size = inv_size != .0 ? (1. / inv_size) : .0;
174174
}
175175

176176
earcutLinked(outerNode);
@@ -231,7 +231,7 @@ Earcut<N>::filterPoints(Node* start, Node* end) {
231231
removeNode(p);
232232
p = end = p->prev;
233233

234-
if (p == p->next) return nullptr;
234+
if (p == p->next) break;
235235
again = true;
236236

237237
} else {
@@ -603,8 +603,8 @@ Earcut<N>::sortLinked(Node* list) {
603603
template <typename N>
604604
int32_t Earcut<N>::zOrder(const double x_, const double y_) {
605605
// coords are transformed into non-negative 15-bit integer range
606-
int32_t x = static_cast<int32_t>(32767.0 * (x_ - minX) / size);
607-
int32_t y = static_cast<int32_t>(32767.0 * (y_ - minY) / size);
606+
int32_t x = static_cast<int32_t>(32767.0 * (x_ - minX) * inv_size);
607+
int32_t y = static_cast<int32_t>(32767.0 * (y_ - minY) * inv_size);
608608

609609
x = (x | (x << 8)) & 0x00FF00FF;
610610
x = (x | (x << 4)) & 0x0F0F0F0F;

test/fixtures/issue83.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
#include "geometries.hpp"
3+
4+
namespace mapbox {
5+
namespace fixtures {
6+
7+
static const Fixture<double> issue83("issue83", 0, 1e-14, Infinity, {
8+
{{0,0},{4000,0},{4000,4000},{0,4000}},
9+
{{0,0},{4000,0},{4000,4000},{0,4000}},
10+
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
11+
});
12+
13+
}
14+
}

0 commit comments

Comments
 (0)