Polygon Range Point Determination algorithm

Source: Internet
Author: User

To determine whether a two-dimensional coordinate point is within a polygon range box, first give the coordinates of each vertex of the range box (given in a clockwise direction), put in two arrays and then compare the size range to determine whether the point is in the Polygon return box

Code is as follows:

public class Rangepoint {double[] x_points;double[] y_points;public rangepoint () {}public rangepoint (double[] x_points, Double[] y_points) {this.x_points = X_points;this.y_points = y_points;} public boolean Rangematch (double x, double y) {int J = X_points.length-1;boolean odd_nodes = false;for (int i = 0; I &lt ; X_points.length; i++) {if (((Y_points[i] < y && y_points[j] >= y) | |                (Y_points[j] < y && y_points[i] >= y))                     && (X_points[i] <= x | | x_points[j] <= x) {odd_nodes ^= (X_points[i]        + (Y-y_points[i])/(Y_points[j]-y_points[i]) * (X_points[j]-x_points[i]) < x);        } j = i;}        if (odd_nodes==true) {return true; } return false;} public static void Main (string[] args) {double[] x_points = {0, 0, 2, 2};d ouble[] y_points = {0, 1.8, 2, 0};d ouble x = 1 .9;double y = 1.8; Rangepoint rp = new Rangepoint (X_points,y_poINTs); if (Rp.    Rangematch (x, y)) {System.out.println ("This Range include point:" + x + "," + y); }}}

Well, what's the use of this thing??

See the following test code you may find it useful, this is to determine whether a latitude and longitude coordinates in a given range of areas, the following is the Imperial palace vertex latitude and longitude (can be ditu.google.cn Click to see the specific latitude and longitude value)

public static void Main (string[] args) {double[] x_points = {39.922886, 39.923264, 39.913275, 39.912929};d ouble[] Y_point s = {116.391517, 116.40199, 116.402505, 116.392034};     Double x = 39.922804;  Double y = 116.391581;  Rangepoint rp = new Rangepoint (x_points,y_points);    if (Rp. Rangematch (x, y)) {    System.out.println ("This Range include point:" + x + "," + y);    }        }



Polygon Range Point Determination algorithm

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.