Fill.cpp
上传用户:whjcdz88
上传日期:2011-09-07
资源大小:121k
文件大小:3k
源码类别:

图形图象

开发平台:

WINDOWS

  1. #include "Fill.h"
  2. void insertEdge( Edge* list , Edge* edge)
  3. {
  4. Edge* p , *q = list;
  5. p = q ->next;
  6. while( p != NULL )
  7. {
  8. if ( edge ->xIntersect < p ->xIntersect)
  9. p = NULL;
  10. else
  11. {
  12. q = p ;  p = p ->next;
  13. }
  14. }
  15. edge ->next = q ->next;
  16. q ->next = edge;
  17. }
  18. int yNext ( int k , int cnt , POINT*  pts)
  19. {
  20. int j;
  21. if( ( k + 1 ) > ( cnt - 1 ) )
  22. j = 0;
  23. else
  24. j = k + 1;
  25. while( pts[k].y == pts[j].y )
  26. if ( ( j + 1 ) > ( cnt - 1 ) )
  27. j = 0 ;
  28. else
  29. j ++;
  30. return ( pts[j].y );
  31. }
  32. void makeEdgeRec( POINT lower , POINT upper, int yComp , Edge* edge , Edge* edges[] , int ylow)
  33. {
  34. edge->dxPerScan = ( float )( upper.x - lower.x ) / (upper.y - lower.y );
  35. edge ->xIntersect = lower.x;
  36. if ( upper.y < yComp )
  37. edge->yUpper = upper.y - ylow -1;
  38. else
  39. edge ->yUpper = upper.y - ylow;
  40. insertEdge( edges[lower.y - ylow] , edge);
  41. }
  42. void buildEdgeList ( int cnt , POINT* pts , Edge* edges[] , int ylow )
  43. {
  44. Edge* edge; POINT v1, v2;
  45. int i , yPrev = pts[cnt - 2 ].x ;
  46. v1.x = pts [ cnt - 1 ].x  ;  v1.y = pts[ cnt - 1 ].y ;
  47. for( i = 0 ; i < cnt ; i ++ )
  48. {
  49. v2 = pts[ i ] ; 
  50. if ( v1.y != v2.y )
  51. {
  52. edge = ( Edge* ) malloc ( sizeof ( Edge) );
  53. if ( v1.y < v2.y )
  54. makeEdgeRec( v1 , v2 , yNext( i ,cnt , pts ) , edge , edges , ylow);
  55. else
  56. makeEdgeRec( v2 , v1 , yPrev , edge , edges , ylow );
  57. }
  58. yPrev = v1.y ;  v1 = v2 ; 
  59. }
  60. }
  61. void buildActiveList( int scan , Edge * active , Edge* edges[] )
  62. {
  63. Edge * p , * q ;
  64. p = edges[scan]->next;
  65. while( p )
  66. {
  67. q = p -> next ; insertEdge( active , p );
  68. p = q ;
  69. }
  70. }
  71. void fillScan( int scan , Edge* active , HDC hdc , int ylow)
  72. {
  73. Edge* p1 , *p2;  int i;
  74. p1 = active->next;
  75. while( p1 )
  76. {
  77. p2 = p1 ->next;
  78. for ( i = p1 ->xIntersect ; p2 != NULL &&i < p2 ->xIntersect ; i ++ )
  79. SetPixel ( hdc ,( int ) i , scan + ylow , RGB( 0 , 0 , 0 ) );
  80. p1 = p2 ->next;
  81. }
  82. }
  83. void deleteAfter ( Edge* q )
  84. {
  85. Edge* p = q ->next;
  86. q ->next = p ->next ;
  87. free( p );
  88. }
  89. void updateActiveList( int scan , Edge* active )
  90. {
  91. Edge* q = active , *p = active->next;
  92. while( p )
  93. {
  94. if( scan >= p->yUpper )
  95. {
  96. p = p ->next ; deleteAfter( q );
  97. }
  98. else
  99. {
  100. p ->xIntersect = p ->xIntersect + p ->dxPerScan ;
  101. q = p ; p = p ->next;
  102. }
  103. }
  104. }
  105. void resortActiveList( Edge* active)
  106. {
  107. Edge* q , *p = active->next;
  108. active->next = NULL;
  109. while( p )
  110. {
  111. q = p -> next; insertEdge( active , p );
  112. p = q ;
  113. }
  114. }
  115. void scanFill( int cnt , POINT* pts , HDC hdc , int ylow , int yhigh)
  116. {
  117. Edge** edges = new Edge*[yhigh - ylow];
  118. Edge* active;
  119. int i , scan ;
  120. for ( i = 0 ; i < yhigh - ylow ; i ++ )
  121. {
  122. edges[i] = ( Edge* )malloc( sizeof( Edge ) ) ;
  123. edges[i] ->next = NULL;
  124. }
  125. buildEdgeList( cnt , pts , edges , ylow);
  126. active = ( Edge* )malloc( sizeof ( Edge ) );
  127. active ->next = NULL;
  128. for ( scan = 0 ; scan < yhigh-ylow  ; scan ++ )
  129. {
  130. buildActiveList( scan , active , edges );
  131. if ( active -> next )
  132. {
  133. fillScan( scan ,active , hdc , ylow );
  134. updateActiveList( scan ,active);
  135. resortActiveList( active );
  136. }
  137. }
  138. delete[] edges;
  139. }