一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - java判斷某個點是否在所畫多邊形/圓形內

java判斷某個點是否在所畫多邊形/圓形內

2021-05-06 11:10阿杜_ardo Java教程

這篇文章主要為大家詳細介紹了java判斷某個點是否在所畫多邊形或圓形內的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

IsPtInPoly.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 圓形類
 * @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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
  }
   
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/ardo_pass/article/details/78552592?locationNum=2&fps=1

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 被老头肉至怀孕小说 | poverty中国老妇人 | 午夜影院在线免费观看 | 美女草b | 精品无码国产污污污免费网站2 | 久久精品男人影院 | 国产精品精品 | 精品视频手机在线观看免费 | tube性睡觉hd| 国产第一综合另类色区奇米 | 亚洲精品在线播放 | 娇妻中日久久持久久 | 午夜宅男宅女看在线观看 | 亚洲香蕉伊在人在线观婷婷 | 超级乱淫伦小说1女多男 | 色综合色狠狠天天久久婷婷基地 | 我和黑色丝袜班主任 | 亚洲99久久无色码中文字幕 | 日本精品一二三区 | 三星w699 | 免费看一级 | 全肉一女n男np高h双龙养成 | 国产亚洲精品看片在线观看 | 精品国产在线观看 | 啊啊啊好大在线观看 | 天美网站传媒入口网址 | 免费在线观看中文字幕 | 色综合图区 | porno美国xxxx| 好大好深好舒服 | 亚洲a在线视频 | 精品一区二区国语对白 | 欧美高清在线 | 美女禁区视频无遮挡免费看 | 精品一区二区三区在线视频观看 | 成人午夜影院在线观看 | 色综合久久最新中文字幕 | 日本高清在线播放 | 韩国三级理韩国三级理人伦 | 香蕉久久ac一区二区三区 | 欧美一级久久久久久久大片 |