Determine if a point falls within the polygon

Source: Internet
Author: User
Tags abs

Turn from: http://www.cnblogs.com/gis_gps/archive/2009/02/12/1389071.html

Spare

I. Background:

How to determine whether a specified latitude and longitude point falls in a polygon region.

Second, the Implementation Code (Delphi)


Type
Tmypoint = Packed record
x:double;
y:double;
End

{*------------------------------------------------------------------------------
Determines whether the specified latitude and longitude coordinate point falls within the specified polygon region
@param the longitude of the ALon point
@param the latitude of the Alat point
@param apoints Specifies the coordinates of each node of the polygon region
@return True falls within range False is not in range
------------------------------------------------------------------------------*}
function Isptinpoly (ALon, alat:double; Apoints:array of Tmypoint): Boolean;
Var
Isum, icount, Iindex:integer;
DLon1, DLon2, DLAT1, DLAT2, dlon:double;
Begin
Result: = False;
if (Length (apoints) < 3) Then
Begin
Result: = False;
Exit;
End
Isum: = 0;
Icount: = Length (apoints);
For Iindex: =0 to ICount-1 do
Begin
if (Iindex = iCount-1) Then
Begin
DLon1: = Apoints[iindex]. X
DLAT1: = Apoints[iindex]. Y
DLon2: = Apoints[0]. X
DLAT2: = Apoints[0]. Y
End
Else
Begin
DLon1: = Apoints[iindex]. X
DLAT1: = Apoints[iindex]. Y
DLon2: = Apoints[iindex + 1]. X
DLAT2: = Apoints[iindex + 1]. Y
End
if ((Alat >= dLat1) and (Alat < DLAT2)) or ((ALAT&GT;=DLAT2) and (Alat < DLAT1)) then
Begin
if (ABS (DLAT1-DLAT2) > 0) Then
Begin
Dlon: = DLon1-((dlon1-dlon2) * (Dlat1-alat))/(DLAT1-DLAT2);
if (Dlon < ALon) Then
INC (isum);
End
End

End
if (isum mod 2 <> 0) Then
Result: = True;
End

_______________________________________________________________________________________________________________ _____________

C # algorithm:///<summary>        ///  Determines whether the specified latitude and longitude coordinate points fall within the specified polygon area         ///</summary>        ///<param name= " ALon "> Point of Longitude </param>        ///<param name=" Alat "> Point of Latitude </ param>        ///<param name= "apoints" > Specify Polygon area node coordinates </param>        ///<returns>true fall within range False not in range </returns>          public bool Isptinpoly (double ALon, double Alat, point[] apoints)          {            int isum, icount, Iindex;             double DLon1, DLon2, DLAT1, DLAT2, Dlon;             if (apoints.length< 3)             {                 return false;            }              isum = 0;             icount = apoints.length;             for (iindex = 0; iindex < iCount-1 iindex++) &NB sp;           {                 if (iindex = iCount-1)                  {                     dLon1 = Apoints[iindex]. X                     DLAT1 = Apoints[iindex]. Y                      DLon2 = apoints[0]. X                      DLAT2 = apoints[0]. Y                }                  Else                  {                     dLon1 = Apoints[iindex]. X                      DLAT1 = Apoints[iindex]. Y                     DLon2 = Apoints[iindex + 1]. X                      DLAT2 = Apoints[iindex + 1]. Y                }                  if ((Alat >= dLat1) && (Alat < DLAT2)) | | ((Alat >= dLat2) && (Alat < DLAT1))                 {                     if (Math.Abs ( DLAT1-DLAT2) > 0)                      {                    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.