home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / layout / ptinpoly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.5 KB  |  130 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "xp.h"
  20.  
  21.  
  22. Bool
  23. lo_is_location_in_poly(int32 x, int32 y, int32 *coords, int32 coord_cnt)
  24. {
  25.     int32 totalv = 0;
  26.     int32 totalc = 0;
  27.     int32 intersects = 0;
  28.     int32 wherex = x;
  29.     int32 wherey = y;
  30.     int32 xval, yval;
  31.     int32 *pointer;
  32.     int32 *end;
  33.  
  34.     totalv = coord_cnt / 2;
  35.     totalc = totalv * 2;
  36.  
  37.     xval = coords[totalc - 2];
  38.     yval = coords[totalc - 1];
  39.     end = &coords[totalc];
  40.  
  41.     pointer = coords + 1;
  42.  
  43.     if ((yval >= wherey) != (*pointer >= wherey)) 
  44.         if ((xval >= wherex) == (*coords >= wherex)) 
  45.         intersects += (xval >= wherex);
  46.         else 
  47.             intersects += (xval - (yval - wherey) * 
  48.                           (*coords - xval) /
  49.                           (*pointer - yval)) >= wherex;
  50.  
  51.     while(pointer < end)  {
  52.         yval = *pointer;
  53.         pointer += 2;
  54.  
  55.         if(yval >= wherey)  {
  56.        while((pointer < end) && (*pointer >= wherey))
  57.         pointer+=2;
  58.        if (pointer >= end)
  59.         break;
  60.            if( (*(pointer-3) >= wherex) == (*(pointer-1) >= wherex) )
  61.             intersects += (*(pointer-3) >= wherex);
  62.          else
  63.         intersects += (*(pointer-3) - (*(pointer-2) - wherey) *
  64.                               (*(pointer-1) - *(pointer-3)) /
  65.                               (*pointer - *(pointer - 2))) >= wherex;
  66.  
  67.         }  else  {
  68.        while((pointer < end) && (*pointer < wherey))
  69.         pointer+=2;
  70.        if (pointer >= end)
  71.         break;
  72.            if( (*(pointer-3) >= wherex) == (*(pointer-1) >= wherex) )
  73.         intersects += (*(pointer-3) >= wherex);
  74.         else
  75.             intersects += (*(pointer-3) - (*(pointer-2) - wherey) *
  76.                               (*(pointer-1) - *(pointer-3)) /
  77.                               (*pointer - *(pointer - 2))) >= wherex;
  78.         }  
  79.     }
  80.     if (intersects & 1)
  81.         return (TRUE);
  82.     else
  83.         return (FALSE);
  84. }
  85.  
  86.  
  87. /* #ifndef NO_TAB_NAVIGATION  */
  88. Bool
  89. lo_find_location_in_poly(int32 *xx, int32 *yy, int32 *coords, int32 coord_cnt)
  90. {
  91.     int32  x1, y1, x2, y2, px1, py1, px2, py2;
  92.     int32    ii;
  93.  
  94.     /* get a edge of the polygon */
  95.     x1 =  y1 = x2 = y2 = 0;
  96.     for( ii = 0; (ii <= coord_cnt-4) && (x1==x2) && (y1==y2); ii+=2 ) { 
  97.         x1 = coords[ii];
  98.         y1 = coords[ii+1];
  99.         x2 = coords[ii+2];
  100.         y2 = coords[ii+3];
  101.  
  102.         if( (x1!=x2) || (y1!=y2) ) {
  103.             /* got a good edge, try it. */
  104.             /* get a point on each side of the edge, 2 pixels away, not too close */
  105.             px1 = ( x1 + x2 ) / 2 + 2;
  106.             px2 = px1 - 4;
  107.             py1 = ( y1 + y2 ) / 2 + 2;
  108.             py2 = py1 - 4;
  109.  
  110.             if( lo_is_location_in_poly( px1, py1, coords, coord_cnt) ) {
  111.                 *xx = px1;
  112.                 *yy = py1;
  113.                 return( TRUE );
  114.             }
  115.  
  116.             if( lo_is_location_in_poly( px2, py2, coords, coord_cnt) ) {
  117.                 *xx = px2;
  118.                 *yy = py2;
  119.                 return( TRUE );
  120.             }
  121.         }
  122.  
  123.     }
  124.  
  125.  
  126.     return( FALSE );
  127. }
  128. /* NO_TAB_NAVIGATION */
  129.  
  130.