Determines whether the GPS longitude and latitude are in the circle, polygon, or rectangle.

Source: Internet
Author: User

Class relationship
{
Static void main (string [] ARGs)
{
// Vector2d point1 = new vector2d (39.909209536859834, 116.3225715637207); // in
Vector2d point1 = new vector2d (39.901045, 116.415596); // out
Vector2d cpoint = new vector2d (39.909209536859834, 116.3225715637207 );
Console. writeline ("Whether the vertex is in the circle:" + whether the vertex is in the circle (cpoint, 8000, point1 ));
Console. writeline ("Whether the vertex is in the circle:" + whether the vertex is in the circle (cpoint, new vector2d (39.901045, 116.415596), point1 ));

// Vector2d point2 = new vector2d (39.924745, 116.379204); // in
Vector2d point2 = new vector2d (39.928695, 116.546059); // out
Vector2d pointa = new vector2d (39.913423004886866, 116.36890411376953 );
Vector2d pointb = new vector2d (39.93450133090293, 116.38727188110351 );
Console. writeline ("Whether the vertex is in the rectangle:" + whether the vertex is in the rectangle (pointa, pointb, point2 ));

Vector2d point3 = new vector2d (39.957649, 116.376801); // in
// Vector2d point3 = new vector2d (39.919216, 116.2817); // out
List <vector2d> polygon = new list <vector2d> ();
Polygon. Add (New vector2d (39.909209536859834, 116.3225715637207 ));
Polygon. Add (New vector2d (39.95920953685983, 116.3725715637207 ));
Polygon. Add (New vector2d (39.95920953685983, 116.42257156372072 ));
Polygon. Add (New vector2d (39.909209536859834, 116.4725715637207 ));
Polygon. Add (New vector2d (39.85920953685984, 116.42257156372072 ));
Polygon. Add (New vector2d (39.85920953685984, 116.3725715637207 ));
Console. writeline ("Whether the vertex is in the polygon:" + whether the vertex is in the polygon (point3, polygon ));

Console. Readline ();
}

///


// whether the vertex is in the circle (the vertex is also considered in the circle on the edge)
///
/// Center Coordinate
/// circle radius
// current vertex
//
whether the public static bool point is in the circle (vector2d cpoint, double cradius, vector2d point)
{< br> var d1 = distance (cpoint. lat, cpoint. lon, point. lat, point. lon);
double distance = math. SQRT (math. pow (math. ABS (point. x-cpoint. x), 2) + math. pow (math. ABS (point. y-cpoint. y), 2);
return distance <= cradius;
}

Private Static double distance (double lon1, double LAT1, double lon2, double LAT2)
{
Double R = 6378137; // Earth radius
LAT1 = LAT1 * Math. PI/180.0;
LAT2 = LAT2 * Math. PI/180.0;
Double SA2 = math. Sin (LAT1-LAT2)/2.0 );
Double sb2 = math. Sin (lon1-lon2) * Math. PI/180.0)/2.0 );
Return 2 * r * Math. asin (math. SQRT (SA2 * SA2 + math. Cos (LAT1) * Math. Cos (LAT2) * sb2 * sb2 ));
}

/// <Summary>
/// Whether the vertex is in the circle (it is also considered in the circle on the edge)
/// </Summary>
/// <Param name = "cpoint"> coordinates of the center </param>
/// <Param name = "onpoint"> coordinates on the circular edge </param>
/// <Param name = "point"> current vertex </param>
/// <Returns> </returns>
Whether the public static bool point is in the circle (vector2d cpoint, vector2d onpoint, vector2d point)
{
Double cradius = distance (cpoint. Lat, cpoint. Lon, onpoint. Lat, onpoint. Lon );
Double distance = distance (cpoint. Lat, cpoint. Lon, point. Lat, point. Lon );
Return distance <= cradius;
}

///


// determines whether the vertex is inside the polygon (or inside the polygon)
///
/// current vertex
/// polygon Points
//
whether the public static bool point is within the polygon (vector2d point, list polygon)
{< br> var res = smath. calcpointpolygonrelation (point, polygon);
return res! = Pointpolygonrelation. outofpolygon;
}

///


// whether the vertex is in the rectangle
///
/// diagonal upper point A
// diagonal upper point B
// current vertex
//
whether the public static bool vertex is in a rectangle (vector2d pointa, vector2d pointb, vector2d point)
{< br> List polygon = new list ();
polygon. add (pointa);
polygon. add (New vector2d (PO INTA. x, pointb. y);
polygon. add (pointb);
polygon. add (New vector2d (pointb. x, pointa. y);
var res = smath. calcpointpolygonrelation (point, polygon);
return res! = Pointpolygonrelation. outofpolygon;
}< BR >}

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.