-void SketcherCurve::draw_smooth(VFrame *out)
-{
- VFrame *vpen = new_vpen(out);
- out->set_pixel_color(color);
- int n = points.size();
- if( !n ) return;
- if( n > 2 ) {
- SketcherPoint *pt0 = points[0], *pt1 = points[1], *pt2 = points[2];
- float bx = pt0->x, by = pt0->y;
- float cx = pt1->x, cy = pt1->y;
- float dx = pt2->x, dy = pt2->y;
- float xc = (bx+dx)/2.f, yc = (by+dy)/2.f;
- float xd = cx - xc, yd = cy - yc;
- float ax = xc - xd, ay = yc - yd;
- float sx, sy;
- for( int pi=0,n2=n-2; pi<n2; ++pi ) {
- float dx = points[pi+2]->x, dy = points[pi+2]->y;
- if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
- smooth_sxy(ax,ay, bx,by, cx,cy, dx,dy, sx, sy);
- vpen->draw_smooth(bx,by, sx,sy, cx,cy);
+class FillRegion
+{
+ class segment { public: int y, lt, rt; };
+ ArrayList<segment> stack;
+
+ void push(int y, int lt, int rt) {
+ segment &seg = stack.append();
+ seg.y = y; seg.lt = lt; seg.rt = rt;
+ }
+ void pop(int &y, int <, int &rt) {
+ segment &seg = stack.last();
+ y = seg.y; lt = seg.lt; rt = seg.rt;
+ stack.remove();
+ }
+
+ VFrame *img;
+ uint8_t *msk;
+ int w, h, nxt;
+ SketcherPoints &points;
+public:
+ SketcherPoint *next();
+ bool exists() { return stack.size() > 0; }
+ void start_at(int x, int y);
+ void run();
+ FillRegion(SketcherPoints &pts, SketcherVPen *vpen);
+ ~FillRegion();
+};
+
+FillRegion::FillRegion(SketcherPoints &pts, SketcherVPen *vpen)
+ : points(pts)
+{
+ this->img = vpen->vfrm;
+ this->msk = vpen->msk;
+ this->w = img->get_w();
+ this->h = img->get_h();
+ nxt = 0;
+}
+FillRegion::~FillRegion()
+{
+}
+
+void FillRegion::start_at(int x, int y)
+{
+ bclamp(x, 0, w-1);
+ bclamp(y, 0, h-1);
+ push(y, x, x);
+}
+
+void FillRegion::run()
+{
+ while( stack.size() > 0 ) {
+ int y, ilt, irt;
+ pop(y, ilt, irt);
+ int ofs = y*w + ilt;
+ for( int x=ilt; x<=irt; ++x,++ofs ) {
+ if( msk[ofs] ) continue;
+ msk[ofs] = 0xff;
+ img->draw_pixel(x, y);
+ int lt = x, rt = x;
+ int lofs = ofs;
+ for( int i=lt; --i>=0; ) {
+ if( msk[--lofs] ) break;
+ img->draw_pixel(i, y);
+ msk[lofs] = 0xff; lt = i;
+ }
+ int rofs = ofs;
+ for( int i=rt; ++i< w; ) {
+ if( msk[++rofs] ) break;
+ img->draw_pixel(i, y);
+ msk[rofs] = 0xff; rt = i;
+ }
+ if( y+1 < h ) push(y+1, lt, rt);
+ if( y-1 >= 0 ) push(y-1, lt, rt);
+ }
+ }
+}
+
+SketcherPoint *FillRegion::next()
+{
+ while( nxt < points.size() ) {
+ SketcherPoint *pt = points[nxt++];
+ if( pt->arc == ARC_OFF ) continue;
+ if( pt->arc != ARC_FILL ) return pt;
+ start_at(pt->x, pt->y);
+ }
+ return 0;
+}
+
+
+void SketcherCurve::draw(VFrame *img)
+{
+ if( !points.size() ) return;
+ const float fmx = 16383;
+ SketcherVPen *vpen = new_vpen(img);
+ FillRegion fill(points, vpen);
+ SketcherPoint *pnt0 = fill.next();
+ SketcherPoint *pnt1 = pnt0 ? fill.next() : 0;
+ SketcherPoint *pnt2 = pnt1 ? fill.next() : 0;
+ if( pnt0 && pnt1 && pnt2 ) {
+ SketcherPoint *pt0 = pnt0, *pt1 = pnt1, *pt2 = pnt2;
+ float ax,ay, bx,by, cx,cy, dx,dy, sx,sy;
+ bx = pt0->x; by = pt0->y;
+ cx = pt1->x; cy = pt1->y;
+ dx = pt2->x; dy = pt2->y;
+ smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
+ while( pt2 ) {
+ dx = pt2->x; dy = pt2->y;
+ switch( pt0->arc ) {
+ case ARC_CURVE:
+ if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
+ // s = ac thru b x bd thru c
+ intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
+ vpen->draw_smooth(bx,by, sx,sy, cx,cy);
+ break;
+ } // fall thru
+ case ARC_LINE:
+ vpen->draw_line(bx, by, cx, cy);
+ break;
+ }
+ ax = bx; ay = by; pt0 = pt1;
+ bx = cx; by = cy; pt1 = pt2;
+ cx = dx; cy = dy; pt2 = fill.next();
+ }
+ switch( pt1->arc ) {
+ case ARC_LINE:
+ vpen->draw_line(bx, by, cx, cy);
+ if( fill.exists() ) {
+ dx = pnt0->x; dy = pnt0->y;
+ vpen->draw_line(cx,cy, dx,dy);