CLipping.cpp
上传用户:whjcdz88
上传日期:2011-09-07
资源大小:121k
文件大小:4k
- #include "Clipping.h"
- int get_code( POINT p , RECT rect )
- {
- int code = 0 ;
- if( p.y < rect.top )
- code |= TOP ;
- else
- if( p.y > rect.bottom )
- code |= BOTTOM ;
- if( p.x > rect.right )
- code |= RIGHT ;
- else
- if( p.x < rect.left )
- code |= LEFT ;
- return code ;
- }
- int Cohen_Sutherland_Line_Clipping( POINT& p1 ,int Code1 ,POINT& p2 , int Code2 , RECT rect , HDC hdc )
- {
- int draw , done , code ;
- int x , y ;
- int code1 = Code1 , code2 = Code2 ;
- draw = 0 ; done = 0 ;
- while( !done )
- {
- if( code1 == 0 && code2 == 0 )
- {
- draw = 1 ; done = 1 ;
- }
- else
- if( (code1 & code2) != 0 )
- done = 1 ;
- else
- {
- draw = 1 ;
- if( code1 != 0 )
- code = code1 ;
- else
- code = code2 ;
- if( (code & TOP) != 0 )
- {
- y = rect.top ;
- x = (int)(p1.x + (float)( y - p1.y ) * ( p2.x - p1.x ) / ( p2.y - p1.y ));
- }
- else
- {
- if( (code & BOTTOM) != 0 )
- {
- y = rect.bottom ;
- x = (int)(p1.x + (float)( y - p1.y ) * ( p2.x - p1.x ) / ( p2.y - p1.y ));
- }
- else
- if( (code & RIGHT) != 0 )
- {
- x = rect.right ;
- y = (int)(p1.y + (float)( x - p1.x ) * ( p2.y - p1.y ) / ( p2.x - p1.x )) ;
- }
- else
- if( (code & LEFT) != 0 )
- {
- x = rect.left ;
- y = (int)(p1.y + (float)( x - p1.x ) * ( p2.y - p1.y ) / ( p2.x - p1.x ));
- }
- }
- if( code == code1 )
- {
- p1.x = x ; p1.y = y ;
- code1 = get_code( p1, rect );
- }
- else
- {
- p2.x = x ; p2.y = y ;
- code2 = get_code( p2 , rect );
- }
- }
- }
- if( draw == 1 )
- {
- MoveToEx( hdc , p1.x , p1.y , NULL );
- LineTo( hdc , p2.x , p2.y ) ;
- }
- return draw ;
- }
-
- void Clipping( POINT points[] , int count , RECT rect ,HDC hdc )
- {
- POINT * In = new POINT[ count * 2 ] ;
- POINT * Out = new POINT[ count * 2 ];
- int * Code = new int[ count ] ;
- int i ;
- for( int k = 0 ; k < count ; k ++ )
- Code[k] = get_code( points[k] , rect ) ;
- int in = 0 , out = 0 , draw = 0 ;
- for( i = 0 ; i < count ; i ++ )
- {
- In[ in ] = points[ i ] ;
- Out[ out ] = points[ ( i + 1 )%count ];
- draw = Cohen_Sutherland_Line_Clipping( In[ in ] ,Code[i] , Out[ out ] , Code[ (i+1)%count ] ,rect , hdc );
- if( draw == 1 )
- {
- if( Code[i] != 0 ) // points[i] is out of the window
- in ++ ;
- if( Code[ (i+1)%count] != 0 )
- out ++ ;
- }
- }
- int position = 0 , finish = 0 ;
- POINT Corner[4] = { {rect.left , rect.bottom } , { rect.right , rect.bottom } , {rect.right , rect.top },{rect.left , rect.top } } ;
- POINT Position , start ;
- int done = 0 ;
- while( finish < out )
- {
- start = Out[ finish ] ;
- done = 0 ;
- if( Out[ finish ] .x == rect.left )
- position = 0 ;
- if( Out[ finish ].x == rect.right )
- position = 2 ;
- if( Out[ finish ].y == rect.top )
- position = 3 ;
- if( Out[ finish ] .y == rect.bottom )
- position = 1 ;
- while( !done )
- {
- Position = Corner[ position ] ;
- for( i = 0 ; i < in ; i ++ )
- {
- switch( position )
- {
- case 0 :
- if( In[i].y < Position.y && In[i].x == rect.left && In[i].y > start.y )
- {
- done = 1 ; Position = In[i] ;
- }
- break;
- case 1 :
- if( In[i].x > start.x && In[i].y == rect.bottom && In[i].x < Position.x )
- {
- done = 1 ; Position = In[i] ;
- }
- break;
- case 2 :
- if( In[i].x == rect.right && In[i].y < start.y && In[i].y > Position.y )
- {
- done = 1 ; Position = In[i] ;
- }
- break;
- case 3 :
- if( In[i].y == rect.top && In[i].x < start.x && In[i].x > Position.x )
- {
- done = 1 ; Position = In[i] ;
- }
- break;
- }
- }
- MoveToEx( hdc , start.x , start.y , NULL );
- LineTo( hdc , Position.x , Position.y );
- if( done == 1 )
- finish ++ ;
- else
- {
- position ++ ; start = Position ;
- }
- }
- }
- delete[] In;
- delete[] Out ;
- delete[] Code;
- }
-