java判断某个点是否在所画多边形/圆形内

 更新时间:2018年05月26日 16:01:08   作者:阿杜_ardo  
这篇文章主要为大家详细介绍了java判断某个点是否在所画多边形或圆形内的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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; 
  } 
   
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java中的反射机制详解

    Java中的反射机制详解

    这篇文章主要介绍了JAVA 反射机制的相关知识,文中讲解的非常细致,代码帮助大家更好的理解学习,感兴趣的朋友可以了解下
    2021-09-09
  • springboot中@component注解的使用实例

    springboot中@component注解的使用实例

    这篇文章主要介绍了springboot中@component注解的使用实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • java对接webservice接口的4种方式总结

    java对接webservice接口的4种方式总结

    因工作需要和一个Sap相关系统以WebService的方式进行接口联调,之前仅听过这种技术,但并没有实操过,所以将本次开发进行记录,这篇文章主要给大家介绍了关于java对接webservice接口的4种方式,需要的朋友可以参考下
    2023-10-10
  • Java并发编程示例(五):线程休眠与恢复

    Java并发编程示例(五):线程休眠与恢复

    这篇文章主要介绍了Java并发编程示例(五):线程休眠与恢复,在本节,我们将开发一个程序,使用sleep()方法来实现每秒钟打印一次当前时间,需要的朋友可以参考下
    2014-12-12
  • idea搭建可运行Servlet的Web项目

    idea搭建可运行Servlet的Web项目

    在网上看到一篇很详细的 intelliJ IDEA 创建web项目并简单部署servlet的图文教程,今天自己也配置一下,留个笔记,感兴趣的可以了解一下
    2021-06-06
  • Java迭代器实现Python中的range代码实例

    Java迭代器实现Python中的range代码实例

    这篇文章主要介绍了Java迭代器实现Python中的range代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java中Optional.of()方法及源码解析(非常详细!)

    Java中Optional.of()方法及源码解析(非常详细!)

    这篇文章主要给大家介绍了关于Java中Optional.of()方法及源码解析的相关资料,Java中java.util .Optional类的of()方法用于获得该Optional类中具有指定类型的指定值的一个实例,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-06-06
  • SpringBoot Maven打包插件spring-boot-maven-plugin无法解析原因

    SpringBoot Maven打包插件spring-boot-maven-plugin无法解析原因

    spring-boot-maven-plugin是spring boot提供的maven打包插件,本文主要介绍了SpringBoot Maven打包插件spring-boot-maven-plugin无法解析原因,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 详解Java信号量Semaphore的原理及使用

    详解Java信号量Semaphore的原理及使用

    Semaphore来自于JDK1.5的JUC包,直译过来就是信号量,被作为一种多线程并发控制工具来使用。本文将详解其原理与使用方法,感兴趣的可以学习一下
    2022-05-05
  • SpringBoot整合第三方技术的实现

    SpringBoot整合第三方技术的实现

    本文主要介绍了SpringBoot整合第三方技术的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02

最新评论