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