最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java判斷某個點是否在所畫多邊形/圓形內(nèi)

 更新時間:2018年05月26日 16:01:08   作者:阿杜_ardo  
這篇文章主要為大家詳細介紹了java判斷某個點是否在所畫多邊形或圓形內(nèi)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java判斷某個點是否在所畫范圍內(nèi)的具體代碼,供大家參考,具體內(nèi)容如下

IsPtInPoly.java

package com.ardo.util.circle; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * java判斷某個點是否在所畫范圍內(nèi)(多邊形【isPtInPoly】/圓形【distencePC】) 
 * @param point 檢測點 
 * @param pts  多邊形的頂點 
 * @return   點在多邊形內(nèi)返回true,否則返回false 
 * @author   ardo 
 */  
public class IsPtInPoly { 
   
  /** 
   * 判斷點是否在多邊形內(nèi) 
   * @param point 檢測點 
   * @param pts  多邊形的頂點 
   * @return   點在多邊形內(nèi)返回true,否則返回false 
   */  
  public static boolean isPtInPoly(Point2D point, List<Point2D> pts){  
      
    int N = pts.size();  
    boolean boundOrVertex = true; //如果點位于多邊形的頂點或邊上,也算做點在多邊形內(nèi),直接返回true  
    int intersectCount = 0;//cross points count of x   
    double precision = 2e-10; //浮點類型計算時候與0比較時候的容差  
    Point2D p1, p2;//neighbour bound vertices  
    Point2D p = point; //當(dāng)前點  
      
    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){//偶數(shù)在多邊形外  
      return false;  
    } else { //奇數(shù)在多邊形內(nèi)  
      return true;  
    }  
      
  }  
   
  /** 
   * 判斷是否在圓形內(nèi) 
   * @param p 
   * @param c 
   * @return 
   */ 
  public static String distencePC(Point2D p,Circle c){//判斷點與圓心之間的距離和圓半徑的關(guān)系 
    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 = "圓內(nèi)"; 
    }else{ 
       s = "圓上"; 
    } 
    return s; 
  }  
   
  public static void main(String[] args) {  
      
    Point2D point = new Point2D(116.404072, 39.916605);  
      
    // 測試一個點是否在多邊形內(nèi)  
    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("點在多邊形內(nèi)");  
    }else{  
      System.out.println("點在多邊形外");  
    }  
     
    // 測試一個點是否在圓形內(nèi)  
    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("點是否在圓內(nèi):"+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; 
  } 
   
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中的反射機制詳解

    Java中的反射機制詳解

    這篇文章主要介紹了JAVA 反射機制的相關(guān)知識,文中講解的非常細致,代碼幫助大家更好的理解學(xué)習(xí),感興趣的朋友可以了解下
    2021-09-09
  • springboot中@component注解的使用實例

    springboot中@component注解的使用實例

    這篇文章主要介紹了springboot中@component注解的使用實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java對接webservice接口的4種方式總結(jié)

    java對接webservice接口的4種方式總結(jié)

    因工作需要和一個Sap相關(guān)系統(tǒng)以WebService的方式進行接口聯(lián)調(diào),之前僅聽過這種技術(shù),但并沒有實操過,所以將本次開發(fā)進行記錄,這篇文章主要給大家介紹了關(guān)于java對接webservice接口的4種方式,需要的朋友可以參考下
    2023-10-10
  • Java并發(fā)編程示例(五):線程休眠與恢復(fù)

    Java并發(fā)編程示例(五):線程休眠與恢復(fù)

    這篇文章主要介紹了Java并發(fā)編程示例(五):線程休眠與恢復(fù),在本節(jié),我們將開發(fā)一個程序,使用sleep()方法來實現(xiàn)每秒鐘打印一次當(dāng)前時間,需要的朋友可以參考下
    2014-12-12
  • idea搭建可運行Servlet的Web項目

    idea搭建可運行Servlet的Web項目

    在網(wǎng)上看到一篇很詳細的 intelliJ IDEA 創(chuàng)建web項目并簡單部署servlet的圖文教程,今天自己也配置一下,留個筆記,感興趣的可以了解一下
    2021-06-06
  • Java迭代器實現(xiàn)Python中的range代碼實例

    Java迭代器實現(xiàn)Python中的range代碼實例

    這篇文章主要介紹了Java迭代器實現(xiàn)Python中的range代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java中Optional.of()方法及源碼解析(非常詳細!)

    Java中Optional.of()方法及源碼解析(非常詳細!)

    這篇文章主要給大家介紹了關(guān)于Java中Optional.of()方法及源碼解析的相關(guān)資料,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包,直譯過來就是信號量,被作為一種多線程并發(fā)控制工具來使用。本文將詳解其原理與使用方法,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • SpringBoot整合第三方技術(shù)的實現(xiàn)

    SpringBoot整合第三方技術(shù)的實現(xiàn)

    本文主要介紹了SpringBoot整合第三方技術(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評論

洞口县| 江油市| 黄平县| 濮阳县| 罗山县| 民丰县| 靖远县| 永顺县| 莆田市| 莆田市| 东方市| 横山县| 大城县| 南陵县| 得荣县| 沂源县| 岑溪市| 长治县| 运城市| 徐汇区| 灯塔市| 黄山市| 大石桥市| 丰台区| 佳木斯市| 宁阳县| 北安市| 内乡县| 基隆市| 祥云县| 来宾市| 鹤岗市| 江津市| 行唐县| 十堰市| 黄山市| 桂东县| 澎湖县| 南皮县| 民和| 将乐县|