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

图形图象

开发平台:

WINDOWS

  1. #include "View3D.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[] )
  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  -1;
  38. else
  39. edge ->yUpper = upper.y;
  40. insertEdge( edges[lower.y ] , edge);
  41. }
  42. void buildEdgeList ( int cnt , POINT* pts , Edge* edges[]  )
  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 );
  55. else
  56. makeEdgeRec( v2 , v1 , yPrev , edge , edges );
  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 )
  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. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. //SetPixel ( hdc ,( int ) i , scan + ylow , RGB( 0 , 0 , 0 ) );
  81. p1 = p2 ->next;
  82. }
  83. }
  84. void deleteAfter ( Edge* q )
  85. {
  86. Edge* p = q ->next;
  87. q ->next = p ->next ;
  88. free( p );
  89. }
  90. void updateActiveList( int scan , Edge* active )
  91. {
  92. Edge* q = active , *p = active->next;
  93. while( p )
  94. {
  95. if( scan >= p->yUpper )
  96. {
  97. p = p ->next ; deleteAfter( q );
  98. }
  99. else
  100. {
  101. p ->xIntersect = p ->xIntersect + p ->dxPerScan ;
  102. q = p ; p = p ->next;
  103. }
  104. }
  105. }
  106. void resortActiveList( Edge* active)
  107. {
  108. Edge* q , *p = active->next;
  109. active->next = NULL;
  110. while( p )
  111. {
  112. q = p -> next; insertEdge( active , p );
  113. p = q ;
  114. }
  115. }
  116. void IsVisible( int cnt , Point* pts , int Hight )
  117. {
  118. Edge** edges = new Edge*[Hight];
  119. Edge* active;
  120. int i , scan ;
  121. for ( i = 0 ; i < yhigh - ylow ; i ++ )
  122. {
  123. edges[i] = ( Edge* )malloc( sizeof( Edge ) ) ;
  124. edges[i] ->next = NULL;
  125. }
  126. buildEdgeList( cnt , pts , edges );
  127. active = ( Edge* )malloc( sizeof ( Edge ) );
  128. active ->next = NULL;
  129. for ( scan = 0 ; scan < yhigh-ylow  ; scan ++ )
  130. {
  131. buildActiveList( scan , active , edges );
  132. if ( active -> next )
  133. {
  134. fillScan( scan ,active );
  135. updateActiveList( scan ,active);
  136. resortActiveList( active );
  137. }
  138. }
  139. delete[] edges;
  140. }