Skip to content

Commit

Permalink
Setting up for scanline
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Nov 8, 2023
1 parent e15239e commit 966b911
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,55 @@ struct point {
}
};

typedef std::pair<point, point> segment;

struct scan_transition {
double y;
size_t segment;

scan_transition(double y_, size_t segment_)
: y(y_), segment(segment_) {
}

bool operator<(scan_transition const &s) const {
if (y < s.y) {
return true;
} else {
return false;
}
}
};

std::vector<segment> snap_round(std::vector<segment> segs) {
bool again = true;

while (again) {
again = false;

// set up for a scanline traversal of the segments
// to find the pairs that intersect
// while not looking at pairs that can't possibly intersect

std::vector<scan_transition> tops;
std::vector<scan_transition> bottoms;

for (size_t i = 0; i < segs.size(); i++) {
if (segs[i].first.y < segs[i].second.y) {
tops.emplace_back(segs[i].first.y, i);
bottoms.emplace_back(segs[i].second.y, i);
} else {
tops.emplace_back(segs[i].second.y, i);
bottoms.emplace_back(segs[i].first.y, i);
}
}

std::sort(tops.begin(), tops.end());
std::sort(bottoms.begin(), bottoms.end());
}

return segs;
}

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

Expand Down Expand Up @@ -40,6 +89,8 @@ drawvec clean_polygon(drawvec const &geom, int z, int detail) {

// snap-round intersecting segments

segments = snap_round(segments);

// remove duplicate segments with opposite windings

// reassemble segments into rings
Expand Down

0 comments on commit 966b911

Please sign in to comment.