直接上代码,并且附上了图形化的测试代码,
有图有真相, 已亲测
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* description: 判断一个点是否在一个由多个连续的点为成的图形内
*
* @author XXX
* @version 1.0
* @date 2022/10/18 17:44
*/
public class IsPtInPoly {
/**
* 判断点是否在多边形内
* @param p 检测点
* @param pts 多边形的顶点
* @return 点在多边形内返回true,否则返回false
*/
public static boolean isPtInPoly(Point2D p, List<Point2D> pts){
int N = pts.size();
//如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
boolean boundOrVertex = true;
//cross points count of x
int intersectCount = 0;
//浮点类型计算时候与0比较时候的容差
double precision = 2e-10;
Point2D p1, p2;
p1 = pts.get(0);
for(int i = 1; i <= N; ++i){
//点在多边形的顶点上
if(p.equals(p1)){
return boundOrVertex;
}
p2 = pts.get(i % N);
//不在两个点的X 坐标围城的区域内
if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){
p1 = p2;
continue;
}
//在两个点的X 坐标围城的区域内
if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
//在两个点与X轴围城的矩形内部
if(p.y <= Math.max(p1.y, p2.y)){
//两点连成的线与Y轴平行
if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){
//在线上
return boundOrVertex;
}
//两点连成的线与x轴平行
if(p1.y == p2.y){
if(p1.y == p.y){
//在线上
return boundOrVertex;
}else{
//在矩形内部
++intersectCount;
}
}else{
//(p2.y - p1.y) / (p2.x - p1.x) 为斜率
//xinters 为p点X坐标对应的线上Y坐标
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{
//(p.x == Math.min(p1.x, p2.x) && p.x == Math.max(p1.x, p2.x))
//必定与其中一个点的x坐标相同
if(p.x == p2.x && p.y <= p2.y){
//下一个点
Point2D p3 = pts.get((i+1) % N);
//在1,3 点之间
if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){
//在内部, 不直接返回true 的原因是,这个只是与X 轴的比较,不是与图行底部比较
++intersectCount;
}else{
不在内部
intersectCount += 2;
}
}
}
p1 = p2;
}
//在内部, 不直接返回true 的原因是,这个只是与X 轴的比较,不是与图行底部比较
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);
Point2D point = new Point2D(116.402, 39.914);
// 测试一个点是否在多边形内
List<Point2D> 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));
final Optional<Double> minX = pts.stream().map(Point2D::getX).min(Double::compare);
final Optional<Double> minY = pts.stream().map(Point2D::getY).min(Double::compare);
if(isPtInPoly(point, pts)){
System.out.println("点在多边形内");
}else{
System.out.println("点在多边形外");
}
JFrame jFrame = new JFrame();
JPanel jpanel = new JPanel() {
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
graphics.setColor(Color.black);
graphics.drawString("*", (int) ((point.getX() - minX.get()) * 10000), (int) ((point.getY() - minY.get()) * 10000));// 右腿(画直线)
for (int i = 0; i < pts.size(); i ++) {
int tail = i < pts.size() - 1 ? i + 1 : 0;
final int x = (int) ((pts.get(i).getX() - minX.get()) * 10000);
final int y = (int) ((pts.get(i).getY() - minY.get()) * 10000);
final int x1 = (int) ((pts.get(tail).getX() - minX.get()) * 10000);
final int y1 = (int) ((pts.get(tail).getY() - minY.get()) * 10000);
graphics.setColor(Color.red);
graphics.drawLine(x, y, x1, y1);// 右腿(画直线)
}
}
};
jFrame.add(jpanel);
jFrame.setSize(1000, 1000);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
// // 测试一个点是否在圆形内
// 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);
}
/**
* 圆形类
* @author ardo
*
*/
public static 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;
}
}
public static class Point2D {
private double x;
private 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;
}
@Override
public boolean equals(Object o){
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Point2D point2D = (Point2D) o;
return Double.compare(point2D.x, x) == 0 && Double.compare(point2D.y, y) == 0;
}
@Override
public int hashCode(){
return Objects.hash(x, y);
}
}
}