+/* segments ending in the current scanline need xrow treatment like everything else.
+ (consider an intersection taking place just above a nearly horizontal segment
+ ending on the current scanline- the intersection would snap down *below* the
+ ending segment if we don't add the intersection point to the latter right away)
+ we need to treat ending segments seperately, however. we have to delete them from
+ the active list right away to make room for intersect operations (which might
+ still be in the current scanline- consider two 45° polygons and a vertical polygon
+ intersecting on an integer coordinate). but once they're no longer in the active list,
+ we can't use the add_points_to_*_sloped_segments() functions anymore, and re-adding
+ them to the active list just for point snapping would be overkill.
+ (One other option to consider, however, would be to create a new active list only
+ for ending segments)
+*/
+static void add_points_to_ending_segments(status_t*status, int32_t y)
+{
+ segment_t*seg = status->ending_segments;
+ while(seg) {
+ segment_t*next = seg->right;seg->right=0;
+
+ assert(seg->b.y == status->y);
+
+ if(status->xrow->num == 1) {
+ // shortcut
+ assert(seg->b.x == status->xrow->x[0]);
+ point_t p = {status->xrow->x[0], y};
+ insert_point_into_segment(status, seg, p);
+ } else {
+ int t;
+ int start=0,end=status->xrow->num,dir=1;
+ if(seg->delta.x < 0) {
+ start = status->xrow->num-1;
+ end = dir = -1;
+ }
+ for(t=start;t!=end;t+=dir) {
+ box_t box = box_new(status->xrow->x[t], y);
+ double d0 = LINE_EQ(box.left1, seg);
+ double d1 = LINE_EQ(box.left2, seg);
+ double d2 = LINE_EQ(box.right1, seg);
+ double d3 = LINE_EQ(box.right2, seg);
+ if(!(d0>=0 && d1>=0 && d2>=0 && d3>0 ||
+ d0<=0 && d1<=0 && d2<=0 && d3<0)) {
+ insert_point_into_segment(status, seg, box.right2);
+ break;
+ }
+ }
+
+#ifdef CHECKS
+ /* we *need* to find a point to insert. the segment's own end point
+ is in that list, for Pete's sake. */
+ assert(t!=end);
+#endif
+ }
+ // now that this is done, too, we can also finally free this segment
+ segment_destroy(seg);
+ seg = next;
+ }
+ status->ending_segments = 0;
+}
+
+static void recalculate_windings(status_t*status, segrange_t*range)
+{
+#ifdef DEBUG
+ fprintf(stderr, "range: [%d]..[%d]\n", SEGNR(range->segmin), SEGNR(range->segmax));
+#endif
+ segrange_adjust_endpoints(range, status->y);
+
+ segment_t*s = range->segmin;
+ segment_t*end = range->segmax;
+ segment_t*last = 0;
+
+#ifdef DEBUG
+ s = actlist_leftmost(status->actlist);
+ while(s) {
+ fprintf(stderr, "[%d]%d%s ", s->nr, s->changed,
+ s == range->segmin?"S":(
+ s == range->segmax?"E":""));
+ s = s->right;
+ }
+ fprintf(stderr, "\n");
+ s = range->segmin;
+#endif
+#ifdef CHECKS
+ /* test sanity: check that we don't have changed segments
+ outside of the given range */
+ s = actlist_leftmost(status->actlist);
+ while(s && s!=range->segmin) {
+ assert(!s->changed);
+ s = s->right;
+ }
+ s = actlist_rightmost(status->actlist);
+ while(s && s!=range->segmax) {
+ assert(!s->changed);
+ s = s->left;
+ }
+ /* in check mode, go through the whole interval so we can test
+ that all polygons where the fillstyle changed also have seg->changed=1 */
+ s = actlist_leftmost(status->actlist);
+ end = 0;
+#endif
+
+ if(end)
+ end = end->right;
+ while(s!=end) {
+#ifndef CHECKS
+ if(s->changed)
+#endif
+ {
+ segment_t* left = actlist_left(status->actlist, s);
+ windstate_t wind = left?left->wind:status->windrule->start(status->context);
+ s->wind = status->windrule->add(status->context, wind, s->fs, s->dir, s->polygon_nr);
+ fillstyle_t*fs_old = s->fs_out;
+ s->fs_out = status->windrule->diff(&wind, &s->wind);
+
+#ifdef DEBUG
+ fprintf(stderr, "[%d] dir=%s wind=%d wind.filled=%s fs_old/new=%s/%s %s\n", s->nr, s->dir==DIR_UP?"up":"down", s->wind.wind_nr, s->wind.is_filled?"fill":"nofill",
+ fs_old?"draw":"omit", s->fs_out?"draw":"omit",
+ fs_old!=s->fs_out?"CHANGED":"");
+#endif
+ assert(!(!s->changed && fs_old!=s->fs_out));
+ s->changed = 0;
+
+#ifdef CHECKS
+ s->fs_out_ok = 1;
+#endif
+ }
+ s = s->right;
+ }
+}
+
+/* we need to handle horizontal lines in order to add points to segments
+ we otherwise would miss during the windrule re-evaluation */
+static void intersect_with_horizontal(status_t*status, segment_t*h)
+{
+ segment_t* left = actlist_find(status->actlist, h->a, h->a);
+ segment_t* right = actlist_find(status->actlist, h->b, h->b);
+
+ /* not strictly necessary, also done by the event */
+ xrow_add(status->xrow, h->a.x);
+ point_t o = h->a;
+
+ if(!right) {
+ assert(!left);
+ return;
+ }
+
+ left = actlist_right(status->actlist, left); //first seg to the right of h->a
+ right = right->right; //first seg to the right of h->b
+ segment_t* s = left;
+
+ while(s!=right) {
+ assert(s);
+ int32_t x = XPOS_INT(s, status->y);
+#ifdef DEBUG
+ fprintf(stderr, "...into [%d] (%d,%d) -> (%d,%d) at (%d,%d)\n", s->nr,
+ s->a.x, s->a.y,
+ s->b.x, s->b.y,
+ x, status->y
+ );
+#endif
+ assert(x >= h->a.x);
+ assert(x <= h->b.x);
+ assert(s->delta.x > 0 && x >= s->a.x || s->delta.x <= 0 && x <= s->a.x);
+ assert(s->delta.x > 0 && x <= s->b.x || s->delta.x <= 0 && x >= s->b.x);
+ xrow_add(status->xrow, x);
+
+ s = s->right;
+ }
+}
+
+static void event_apply(status_t*status, event_t*e)