這篇文章主要介紹java怎么判斷某個(gè)點(diǎn)是否在所畫(huà)多邊形/圓形內(nèi),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都做網(wǎng)站、安康網(wǎng)絡(luò)推廣、成都微信小程序、安康網(wǎng)絡(luò)營(yíng)銷、安康企業(yè)策劃、安康品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供安康建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
java判斷某個(gè)點(diǎn)是否在所畫(huà)范圍內(nèi)的具體代碼,具體內(nèi)容如下
IsPtInPoly.java
package com.ardo.util.circle; import java.util.ArrayList; import java.util.List; /** * java判斷某個(gè)點(diǎn)是否在所畫(huà)范圍內(nèi)(多邊形【isPtInPoly】/圓形【distencePC】) * @param point 檢測(cè)點(diǎn) * @param pts 多邊形的頂點(diǎn) * @return 點(diǎn)在多邊形內(nèi)返回true,否則返回false * @author ardo */ public class IsPtInPoly { /** * 判斷點(diǎn)是否在多邊形內(nèi) * @param point 檢測(cè)點(diǎn) * @param pts 多邊形的頂點(diǎn) * @return 點(diǎn)在多邊形內(nèi)返回true,否則返回false */ public static boolean isPtInPoly(Point2D point, Listpts){ int N = pts.size(); boolean boundOrVertex = true; //如果點(diǎn)位于多邊形的頂點(diǎn)或邊上,也算做點(diǎn)在多邊形內(nèi),直接返回true int intersectCount = 0;//cross points count of x double precision = 2e-10; //浮點(diǎn)類型計(jì)算時(shí)候與0比較時(shí)候的容差 Point2D p1, p2;//neighbour bound vertices Point2D p = point; //當(dāng)前點(diǎn) 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){//判斷點(diǎn)與圓心之間的距離和圓半徑的關(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); // 測(cè)試一個(gè)點(diǎn)是否在多邊形內(nèi) List pts = new ArrayList (); 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("點(diǎn)在多邊形內(nèi)"); }else{ System.out.println("點(diǎn)在多邊形外"); } // 測(cè)試一個(gè)點(diǎn)是否在圓形內(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("點(diǎn)是否在圓內(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; } }
Java的基本數(shù)據(jù)類型分為:1、整數(shù)類型,用來(lái)表示整數(shù)的數(shù)據(jù)類型。2、浮點(diǎn)類型,用來(lái)表示小數(shù)的數(shù)據(jù)類型。3、字符類型,字符類型的關(guān)鍵字是“char”。4、布爾類型,是表示邏輯值的基本數(shù)據(jù)類型。
以上是“java怎么判斷某個(gè)點(diǎn)是否在所畫(huà)多邊形/圓形內(nèi)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!