Skip to content

Commit

Permalink
Remove collinear points
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Nov 13, 2023
1 parent bd24168 commit 546b5fd
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,44 @@ void flatten_rings(std::vector<ring_area> &rings, size_t i, drawvec &out, ssize_
}
}

bool same_slope(draw d1, draw d2, draw d3) {
long long dx12 = d2.x - d1.x;
long long dy12 = d2.y - d1.y;

long long dx23 = d3.x - d2.x;
long long dy23 = d3.y - d2.y;

if (dx12 == 0) {
if (dx12 == dx23) {
return true;
} else {
return false;
}
}

if (dy12 / (double) dx12 == dy23 / (double) dx23) {
return true;
} else {
return false;
}
}

drawvec remove_collinear(drawvec const &geom) {
drawvec out;

for (size_t i = 0; i < geom.size(); i++) {
if (i > 0 && i + 1 < geom.size() &&
geom[i].op == VT_LINETO && geom[i + 1].op == VT_LINETO &&
same_slope(out.back(), geom[i], geom[i + 1])) {
continue;
}

out.push_back(geom[i]);
}

return out;
}

drawvec clean_polygon(drawvec const &geom, int z, int detail) {
double scale = 1LL << (32 - detail - z);

Expand Down Expand Up @@ -812,7 +850,9 @@ drawvec clean_polygon(drawvec const &geom, int z, int detail) {
}
#endif

// remove collinear points?
// remove collinear points

ret = remove_collinear(ret);

#if 0
drawvec ret;
Expand Down

0 comments on commit 546b5fd

Please sign in to comment.