C # Calculates the distance of a coordinate point on a map to the sides of a polygon

Source: Internet
Author: User
Tags asin cos pow sin

After judging whether a coordinate point is within a polygon, there is another requirement that when I have this coordinate point outside the polygon, I need to calculate whether the distance from the polygon to the polygonal is calculated by the latitude and longitude coordinates of the two locations in the range of the coordinates (C # version).

Turn from: 78423496

The method of finding the distance between points and segments in the latitude and longitude coordinate system transfer from C language version: 46487001 calculates the distance from a point to a polygon by the latitude and longitude coordinates on the map

Go from Java version 72881056?locationnum=12&fps=1

In some map applications (such as yaw), it is often necessary to find a point to a thread to determine whether to stay away from the route. However, in the latitude and longitude coordinates, there is no formula similar to the Cartesian coordinate system to calculate. In the latitude and longitude, the general application of the most widely used in the formula is to find a two-point distance method, how to calculate the distance between the two points to achieve the point-to-line method, we first look at the latitude and longitude to find a two-point distance calculation method.

A method for calculating the distance of two points in latitude and longitude

There are many ways to introduce this method on the Internet, which is no longer one by one explained. In the Northern hemisphere:

C = sin (lata*pi/180) *sin (latb*pi/180) + cos (lata*pi/180) *cos (latb*pi/180) *cos ((MLONA-MLONB) *pi/180)

Distance = R*arccos (C) *pi/180

Note 1: Where Lona, LatA, LONB, LATB respectively is a, b two points of latitude and longitude values, where the trigonometric function input and output are used in radians value

Note 2:r (Earth radius) and distance units are the same, if the use of 6378.137 km as a radius, then distance is the kilometer unit

C Language code:

DoubleGETDISTANCEBTWP (DoubleLona,DoubleLatA,DoubleLONB,DoubleLATB)//calculates the distance, x longitude, y latitude, according to the longitude and latitude of two points{    DoubleradLng1 = LatA * M_pi/180.0; DoubleRadLng2 = LATB * M_pi/180.0; DoubleA = radLng1-radLng2; Doubleb = (lona-lonb) * m_pi/180.0; Doubles =2* ASIN (SQRT (Pow (sin (A/2),2) + cos (radLng1) * cos (radLng2) * POW (sin (b/2),2))) *6378.137;//return units for kilometers    returns;}

The method of finding the distance between the point and the line in the latitude and longitude coordinates

In the warp and longitude coordinate system, find the distance D (LONC,LATC) to the segment with point A (Lona,lata) and point B (LONB,LATB) as the endpoint. This problem can be divided into three scenarios:

① Point C is directly above the line AB, the vertical distance from the d= point C to the straight AB, 1;

When the ②AC and ab form an obtuse angle, the length of AC from the d= segment is 2;

When the ③BC and ab form an obtuse angle, the length of BC from the d= segment is 3;

1, first how to determine what kind of situation

We can use the converse of the Pythagorean theorem, if the lengths of AB, BC, and AC are a,b,c respectively.

① if b*b+c*c<a*a, then the angle of side A is obtuse, that is, the case of Figure 1;

② if a*a+c*c<b*b, then the angle of side B is obtuse, that is, the case of Figure 2;

③ if a*a+b*b<c*c, then the angle of the side C is obtuse, that is, the case of Figure 3;

2, figure 1 distance of the case D

We hope that we can find the distance d by distance formula, and then associate it with the Helen formula.

In the Helen formula, the area of the triangle, where the distance is d=2s/a;

Iii. Summary of calculation methods

For the case of Figure 1 and calculated, the calculations for figures 2 and 3 have been converted to a distance formula between two points, which is no longer burdensome. Therefore, in the latitude and longitude coordinate system, the C language code that points to the distance of the line segment is as follows:

//the distance from point Pcx,pcy to line Pax,pay,pbx,pbyDoubleGetnearestdistance (DoublePAx,DoublePay,DoublePbxDoublePBy,DoublePCx,DoublePCy) {         DoubleA,b,c; A=getdistancebtwp (PAY,PAX,PBY,PBX);//the distance formula of two points in the latitude and longitude coordinate systemB=getdistancebtwp (PBY,PBX,PCY,PCX);//the distance formula of two points in the latitude and longitude coordinate systemC=getdistancebtwp (PAY,PAX,PCY,PCX);//the distance formula of two points in the latitude and longitude coordinate system    if(b*b>=c*c+a*a)returnC; if(c*c>=b*b+a*a)returnb; DoubleL= (A+B+C)/2;//half the perimeter.    DoubleS=sqrt (l* (l-a) * (l-b) * (l-c));//Helen formula to calculate area    return 2*s/A; }

Okay, here's the C-language version of the logic, and we can understand the basic computational logic.

Here's my simple modified C # version

  //radius of the earth, per metre        Private Const DoubleEarth_radius =6378137; /// <summary>        ///determine if it is within the error range/// </summary>        /// <param name= "point" ></param>        /// <param name= "points" ></param>        /// <param name= "limitdistance" ></param>        /// <returns></returns>         Public Static BOOLInlimitdistance (location point, list<location> points,Doublelimitdistance) {List<Double> distance=Newlist<Double>(); varLen =points.            Count; varMaxindex = Len-1;  for(inti =0; i < Len; i++)            {                //the current point in the polygon                varCurrentpoint =Points[i]; varNearpoint = Maxindex = = I? points[0]: Points[i +1]; DoubleA, B, C; A= Getdistance (point, Currentpoint);//the distance formula of two points in the latitude and longitude coordinate systemb = Getdistance (point, Nearpoint);//the distance formula of two points in the latitude and longitude coordinate systemc = getdistance (Currentpoint, nearpoint);//the distance formula of two points in the latitude and longitude coordinate system                if(b * b >= c * C + A *a) {distance.                    ADD (c); Continue; }                 if(c * C >= b * B + A *a) {distance.                    ADD (b); Continue; }                DoubleL = (A + B + c)/2;//half the perimeter.                Doubles = math.sqrt (L * (l-a) * (l-b) * (l-c));//Helen formula to calculate areaDistance. ADD (2* S/a); }            if(!distance. Any ()) {return false; }            varCount = distance. Where (s + S <limitdistance).            Count (); if(Count >0)return true; return false; }        /// <summary>        ///calculates the distance from the two-point position, and returns the distance of two points, in meters///This formula is available to Google with an error of less than 0.2 meters/// </summary>        /// <param name= "Lng1" >1th Longitude</param>        /// <param name= "LAT1" >1th Latitude</param>                /// <param name= "Lng2" >2nd Longitude</param>        /// <param name= "LAT2" >2nd Latitude</param>        /// <returns></returns>        Private Static Doublegetdistance (location point1, location Point2) {DoubleRADLAT1 =Rad (Point1.lat); DoubleRadLng1 =Rad (POINT1.LNG); DoubleRADLAT2 =Rad (Point2.lat); DoubleRadLng2 =Rad (POINT2.LNG); DoubleA = RADLAT1-RadLat2; Doubleb = radLng1-radLng2; Doubleresult =2* Math.asin (Math.sqrt (Math.pow (Math.sin (A/2),2) + Math.Cos (RADLAT1) * MATH.COS (RADLAT2) * MATH.POW (Math.sin (b/2),2))) *Earth_radius; returnresult; }        /// <summary>        ///longitude and latitude converted into radians/// </summary>        /// <param name= "D" ></param>        /// <returns></returns>        Private Static DoubleRad (Doubled) {return(Double) d * Math.PI/180d; }

C # Calculates the distance of a coordinate point on a map to the sides of a polygon

Related Article

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.