[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Question: 'Skins' support



Pete Shinners wrote:
[...]

> only use rectangles for the hotspots. although if you end
> up adding hit-detection for the other html-map shapes like
> circle and polygon, please send the code in, i know many
> people want something like that :]
> 


I found this on the web. C -> Python translation and bugs by Niki Spahiev.


def point_in_poly( p, poly ):
     """
char InPoly( tPointi q, tPolygoni P, int n )
{
   int	 i, i1;      /* point index; i1 = i-1 mod n */
   int	 d;          /* dimension index */
   double x;          /* x intersection of e with ray */
   int	 Rcross = 0; /* number of right edge/ray crossings */
   int    Lcross = 0; /* number of left edge/ray crossings */

   printf("\n==>InPoly: q = "); PrintPoint(q); putchar('\n');

   /* Shift so that q is the origin. Note this destroys the polygon.
      This is done for pedogical clarity. */
   for( i = 0; i < n; i++ ) {
     for( d = 0; d < DIM; d++ )
       P[i][d] = P[i][d] - q[d];
   }

   /* For each edge e=(i-1,i), see if crosses ray. */
   for( i = 0; i < n; i++ ) {
     /* First see if q=(0,0) is a vertex. */
     if ( P[i][X]==0 && P[i][Y]==0 ) return 'v';
     i1 = ( i + n - 1 ) % n;
     /* printf("e=(%d,%d)\t", i1, i); */

     /* if e "straddles" the x-axis... */
     /* The commented-out statement is logically equivalent to the one
        following. */
     /* if( ( ( P[i][Y] > 0 ) && ( P[i1][Y] <= 0 ) ) ||
        ( ( P[i1][Y] > 0 ) && ( P[i] [Y] <= 0 ) ) ) { */

     if( ( P[i][Y] > 0 ) != ( P[i1][Y] > 0 ) ) {

       /* e straddles ray, so compute intersection with ray. */
       x = (P[i][X] * P[i1][Y] - P[i1][X] * P[i][Y])
         / (double)(P[i1][Y] - P[i][Y]);
       /* printf("straddles: x = %g\t", x); */

       /* crosses ray if strictly positive intersection. */
       if (x > 0) Rcross++;
     }
     /* printf("Right cross=%d\t", Rcross); */

     /* if e straddles the x-axis when reversed... */
     /* if( ( ( P[i] [Y] < 0 ) && ( P[i1][Y] >= 0 ) ) ||
        ( ( P[i1][Y] < 0 ) && ( P[i] [Y] >= 0 ) ) )  { */

     if ( ( P[i][Y] < 0 ) != ( P[i1][Y] < 0 ) ) {

       /* e straddles ray, so compute intersection with ray. */
       x = (P[i][X] * P[i1][Y] - P[i1][X] * P[i][Y])
           / (double)(P[i1][Y] - P[i][Y]);
       /* printf("straddles: x = %g\t", x); */

       /* crosses ray if strictly positive intersection. */
       if (x < 0) Lcross++;
     }
     /* printf("Left cross=%d\n", Lcross); */
   }	

   /* q on the edge if left and right cross are not the same parity. */
   if( ( Rcross % 2 ) != (Lcross % 2 ) )
     return 'e';

   /* q inside iff an odd number of crossings. */
   if( (Rcross % 2) == 1 )
     return 'i';
   else	return 'o';
}
     """
     X=0;Y=1
     Rcross = Lcross = 0
     Pj = subp( poly[-1], p )
     for Pi in poly:
         Pi = subp( Pi, p )
         if Pi[X] == 0 and Pi[Y] == 0:
             return 0
         if ( Pi[Y] > 0 ) != ( Pj[Y] > 0 ):
             x = (Pi[X] * Pj[Y] - Pj[X] * Pi[Y])
             y = (Pj[Y] - Pi[Y])
             if x/y > 0:
                 Rcross = not Rcross
         if ( Pi[Y] < 0 ) != ( Pj[Y] < 0 ):
             x = (Pi[X] * Pj[Y] - Pj[X] * Pi[Y])
             y = (Pj[Y] - Pi[Y])
             if x/y < 0:
                 Lcross = not Lcross
         Pj = Pi
     if Rcross != Lcross:
         return 0
     elif Rcross:
         return 1
     else:
         return -1

def addp( pi, pj ):
     return pi[0]+pj[0],pi[1]+pj[1]
def subp( pi, pj ):
     return pi[0]-pj[0],pi[1]-pj[1]


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org