判断一个点是否在某个封闭曲线内的算法

 

http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=198803

//             判断某点是否落在多边形中。            2003.2.28.  
long   fall1( double   a3,   double   a1,   long   na,   long   nb,  CPoint3DArray &   f)  
{  
//a3  被测点X坐标  
//a1  被测点Y坐标  
//na  多边形首点序号  
//nb  多边形末点序号  
//f  多边形坐标数组  
       double  f1,f2,tk,lvalue,f3,f4;  
       
double  tt;  
       
long  ncr,n3;          
         
       
if(f[na].x>1000000.)        lvalue=1.0;  
       
else  if(f[na].x>100000.)        lvalue=0.1;  
       
else  if(f[na].x>10000.)        lvalue=0.01;  
       
else  if(f[na].x>1000.)        lvalue=0.001;  
       
else        lvalue  =  0.00000001;  
           lvalue  
=  0.00000001;  
/////        fp5=fopen("tmp1","wt");  
/*      WRITE(*,*)  NA,NB      */  
       ncr
=0;  
//        xx0=x0;  
//        yy0=y00;  
/*    printf(str,"%4ld  %4ld  %12.1f  %12.1f ",  
       a,nb,a3,a1);  
       extOut(hDC,10,150,str,70);  
       essageBox(hWnd,  str,  "fall1",  MB_OK    ¦  MB_ICONEXCLAMATION);    
*/
  
       
for(n3=na;n3<=nb;n3++)  
       
{  
/*    printf(str,"%4ld  %12.1f  %12.1f ",  
       3,(*(f+n3)).x,(*(f+n3)).y);  
       extOut(hDC,10,70,str,strlen(str));    
*/
  
                   
if(fabs(a3-f[n3].x)<lvalue&&fabs(a1-f[n3].y)<lvalue)  
                       
{  
                               ncr
=1;  
                               
goto  a99;  
                       }
  
       }
  
       n3
=na-1;  
a5:  n3
++;  
       f1
=f[n3].x;  
       f2
=f[n3+1].x;  
       f3  
=  (n3==na)  ?  f[nb-1].x  :  f[n3-1].x;  
       f4  
=  (n3+1==nb)  ?  f[na+1].x  :  f[n3+2].x;  
/*    ia1=(f1-x0)*xp+10;  
       ia2=410-((*(f+n3)).y-y00)*yp;  
       MoveTo(hDC,ia1,ia2);  
       ia1=(f2-x0)*xp+10;  
       ia2=410-((*(f+n3+1)).y-y00)*yp;  
       LineTo(hDC,ia1,ia2);    
*/
  
//    printf(fp5,"%4ld  %12.1f  %12.1f  %12.1f  %12.1f ",  
//    3,f1,(*(f+n3)).y,f2,(*(f+n3+1)).y);  
/*    if(fabs(a3-292840.)       {  
       printf(str,"%4ld  %12.1f  %12.1f  %12.1f  %12.1f",  
       3,f1,(*(f+n3)).y,f2,(*(f+n3+1)).y);  
       extOut(hDC,10,70,str,strlen(str));  
       }    
*/
  
a199:            ;  
           
if(fabs(f1-f2)<lvalue)  
           
{          /*    Xi=xi+1    */  
                       
if(fabs(a3-f1)<lvalue)  
                       
{  
                                   
if(f[n3].y>f[n3+1].y)  
                                   
{    /*    2    */  
                                               
if(a1>f[n3+1].y&&a1<f[n3].y)  
                                               
{      /*    1    */  
                                             ncr
=1;  
                                             
goto  a99;  
                                               }
            /*    1'    */  
                                               
else  
                                             
goto  a64;  
                                   }
    /*      2'    */  
                                   
else  
                                   
{      /*      2.1      */  
                                               
if(a1>f[n3].y&&a1<f[n3+1].y)  
                                               
{        /*    1      */  
                                                           ncr
=1;  
                                             
goto  a99;  
                                               }
  
                                               
else  
                                                   
goto  a64;  
                                   }
              /*    2.1'    */  
                       }
  
                       
else  
                               
goto  a64;  
           }
  
           
if(fabs(a3-f1)<lvalue)  
           
{  
                       
if(f2<f1&&f3<f1  ¦  ¦f2>f1&&f3>f1)goto  a64;  
                   f1
=f1+lvalue*2.;  
           }
    
           
if(fabs(a3-f2)<lvalue)  
           
{  
                             
if(f1<f2&&f4<f2  ¦  ¦f1>f2&&f4>f2)  
                       
{  
                               n3
++;  
                                         
goto  a64;  
                             }
  
                   f2
=f2+lvalue*2.;  
           }
  
           
if(fabs(f1-f2)<lvalue)  goto  a199;  
           
if(f1>f2&&(a3>f1  ¦  ¦a3<f2)  ¦  ¦f1<f2&&(a3>f2  ¦  ¦a3<f1))  goto  a64;  
/*    if(fabs(a3-292840.)       {  
       printf(str,"%4ld  %6ld  %12.1f  %12.1f  %12.1f  %12.1f",  
       3,nb,f1,(*(f+n3)).y,f2,(*(f+n3+1)).y);  
       extOut(hDC,10,90,str,strlen(str));  
       }      
*/
  
           tt
=(double)(f[n3+1].y-f[n3].y)/(double)(f2-f1);  
   
/*        if(f1>f2)  
               tk=tt*(a3-f1)+(*(f+n3)).y;  
               else  
               tk=tt*(a3-f2)+(*(f+n3+1)).y;      
*/
  
           tk
=tt*(double)(a3-f1)+f[n3].y;  
           
if(fabs(a1-tk)>lvalue)  goto  a63;  
           ncr
=1;  
           
goto  a99;  
a63:            ;  
           
if(a1>tk)  goto  a64;  
           ncr
=ncr+1;  
/*          sprintf(str,  
"%4ld  %4ld  %12.1f  %12.1f  %12.1f  %12.1f  %12.1  %12.1f  %12.1f  %12.1f  %12.1f ",  
             ncr,n3,a3,a1,(*(f+n3)).x,(*(f+n3)).y,(*(f+n3+1)).x,(*(f+n3+1)).y,f1,f2,tk);    
*/
  
/*          TextOut(hDC,10,150,str,strlen(str));    */  
   
/*        ia1=(f1-x0)*xp+10;  
               ia2=410-((*(f+n3)).y-y00)*yp;  
               MoveTo(hDC,ia1,ia2);  
               ia1=(f2-x0)*xp+10;  
               ia2=410-((*(f+n3+1)).y-y00)*yp;  
               LineTo(hDC,ia1,ia2);  
             MessageBox(hWnd,  str,  "ncr",  MB_OK    ¦  MB_ICONEXCLAMATION);      
*/
  
a64:            ;  
           
if(n3<nb-1)  goto  a5;  
/*    99            IER=SET_COLOR(0)  
CC            ier  =  filled_rectangle(2,470,640,480)  
CC                WRITE(VB,'(A,I4)')  'Ncr:',NCR  
CC                IER=GRAPHIC_TEXT(VB,2,470,12)  
CC                  IER=PAUSE()                                                  
*/
  
a99:          ;  
/*          sprintf(str,"%4ld  %4ld  %4ld  %12.1f  %12.1f ",  
             ncr,na,nb,a3,a1);  
             TextOut(hDC,10,150,str,70);  
             MessageBox(hWnd,  str,  "ncr",  MB_OK    ¦  MB_ICONEXCLAMATION);        
*/
  
//              fclose(fp5);  
       return  ncr;  
}
  
 

你可能感兴趣的:(收藏,算法,xp,vb,fp)