@@ -34,7 +34,8 @@ struct point {
34
34
35
35
typedef std::pair<point, point> segment;
36
36
37
- void fix_opposites (std::vector<segment> &segs) {
37
+ bool fix_opposites (std::vector<segment> &segs) {
38
+ bool changed = false ;
38
39
std::multimap<segment, size_t > opposites;
39
40
segment erased = std::make_pair (point (INT_MAX, INT_MAX), point (INT_MAX, INT_MAX));
40
41
@@ -54,8 +55,28 @@ void fix_opposites(std::vector<segment> &segs) {
54
55
continue ;
55
56
}
56
57
57
- segs[i] = erased;
58
- segs[f.first ->second ] = erased;
58
+ double dx = std::round (segs[i].second .x ) - std::round (segs[i].first .x );
59
+ double dy = std::round (segs[i].second .y ) - std::round (segs[i].first .y );
60
+ double dsq = dx * dx + dy * dy;
61
+ if (dsq >= 5 * 5 ) {
62
+ // alter the segments instead to keep it from collapsing away
63
+
64
+ double ang = atan2 (dy, dx) + M_PI / 2 ;
65
+ double cx = std::round ((std::round (segs[i].second .x ) + std::round (segs[i].first .x )) / 2 + sqrt (2 ) / 2 * cos (ang));
66
+ double cy = std::round ((std::round (segs[i].second .y ) + std::round (segs[i].first .y )) / 2 + sqrt (2 ) / 2 * sin (ang));
67
+
68
+ segs.emplace_back (point (cx, cy), segs[i].second );
69
+ segs[i] = std::make_pair (segs[i].first , point (cx, cy));
70
+ changed = true ;
71
+
72
+ // segs[i] is not erased, so segs[f.first->second]
73
+ // will still match against it and will be bowed out
74
+ // in the opposite direction.
75
+ } else {
76
+ segs[i] = erased;
77
+ segs[f.first ->second ] = erased;
78
+ }
79
+
59
80
opposites.erase (f.first );
60
81
break ;
61
82
}
@@ -68,6 +89,7 @@ void fix_opposites(std::vector<segment> &segs) {
68
89
}
69
90
}
70
91
segs.resize (out);
92
+ return changed;
71
93
}
72
94
73
95
const std::pair<double , double > SAME_SLOPE = std::make_pair(-INT_MAX, INT_MAX);
@@ -297,7 +319,10 @@ void snap_round(std::vector<segment> &segs) {
297
319
// in the course of trying to keep spindles alive, and will then need to
298
320
// resolve those.
299
321
300
- fix_opposites (segs);
322
+ if (fix_opposites (segs)) {
323
+ again = true ;
324
+ continue ;
325
+ }
301
326
302
327
// set up for a scanline traversal of the segments
303
328
// to find the pairs that intersect
0 commit comments