The examples in this article share with you the specific code for java to determine whether a point is within the range drawn for your reference. The specific content is as follows
IsPtInPoly.java
package com.ardo.util.circle; import java.util.ArrayList; import java.util.List; /** * java determines whether a point is within the drawn range (poly【isPtInPoly】/circle【distencePC】) * @param point Detection point* @param pts Vertex of the polygon* @return Point returns true in the polygon, otherwise it returns false * @author ardo */ public class IsPtInPoly { /** * Determine whether the point is within the polygon* @param point Detection point* @param pts Vertex of the polygon* @return Point returns true in polygon, otherwise return false */ public static boolean isPtInPoly(Point2D point, List<Point2D> pts){ int N = pts.size(); boolean boundOrVertex = true; //If the point is on the vertex or edge of the polygon, it can be regarded as a point in the polygon, and it will be directly returned true int intersectCount = 0;//cross points count of x double precision = 2e-10; //Tolerance when comparing with 0 during floating point type calculation Point2D p1, p2;//neighbour bound vertices Point2D p = point; //The current point p1 = pts.get(0);//left vertex for(int i = 1; i <= N; ++i){//check all rays if(p.equals(p1)){ return boundOrVertex;//p is an vertex } p2 = pts.get(i % N);//right vertex if(px < Math.min(p1.x, p2.x) || px > Math.max(p1.x, p2.x)){//ray is outside of our interests p1 = p2; continue;//next ray left point } if(px > Math.min(p1.x, p2.x) && px < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of) if(py <= Math.max(p1.y, p2.y)){//x is before of ray if(p1.x == p2.x && py >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray return boundOrVertex; } if(p1.y == p2.y){//ray is vertical if(p1.y == py){//overlies on a vertical ray return boundOrVertex; }else{//before ray ++intersectCount; } }else{//cross point on the left side double xinters = (px - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y if(Math.abs(py - xinters) < precision){//overlies on a ray return boundOrVertex; } if(py < xinters){//before ray ++intersectCount; } } } }else{//special case when ray is crossing through the vertex if(px == p2.x && py <= p2.y){//p crossing over p2 Point2D p3 = pts.get((i+1) % N); //next vertex if(px >= Math.min(p1.x, p3.x) && px <= Math.max(p1.x, p3.x)){//px lies between p1.x & p3.x ++intersectCount; }else{ intersectCount += 2; } } } p1 = p2;//next ray left point } if(intersectCount % 2 == 0){//Even numbers are outside the polygon return false; } else { //Odd numbers are inside the polygon return true; } } /** * Determine whether it is inside the circle* @param p * @param c * @return */ public static String distancePC(Point2D p,Circle c){//Judge the relationship between the distance between the point and the center of the circle and the radius of the circle String s ; double d2 = Math.hypot( (p.getX() - c.getCC().getX() ), (p.getY() - c.getCC().getY()) ); System.out.println("d2=="+d2); double r = c.getR(); if(d2 > r){ s = "outside the circle"; }else if(d2 < r){ s = "inside the circle"; }else{ s = "on the circle"; } return s; } public static void main(String[] args) { Point2D point = new Point2D(116.404072, 39.916605); // Test whether a point is inside a polygon List<Point2D> pts = new ArrayList<Point2D>(); pts.add(new Point2D(116.395, 39.910)); pts.add(new Point2D(116.394, 39.914)); pts.add(new Point2D(116.403, 39.920)); pts.add(new Point2D(116.402, 39.914)); pts.add(new Point2D(116.410, 39.913)); if(isPtInPoly(point, pts)){ System.out.println("Point is inside a polygon"); }else{ System.out.println("Point is outside a polygon"); } // Test whether a point is inside a circle Point2D centerPoint = new Point2D(116.404172, 39.916605); Circle c = new Circle(); c.setCC(centerPoint); c.setR(0.0056); String s = distancePC(point,c); System.out.println("Is the point in the circle: "+s); } } Circle.java
/** * Circular class* @author ardo * */ public class Circle { private double r; private Point2D cc; public void setR(double a){ r = a; } public void setCC(Point2D centerOfCir){ cc = centerOfCir; } public double getR(){ return r; } public Point2D getCC(){ return cc; } } Point2D.java
public class Point2D { public double x; public double y; public Point2D(double x, double y) { super(); this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.