Detect.cpp
上传用户:whjcdz88
上传日期:2011-09-07
资源大小:121k
文件大小:3k
- #include "View3D.h"
- void insertEdge( Edge* list , Edge* edge)
- {
- Edge* p , *q = list;
- p = q ->next;
- while( p != NULL )
- {
- if ( edge ->xIntersect < p ->xIntersect)
- p = NULL;
- else
- {
- q = p ; p = p ->next;
- }
- }
- edge ->next = q ->next;
- q ->next = edge;
- }
- int yNext ( int k , int cnt , POINT* pts)
- {
- int j;
- if( ( k + 1 ) > ( cnt - 1 ) )
- j = 0;
- else
- j = k + 1;
- while( pts[k].y == pts[j].y )
- if ( ( j + 1 ) > ( cnt - 1 ) )
- j = 0 ;
- else
- j ++;
- return ( pts[j].y );
- }
- void makeEdgeRec( POINT lower , POINT upper, int yComp , Edge* edge , Edge* edges[] )
- {
- edge->dxPerScan = ( float )( upper.x - lower.x ) / (upper.y - lower.y );
- edge ->xIntersect = lower.x;
- if ( upper.y < yComp )
- edge->yUpper = upper.y -1;
- else
- edge ->yUpper = upper.y;
- insertEdge( edges[lower.y ] , edge);
- }
- void buildEdgeList ( int cnt , POINT* pts , Edge* edges[] )
- {
- Edge* edge; POINT v1, v2;
- int i , yPrev = pts[cnt - 2 ].x ;
- v1.x = pts [ cnt - 1 ].x ; v1.y = pts[ cnt - 1 ].y ;
- for( i = 0 ; i < cnt ; i ++ )
- {
- v2 = pts[ i ] ;
- if ( v1.y != v2.y )
- {
- edge = ( Edge* ) malloc ( sizeof ( Edge) );
- if ( v1.y < v2.y )
- makeEdgeRec( v1 , v2 , yNext( i ,cnt , pts ) , edge , edges );
- else
- makeEdgeRec( v2 , v1 , yPrev , edge , edges );
- }
- yPrev = v1.y ; v1 = v2 ;
- }
- }
- void buildActiveList( int scan , Edge * active , Edge* edges[] )
- {
- Edge * p , * q ;
- p = edges[scan]->next;
- while( p )
- {
- q = p -> next ; insertEdge( active , p );
- p = q ;
- }
- }
- void fillScan( int scan , Edge* active )
- {
- Edge* p1 , *p2; int i;
- p1 = active->next;
- while( p1 )
- {
- p2 = p1 ->next;
- for ( i = p1 ->xIntersect ; p2 != NULL &&i < p2 ->xIntersect ; i ++ )
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- //SetPixel ( hdc ,( int ) i , scan + ylow , RGB( 0 , 0 , 0 ) );
- p1 = p2 ->next;
- }
- }
- void deleteAfter ( Edge* q )
- {
- Edge* p = q ->next;
- q ->next = p ->next ;
- free( p );
- }
- void updateActiveList( int scan , Edge* active )
- {
- Edge* q = active , *p = active->next;
- while( p )
- {
- if( scan >= p->yUpper )
- {
- p = p ->next ; deleteAfter( q );
- }
- else
- {
- p ->xIntersect = p ->xIntersect + p ->dxPerScan ;
- q = p ; p = p ->next;
- }
- }
- }
- void resortActiveList( Edge* active)
- {
- Edge* q , *p = active->next;
- active->next = NULL;
- while( p )
- {
- q = p -> next; insertEdge( active , p );
- p = q ;
- }
- }
- void IsVisible( int cnt , Point* pts , int Hight )
- {
- Edge** edges = new Edge*[Hight];
- Edge* active;
- int i , scan ;
- for ( i = 0 ; i < yhigh - ylow ; i ++ )
- {
- edges[i] = ( Edge* )malloc( sizeof( Edge ) ) ;
- edges[i] ->next = NULL;
- }
- buildEdgeList( cnt , pts , edges );
- active = ( Edge* )malloc( sizeof ( Edge ) );
- active ->next = NULL;
- for ( scan = 0 ; scan < yhigh-ylow ; scan ++ )
- {
- buildActiveList( scan , active , edges );
- if ( active -> next )
- {
- fillScan( scan ,active );
- updateActiveList( scan ,active);
- resortActiveList( active );
- }
- }
- delete[] edges;
- }