赞
踩
基本原理是逐条边判断与待检测点的距离
- public class Point{
-
- /**
- * x轴坐标
- */
- double x;
- /**
- * y轴坐标
- */
- double y;
-
- public Point setPoint(Point point){
- this.x = point.getX();
- this.y = point.getY();
- return this;
- }
-
- public Point() {
- }
-
- public Point(Float[] point){
- this.x = point[0];
- this.y = point[1];
- }
-
- public Point(Double[] point) {
- this.x = point[0];
- this.y = point[1];
- }
-
- 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;
- }
-
-
- }
工具类
- import java.util.ArrayList;
- import java.util.List;
-
- public class GeometryUtil {
-
- /**
- * 获取点与直线之间的距离
- * @param p 点
- * @param a 直线上一点
- * @param b 直线上一点
- * @return
- */
- public static double pointToLineDist(Point p,Point a,Point b) {
- double ABx = b.x - a.x;
- double ABy = b.y - a.y;
- double APx = p.x - a.x;
- double APy = p.y - a.y;
-
- double AB_AP = ABx * APx + ABy * APy;
- double distAB2 = ABx * ABx + ABy * ABy;
-
- double Dx = a.x, Dy = a.y;
- if (distAB2 != 0) {
- double t = AB_AP / distAB2;
- if (t >= 1) {
- Dx = b.x;
- Dy = b.y;
- } else if (t > 0) {
- Dx = a.x + ABx * t;
- Dy = a.y + ABy * t;
- } else {
- Dx = a.x;
- Dy = a.y;
- }
- }
- double PDx = Dx - p.x, PDy = Dy - p.y;
- return Math.sqrt(PDx * PDx + PDy * PDy);
- }
-
-
- /**
- * 获取点与多边形之间最近距离
- * @param point
- * @param points
- * @return 0.0 :点位于多边形内部 >0.0 : 点与多边形之间的最近距离
- */
- public static double pintoToPolygonMinDist(Point point, List<Point> points) {
- double dist = Double.MAX_VALUE;
- int N = points.size();
- for (int i = 0, j = N - 1; i < N; j = i++) {
- dist = Math.min(dist, pointToLineDist(point, points.get(i), points.get(j)));
- }
- return dist;
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。