Polygon range point determination algorithm

Source: Internet
Author: User

Polygon range point determination algorithm

Determine whether a two-dimensional coordinate point is in a polygon range box. First, give the coordinates of each vertex in the range box (in clockwise direction ), put them in two arrays, and then compare the size range to determine whether the point is in the polygon return box.

 

 

The 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 < 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 };double[] y_points = {0, 1.8, 2, 0 };double 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 is the use of this item ??

If you see the following test code, you may find it useful. This is to determine whether the coordinates of a longitude and latitude are within the specified range, below is the longitude and latitude of the Palace Museum vertex (you can click ditu.google.cn to view the specific latitude and longitude value)

public static void main(String[] args) {double[] x_points = {39.922886, 39.923264, 39.913275, 39.912929 };double[] y_points = {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);    }        }



 

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.