java判断某个点是否在所画多边形/圆形内
程序员文章站
2023-12-16 22:02:28
本文实例为大家分享了java判断某个点是否在所画范围内的具体代码,供大家参考,具体内容如下
isptinpoly.java
package com.ardo.u...
本文实例为大家分享了java判断某个点是否在所画范围内的具体代码,供大家参考,具体内容如下
isptinpoly.java
package com.ardo.util.circle; import java.util.arraylist; import java.util.list; /** * java判断某个点是否在所画范围内(多边形【isptinpoly】/圆形【distencepc】) * @param point 检测点 * @param pts 多边形的顶点 * @return 点在多边形内返回true,否则返回false * @author ardo */ public class isptinpoly { /** * 判断点是否在多边形内 * @param point 检测点 * @param pts 多边形的顶点 * @return 点在多边形内返回true,否则返回false */ public static boolean isptinpoly(point2d point, list<point2d> pts){ int n = pts.size(); boolean boundorvertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true int intersectcount = 0;//cross points count of x double precision = 2e-10; //浮点类型计算时候与0比较时候的容差 point2d p1, p2;//neighbour bound vertices point2d p = 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(p.x < math.min(p1.x, p2.x) || p.x > math.max(p1.x, p2.x)){//ray is outside of our interests p1 = p2; continue;//next ray left point } if(p.x > math.min(p1.x, p2.x) && p.x < math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of) if(p.y <= math.max(p1.y, p2.y)){//x is before of ray if(p1.x == p2.x && p.y >= math.min(p1.y, p2.y)){//overlies on a horizontal ray return boundorvertex; } if(p1.y == p2.y){//ray is vertical if(p1.y == p.y){//overlies on a vertical ray return boundorvertex; }else{//before ray ++intersectcount; } }else{//cross point on the left side double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y if(math.abs(p.y - xinters) < precision){//overlies on a ray return boundorvertex; } if(p.y < xinters){//before ray ++intersectcount; } } } }else{//special case when ray is crossing through the vertex if(p.x == p2.x && p.y <= p2.y){//p crossing over p2 point2d p3 = pts.get((i+1) % n); //next vertex if(p.x >= math.min(p1.x, p3.x) && p.x <= math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x ++intersectcount; }else{ intersectcount += 2; } } } p1 = p2;//next ray left point } if(intersectcount % 2 == 0){//偶数在多边形外 return false; } else { //奇数在多边形内 return true; } } /** * 判断是否在圆形内 * @param p * @param c * @return */ public static string distencepc(point2d p,circle c){//判断点与圆心之间的距离和圆半径的关系 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 = "圆外"; }else if(d2 < r){ s = "圆内"; }else{ s = "圆上"; } return s; } public static void main(string[] args) { point2d point = new point2d(116.404072, 39.916605); // 测试一个点是否在多边形内 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("点在多边形内"); }else{ system.out.println("点在多边形外"); } // 测试一个点是否在圆形内 point2d centerpoint = new point2d(116.404172, 39.916605); circle c = new circle(); c.setcc(centerpoint); c.setr(0.0056); string s = distencepc(point,c); system.out.println("点是否在圆内:"+s); } }
circle.java
/** * 圆形类 * @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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。