Skip to content

Commit

Permalink
Committing to integer coordinates to fix more accidental mirror segs
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Nov 10, 2023
1 parent e849d42 commit 10290b1
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bool fix_opposites(std::vector<segment> &segs) {
opposites.emplace(opposite, i);
}

size_t found = 0;
for (size_t i = 0; i < segs.size(); i++) {
if (segs[i] == erased) {
continue;
Expand All @@ -55,6 +56,8 @@ bool fix_opposites(std::vector<segment> &segs) {
continue;
}

found++;

double dx = std::round(segs[i].second.x) - std::round(segs[i].first.x);
double dy = std::round(segs[i].second.y) - std::round(segs[i].first.y);
double dsq = dx * dx + dy * dy;
Expand Down Expand Up @@ -82,6 +85,11 @@ bool fix_opposites(std::vector<segment> &segs) {
}
}

printf("found %zu\n", found);
if (found > 0) {
changed = true;
}

size_t out = 0;
for (size_t i = 0; i < segs.size(); i++) {
if (segs[i] != erased) {
Expand Down Expand Up @@ -321,7 +329,6 @@ void snap_round(std::vector<segment> &segs) {

if (fix_opposites(segs)) {
again = true;
continue;
}

// set up for a scanline traversal of the segments
Expand Down Expand Up @@ -442,6 +449,39 @@ drawvec reassemble(std::vector<segment> const &segs) {
// lead around either the largest outer ring or
// the smallest inner ring that includes this point.

auto best = options.first;
double bestang = -500;

#if 0
for (; options.first != options.second; ++options.first) {
double ang1 = atan2(here.second.y - here.first.y, here.second.x - here.first.x);
// the vector for ang2 is backwards, so a complete reversal would be a difference of 0
double ang2 = atan2(options.first->second.first.y - options.first->second.second.y,
options.first->second.first.x - options.first->second.second.x);
double diff = ang1 - ang2;
if (diff < 0) {
diff += 2 * M_PI;
}

printf("%f,%f to %f,%f to %f,%f, %f,%f: %f\n",
here.first.x, here.first.y,
here.second.x, here.second.y,
options.first->second.first.x, options.first->second.first.y,
options.first->second.second.x, options.first->second.second.y,
diff * 180 / M_PI);

if (diff > bestang) {
bestang = diff;
best = options.first;
}
}
printf("\n");

here = best->second;
examined.emplace(here.first, here);
examining.erase(best);
#endif

// XXX just arbitrarily choosing the first for the moment

here = options.first->second;
Expand Down Expand Up @@ -525,8 +565,8 @@ drawvec clean_polygon(drawvec const &geom, int z, int detail) {

for (size_t k = i; k + 1 < j; k++) {
std::pair<point, point> seg = std::make_pair(
point(geom[k].x / scale, geom[k].y / scale),
point(geom[k + 1].x / scale, geom[k + 1].y / scale));
point(std::round(geom[k].x / scale), std::round(geom[k].y / scale)),
point(std::round(geom[k + 1].x / scale), std::round(geom[k + 1].y / scale)));

if (std::round(seg.first.x) != std::round(seg.second.x) ||
std::round(seg.first.y) != std::round(seg.second.y)) {
Expand Down

0 comments on commit 10290b1

Please sign in to comment.