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 < ; 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