@@ -579,6 +579,53 @@ RendererAgg::draw_polygon(const Py::Tuple& args) {
579579
580580}
581581
582+
583+
584+ SnapData
585+ SafeSnap::snap (const float & x, const float & y) {
586+ xsnap = (int )x + 0.5 ;
587+ ysnap = (int )y + 0.5 ;
588+
589+
590+
591+ if ( first || ( (xsnap!=lastxsnap) || (ysnap!=lastysnap) ) ) {
592+ lastxsnap = xsnap;
593+ lastysnap = ysnap;
594+ lastx = x;
595+ lasty = y;
596+ first = false ;
597+ return SnapData (true , xsnap, ysnap);
598+ }
599+
600+ // ok both are equal and we need to do an offset
601+ if ( (x==lastx) && (y==lasty) ) {
602+ // no choice but to return equal coords; set newpoint = false
603+ lastxsnap = xsnap;
604+ lastysnap = ysnap;
605+ lastx = x;
606+ lasty = y;
607+ return SnapData (false , xsnap, ysnap);
608+ }
609+
610+ // ok the real points are not identical but the rounded ones, so do
611+ // a one pixel offset
612+ if (x>lastx) xsnap += 1 .;
613+ else if (x<lastx) xsnap -= 1 .;
614+
615+ if (y>lasty) ysnap += 1 .;
616+ else if (y<lasty) ysnap -= 1 .;
617+
618+ lastxsnap = xsnap;
619+ lastysnap = ysnap;
620+ lastx = x;
621+ lasty = y;
622+ return SnapData (true , xsnap, ysnap);
623+ }
624+
625+
626+
627+
628+
582629Py::Object
583630RendererAgg::draw_line_collection (const Py::Tuple& args) {
584631
@@ -669,8 +716,14 @@ RendererAgg::draw_line_collection(const Py::Tuple& args) {
669716 xys = segments[i%Nsegments];
670717 size_t numtups = xys.length ();
671718 if (numtups<2 ) continue ;
719+
720+
672721 bool snapto=numtups==2 ;
673722 agg::path_storage path;
723+
724+ // std::cout << "trying snapto " << numtups << " " << snapto << std::endl;
725+
726+ SafeSnap snap;
674727
675728
676729 for (size_t j=0 ; j<numtups; j++) {
@@ -694,8 +747,17 @@ RendererAgg::draw_line_collection(const Py::Tuple& args) {
694747 }
695748
696749 if (snapto) { // snap to pixel for len(2) lines
697- thisx = (int )thisx + 0.5 ;
698- thisy = (int )thisy + 0.5 ;
750+ SnapData snapdata (snap.snap (thisx, thisy));
751+ // TODO: process newpoint
752+ // if (!snapdata.newpoint) {
753+ // std::cout << "newpoint warning " << thisx << " " << thisy << std::endl;
754+ // }
755+ // std::cout << "snapto" << thisx << " " << thisy << std::endl;
756+ thisx = snapdata.xsnap ;
757+ thisy = snapdata.ysnap ;
758+
759+ // thisx = (int)thisx + 0.5;
760+ // thisy = (int)thisy + 0.5;
699761 }
700762
701763 if (j==0 ) path.move_to (thisx, height-thisy);
0 commit comments