@@ -734,7 +734,7 @@ ImagingDrawRectangle(
734734
735735int
736736ImagingDrawPolygon (Imaging im , int count , int * xy , const void * ink_ , int fill , int op ) {
737- int i , n ;
737+ int i , n , x0 , y0 , x1 , y1 ;
738738 DRAW * draw ;
739739 INT32 ink ;
740740
@@ -753,20 +753,38 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i
753753 return -1 ;
754754 }
755755 for (i = n = 0 ; i < count - 1 ; i ++ ) {
756- add_edge (& e [n ++ ], xy [i + i ], xy [i + i + 1 ], xy [i + i + 2 ], xy [i + i + 3 ]);
756+ x0 = xy [i * 2 ];
757+ y0 = xy [i * 2 + 1 ];
758+ x1 = xy [i * 2 + 2 ];
759+ y1 = xy [i * 2 + 3 ];
760+ if (y0 == y1 && i != 0 && y0 == xy [i * 2 - 1 ]) {
761+ // This is a horizontal line,
762+ // that immediately follows another horizontal line
763+ Edge * last_e = & e [n - 1 ];
764+ if (x1 > x0 && x0 > xy [i * 2 - 2 ]) {
765+ // They are both increasing in x
766+ last_e -> xmax = x1 ;
767+ continue ;
768+ } else if (x1 < x0 && x0 < xy [i * 2 - 2 ]) {
769+ // They are both decreasing in x
770+ last_e -> xmin = x1 ;
771+ continue ;
772+ }
773+ }
774+ add_edge (& e [n ++ ], x0 , y0 , x1 , y1 );
757775 }
758- if (xy [i + i ] != xy [0 ] || xy [i + i + 1 ] != xy [1 ]) {
759- add_edge (& e [n ++ ], xy [i + i ], xy [i + i + 1 ], xy [0 ], xy [1 ]);
776+ if (xy [i * 2 ] != xy [0 ] || xy [i * 2 + 1 ] != xy [1 ]) {
777+ add_edge (& e [n ++ ], xy [i * 2 ], xy [i * 2 + 1 ], xy [0 ], xy [1 ]);
760778 }
761779 draw -> polygon (im , n , e , ink , 0 );
762780 free (e );
763781
764782 } else {
765783 /* Outline */
766784 for (i = 0 ; i < count - 1 ; i ++ ) {
767- draw -> line (im , xy [i + i ], xy [i + i + 1 ], xy [i + i + 2 ], xy [i + i + 3 ], ink );
785+ draw -> line (im , xy [i * 2 ], xy [i * 2 + 1 ], xy [i * 2 + 2 ], xy [i * 2 + 3 ], ink );
768786 }
769- draw -> line (im , xy [i + i ], xy [i + i + 1 ], xy [0 ], xy [1 ], ink );
787+ draw -> line (im , xy [i * 2 ], xy [i * 2 + 1 ], xy [0 ], xy [1 ], ink );
770788 }
771789
772790 return 0 ;
0 commit comments