java 实现电子围栏—判断点是否在指定区域内的方法

java 实现电子围栏—判断点是否在指定区域内的方法

一、意义

通过实现电子围栏—判断点是否在指定区域内方法,可以方便地对点的位置判断,从而实现复杂的功能。

二、方法

package use;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

public class PolygonUtil {
    /**
     * 判断点是否在多边形内
     * @Title: IsPointInPoly
     * @Description:判断点是否在指定区域内
     * @param point 测试点
     * @param pts 多边形的点
     * @return boolean
     * @throws
     */
    public static boolean isInPolygon(Point2D.Double point, List<Point2D.Double> pts, boolean bj){
        int N = pts.size();
        boolean boundOrVertex = bj;
        int intersectCount = 0;//交叉点数量
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        Point2D.Double p1, p2;//临近顶点
        Point2D.Double p = point; //当前点
        p1 = pts.get(0);
        for(int i = 1; i <= N; ++i){

            if(p.equals(p1)){
                return boundOrVertex;
            }
            p2 = pts.get(i % N);
            if(p1.equals(p2)){
                continue;
            }
            if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){
                p1 = p2;
                continue;
            }
            //射线穿过算法
            if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
                if(p.y <= Math.max(p1.y, p2.y)){
                    if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){
                        return boundOrVertex;
                        //return true;
                    }
                    if(p1.y == p2.y){
                        if(p1.y == p.y){
                            return boundOrVertex;
                        }else{
                            ++intersectCount;
                        }
                    }else{
                        double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;
                        if(Math.abs(p.y - xinters) < precision){
                            return boundOrVertex;
                        }
                        if(p.y < xinters){
                            ++intersectCount;
                        }
                    }
                }
            }else{
                if(p.x == p1.x && p.x == p2.x) {
                    if(p.y >= Math.min(p1.y, p2.y) && p.y <= Math.max(p1.y, p2.y)){
                        return boundOrVertex;
                    }
                }
                if(p.x == p2.x && p.y <= p2.y){
                    Point2D.Double p3 = p2;
                    for(int k = 1; k <= N; ++k) {
                        p3 = pts.get((i + k) % N);
                        if (p2.equals(p3)) {
                            continue;
                        }
                        break;
                    }
                    if (p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)) {
                        ++intersectCount;
                    } else {
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;
        }
        if(intersectCount % 2 == 0){//偶数在多边形外
            return false;
        } else { //奇数在多边形内
            return true;
        }
    }

    public static boolean isin(double x,double y,double [] xa,double [] ya)
    {
        Point2D.Double point = new Point2D.Double();
        point.setLocation(x,y);
        List<Point2D.Double> pts = new ArrayList<>();
        int l = xa.length;
        if(l==0)
        {
            return false;
        }
        for(int i=0;i<l;i++)
        {
            Point2D.Double pointa = new Point2D.Double();
            pointa.setLocation(xa[i],ya[i]);
            pts.add(pointa);
        }
        boolean is =isInPolygon(point,pts,true);
        return is;
    }

    public static boolean isin1(double x,double y,double [][] z)
    {
        Point2D.Double point = new Point2D.Double();
        point.setLocation(x,y);
        List<Point2D.Double> pts = new ArrayList<>();
        int l = z.length;
        if(l==0)
        {
            return false;
        }
        for(int i=0;i<l;i++)
        {
            Point2D.Double pointa = new Point2D.Double();
            pointa.setLocation(z[i][0],z[i][1]);
            pts.add(pointa);
        }
        boolean is =isInPolygon(point,pts,true);
        return is;
    }
}

三、使用例子

    public static void main(String[] args) {
        double [] a = {1,1,0,0};
        double [] b = {1,0,0,1};
        double [][] c = {{1,1},{1,0},{0,0},{0,1}};
        System.out.println(isin(0.1,0.1,a,b));
        System.out.println(isin1(0.1,0.1,c));
        System.out.println(isin(2,0.1,a,b));
        System.out.println(isin1(2,0.1,c));
    }

输出:

true
true
false
false

四、结尾

本方法经过本人多次分析与测试,如有更好的方法或者发现错误的地方欢迎大家评论与指正!

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于Java电子围栏算法实现: ``` import java.util.ArrayList; import java.util.List; public class ElectronicFence { private List<Point> points; public ElectronicFence() { this.points = new ArrayList<>(); } public void addPoint(Point point) { points.add(point); } public boolean isInside(Point point) { int intersections = 0; for (int i = 0; i < points.size(); i++) { Point a = points.get(i); Point b = points.get((i + 1) % points.size()); if (point.getY() > Math.min(a.getY(), b.getY()) && point.getY() <= Math.max(a.getY(), b.getY()) && point.getX() <= Math.max(a.getX(), b.getX()) && a.getY() != b.getY()) { double xIntersection = (point.getY() - a.getY()) * (b.getX() - a.getX()) / (b.getY() - a.getY()) + a.getX(); if (a.getX() == b.getX() || point.getX() <= xIntersection) { intersections++; } } } return intersections % 2 != 0; } } class Point { private double x; private double y; public Point(double x, double y) { 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; } } ``` 这个算法实现了一个ElectronicFence类,用于存储多边形的顶,以及判断一个是否在这个多边形内部。其中,Point类用于表示一个的坐标。isInside方法用于判断一个是否在多边形内,它通过计算这个与多边形的交数量来判断,如果是奇数个,则在多边形内,否则在多边形外。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值