| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

point_in_poly

#include "sflmath.h"
int
point_in_poly (const FPOINT *point, const FPOINT *pgon, int nbpoints)

Synopsis

Checks if the requested FPOINT is within the specified polygon. Returns TRUE or FALSE.

Source Code - (sflmath.c)

{
    int
        inside_flag,
        xflag0,
        crossings;
    const double
        *stop;
    double
        *p,
        tx,
        ty,
        y;

    crossings = 0;

    tx = point-> x;
    ty = point-> y;
    y  = pgon [nbpoints - 1].y;

    p = (double *) pgon + 1;
    if ((y >= ty) != (*p >= ty))
      {
        if ((xflag0 = (pgon [nbpoints - 1].x >= tx)) ==
                (*(double *) pgon >= tx))
          {
            if (xflag0)
               crossings++;
          }
        else
            crossings += (pgon [nbpoints - 1].x - (y - ty) *
                         (*(double *) pgon - pgon [nbpoints - 1].x) /
                         (*p - y)) >= tx;
      }
     stop = &pgon [nbpoints].y;
     for (y = *p, p += 2; p <= stop; y = *p, p += 2)
       {
         if (y >= ty)
           {
             while ((p < stop) && (*p >= ty))
                 p += 2;
             if (p >= stop)
                break;
             if ((xflag0 = (*(p - 3) >= tx)) == (*(p - 1) >= tx))
               {
                 if (xflag0)
                    crossings++;
               }
             else
                 crossings += (*(p - 3) - (*(p - 2) - ty) *
                              (*(p - 1) - *(p - 3)) /
                              (*p - *(p - 2))) >= tx;
           }
         else
           {
             while ((p < stop) && (*p < ty))
                p += 2;
             if (p >= stop)
                break;
             if ((xflag0 = (*(p - 3) >= tx)) == (*(p - 1) >= tx))
               {
                 if (xflag0)
                    crossings++;
               }
             else
                 crossings += (*(p - 3) - (*(p - 2) - ty) *
                              (*(p - 1) -  *(p - 3)) /
                              (*p - *(p - 2))) >= tx;
           }
       }
     inside_flag = crossings & 0x01;
     return (inside_flag);
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation